Does not pause to allow input

Pages: 12
OK, I am really, really new at this. Let me paste my code, and I will put at the bottom what I am experincing There is a .h file and another .cpp file also that this draws from, but they seem to be fine, and this is simple enough that you can probably infer what is in them. I will also paste them if needed though.

#include <iostream>
#include "cash3.h"
using namespace std;



int num1;
int num2;
int num3;
int rand1;
int decision;
int Y;
int y;

int main()
{
cout << "Welcome to the Cash 3 Lottery!!" << endl;
cout << "Would you like to play a game?!" << endl; //Just so I can set decision intially to Y
cin >> decision;


//do
//{



cout << "Enter 3 numbers, with a space between each one" << endl;
cin.get();
cin >> num1 >> num2 >> num3;
cout << "Your numbers are " << cout << num1 << num2 << num3 << endl;
cout << "The winning numbers are ";

Cash3 MyObject; //Create object of cash3

cout << MyObject.GetFirstNumber(); //call and output all getnumber functions of MyObject
cout << MyObject.GetSecondNumber();
cout << MyObject.GetThirdNumber();

if ( num1 == MyObject.GetFirstNumber()) //nested ifs only return true if all 3 are true
{
if ( num2 == MyObject.GetSecondNumber() )
{
if (num3 == MyObject.GetThirdNumber() )

cout << "You have won Cash3 !!! Would you like to play again?\n";
}
}
else
cout << "Sorry, you did not win. Would you like to play again?\n"; //If any of the three are false

cin >> decision;
//}while (decision == Y || y); //end do while

}

This compiles ok (VS2008)

Problem 1)

It stops to allow user to enter the first "Y", but does not stop to allow entering of num1, num2, num3...just blasts through rest of program without stopping. cin.get(); was aded in an attempt to fix this (found this solution on forum) but does not seem to do anything to help.

Problem 2)

The do while statment and the

int decision;
int Y;
int y;

stuff was written in an attmept to have it loop back to the do, if user entered "Y" to play again. Since program does not stop for this input either,, there is never a chance to change decision to anything except Y, so program loops back uncontrollable forever (you see that I have commeneted it out as an aide to troubleshooting).

I am completely new at this, and to the board, so I pre-apologize if I am not posting and asking correctly. I did read the "read before posting" :)

please help.


---1---
You might want the type char for your decision. Keep the std::cin.get().

EDIT: Although, this piece of code may be of interest to you:
std::cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
This clears the whole stream, leaving nothing in it. Nothing. You'll need to #include <limits> .

---2---
decision == Y || y
This doesn't do what you think it does. To have individual character comparisons, consider single brackets 'y'.

And the or operator doesn't work like that. You need the comparison done on both sides, not just the left.

You're one std::cin.get() or one std::cin.ignore(...) short.

---3---
[ code] tags?

rand1 is unused.

You have a redundant cout here:
"Your numbers are " << cout

Welcome to the forum.

-Albatross
Last edited on
1--
char seems to make more sense. Instructor seemed to think int was OK, but I think char makes more sense.

2--yes, I remember now he said to use single qoutes. Aside from that he semed to think it would work though. At least my earlier version which was "while decision=='y'". My understanding of what it does is that program will loop back as long as decision value equals Y. It would not suprise me to find out anything here does not do what I think it does.

3--
Not sure what you are saying here, but yes, rand1 was a leftover from an earlier attemept, not needed. Will delete.

The second cout here is what you mean, right?

cout << "Your numbers are " << cout << num1 << num2 << num3 << endl;

I think my original thought was that I had to cout each num seperately. Obviously I never even finished that thought. Will delete.

Thank you for the help. I really do NOT understand any of the syntax here.. I will make these changes and see if it helps any.

---3---
Here's an article regarding tags:
http://cplusplus.com/articles/firedraco1/

Yes.

Note that you need to also delete one of the << operators just before or just after the cout to be deleted as well.

You don't understand the << operator with a cout before it, and yet you're using classes? That's a problem...
http://cplusplus.com/doc/tutorial/
^This should help you quite a lot as far as your understanding is concerned.

Feel free to contact me with any questions.

-Albatross

Last edited on
Yea I know. I think this is being taught very awkwardly, and I have complained, but you know how that goes.

Thank you for the article. will read in a sec. Im in amazement that you have alrady been able to fix most of this. I have uncommented the do while, so it will operate and it actually seems to work (!!!!!!). I know, you are falling off your chair at the newb here, but thats all right :)

However it loops back no matter what is entered in response to Would you like to play again?\n";


So I assume that the "decision thingie" is not working. Updated code follows.

#include <iostream>
#include "cash3.h"
using namespace std;



int num1;
int num2;
int num3;
char decision;
char Y;
char y;

int main()
{
cout << "Welcome to the COP Cash 3 Lottery!!" << endl;
cout << "Would you like to play a game?!" << endl; //Just so I can set decision intially to Y
cin >> decision;


do
{



cout << "Enter 3 numbers, with a space between each one" << endl;
cin.get();
cin >> num1 >> num2 >> num3;
cout << "Your numbers are " << num1 << num2 << num3 << endl;
cout << "The winning numbers are ";

Cash3 MyObject; //Create object of cash3

cout << MyObject.GetFirstNumber(); //call and output all getnumber functions of MyObject
cout << MyObject.GetSecondNumber();
cout << MyObject.GetThirdNumber();

if ( num1 == MyObject.GetFirstNumber()) //nested ifs only return true if all 3 are true
{
if ( num2 == MyObject.GetSecondNumber() )
{
if (num3 == MyObject.GetThirdNumber() )

cout << "You have won Cash3 !!! Would you like to play again?\n";
}
}
else
cout << "Sorry, you did not win. Would you like to play again?\n"; //If any of the three are false

cin >> decision;
}while (decision == 'Y' || 'y'); //end do while

}

I have additional problem with this

// Call Cash3.h draw function, define object

void Cash3::Draw()
{
srand(time(0));

size_t firstNumber_ = (rand() % 10);
size_t secondNumber_ = (rand() % 10);
size_t thirdNumber_ = (rand() % 10);
}
WHich is SUPPOSED to generate 3 random numbers between 1 and 9

Instead it returns the following

-858993460-858993460-858993460
ah. I see what tags are and will try to use the code tag from now on. I can see that it would be very helpful Not sure I understand the output one, and not sure I need the rest at the moment. Is there a forum for test posts?
EDIT: You can use the preview button just to the left of the submit button to preview your posts. Nothing shows up forum-side. There is no forum for experimentation with these features.

And as for the newbness... heh, we all started somewhere.

Right then...

while (decision == 'Y' || 'y');
I warned you about this line, although I suppose I wasn't clear enough. You were right in suspecting it though. You need a comparison on both sides of the or operator, else it will just return true regardless of the lefthand side (Why? Because 'y' is a nonzero value). It should be more like:
while (decision == 'Y' || decision == 'y');
*I really shouldn't give out code solutions, but I did try to explain...*

As for your function... okay. First of all, you could have used a simple int but that's a detail. What's more important is that those three variables you declared and defined in the same lines are discarded at the end of your function. They aren't from a larger scope (i.e. your class), so they are just... discarded.

To access a variable or function in your class from inside a function that's inside the class (did you follow that?), here's a semi-syntactical template for you to use:
Cash3::variable;

-Albatross
Last edited on
1. While I agree with the concept of pointing someone in the right direction and then having them think and learn for themselves, I am just not even to that level yet. I would just continue chasing my own tail, and lord knows theres been a lot of that here. The tutorial you posted looks like it will be a lot of help.

2. The header file was provided by the instructor. When I quesioned the use of "size_t" instead of just "int", I got some gobblygook about it just being an unsigned integer and it not really mattering. I wasnt happy with that the answer then....and am even less now. I also told him that diving off into classes before giving us a working knowledge of the syntax was a bad idea...but....

3. Here is .h file guts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <cstdlib> //size_t.

//The Cash3 class declaration.

class Cash3
{

    public:
    
        //void setFirstNumber(string);
       // void setSecondNumber(string);
        //void setThirdNumber(string);
        
        int GetFirstNumber();   //Returns the first lottery number.
        int GetSecondNumber();  //Returns the second lottery number.
        int GetThirdNumber();   //Returns the third lottery number.

        void Draw();            //Randomly draws three lottery numbers from 0 to 9,
                                //storing each number in the corresponding variable below.

    private:

        size_t firstNumber_;    //The first lottery number.
        size_t secondNumber_;   //The second lottery number.
        size_t thirdNumber_;    //The third lottery number. 
I dont know whats the norm for c++ is ( or any other language for that matter), but the goal being taught here is that the classes are created in the .h file, objects that define them are put in a .cpp file, and prgram main and all I/O functions are put into a driver.cpp file.

This is the guts of my .cpp file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Functions to get the FirstNumber, etc
int Cash3::GetFirstNumber()
{
    return firstNumber_; //returns objects firstNumber
}
int Cash3::GetSecondNumber()
{
    return secondNumber_; //returns objects secondNumber
}
int Cash3::GetThirdNumber()
{
    return thirdNumber_; //returns objects thirdNumber
}

// Call Cash3.h draw function, define object 

void Cash3::Draw()
{
srand(time(0));
      
      size_t firstNumber_ = (rand() % 10); 
      size_t secondNumber_ = (rand() % 10);
      size_t thirdNumber_ = (rand() % 10);
}
EDIT2: How do you manage to get in a post just before I do?!? Goodness! *laughs*

Your first three functions are correct syntax-wise (though I think I'll have a chat with your professor about the difference between signed and unsigned types, give him a piece of MY mind).

I will ask you to think a little bit regarding the third; it shouldn't be too hard.

Consider this psuedotemplate:
class::variable;
Where class is the name of your class (not the object) and variable is the name of a variable within that class.

When used within an implementation of a function (in other words when it's used in the code of one of the functions of your class), this allows direct access to the variable. So if you wanted to access and modify firstNumber_, you'd have
Cash3::firstNumber_
...which you would then treat like a variable name (you don't need to state the type, you can do everything with it you could with a variable inside your function). That's how you access those variables; not be redeclaring them within a function (which just creates a variable with the same name but only in the function).

Do you see what you need to do now?

-Albatross

EDIT: 15 posts, and counting. Three cheers for binary!
Last edited on
I thought that might have been the case, that I got that second one in while you were writing. So I stopped here for a minute....

Here is my understanding of the cpp file. I am (attempting to) assign random numbers to my private data mambers, (by defining the cash3.h "draw" funtion here.

Then I defiine cash3.h GetFirstNumber with the "return firstNumber_; " which makes the value of the private data member available to me as a public function.

How am I doing so far?

It seems to me that these are being done in the wrong order here though.
Maybe thats not the right wording. How about: value of the private data member available to me as a public "data member", so I can later manipulate it in the driver.cpp file.
Your Get functions are designed to allow you to access the variables' values without being able to set them.

The order in which you implement your functions however, in this case, doesn't matter much.

EDIT: /desk You managed another post. Goodness, I'm slow tonight. *becomes coffee zombie*.
I'm not the strongest person on the vocabulary (due to the fact I'm self-taught), however I'd write:
The Get functions permit me to obtain the value of the corresponding private data member publicly, but not set it.


-Albatross
Last edited on
regarding my understanding of srand and rand:

srand(time(0)); initializes srand at system time (time since earth was born or something), so as to pick up the srand sequence in a differnt sopt each time.

Each itteration of rand() is supposed to pick a number randomly far awaty from srand(time(0));

Since the numbers that are generated from the above sequence contain many (how many?) digits, and I only want one digit between 1 and 9 .... % 10- keeps the remainder ( randomg ) after the number has been divided by 10. "10", doesnt seem to be near the amount I want though, if the resulting rand numbers are really that big...
 Your Get functions are designed to allow you to access the variables' values without being able to set them. 


Works for me.

 The order in which you implement your functions however, in this case, doesn't matter much. 


seems counterintuitive to me, but I wont belabor it. Im sure this will become clearer after I do a little more of this.
time() returns the number of seconds since... I think it was January 1, 1970, a historic date of sorts. It's use in srand() has been understood correctly.

rand() returns a psuedo-random number that changes each time the function is called and is dependent on the seed which was set by srand(), so I think you understood that as well.

The numbers are random garbage that was in the memory space before your program allocated it, and they are not set to anything else after that. Did you see why?

EDIT:
It seems counterintuitive now. However, this might help a bit. Your header file contains a prototype of your class and therefore all those functions, so they can call upon each other without having their guts written out yet; your compiler later deals with pairing the implementation and the prototype. If you write a prototype and use it without writing an implementation, then the compiler will give you an error on one very specific stage.

-Albatross

Last edited on
You are saying that I can manipulate things out of order, as long as nothing external calls it yet?

No, I do not understand why its not working. Here is what I want to /think is happening.

srand rand deal (implementation, Ill try to use the correct lingo here) creates the numbers I want and assigns them to my prototype values ( I like to think of these as placeholders) in my .h file.

The return functions implemented in the .cpp file convert them into public data I can access ( as we discussed above)

Then, later in my driver file I attempt to call this info by creating an object ( I like to think of these as "instances") of cash3 class called MyObject and then use MyObject.GetFirstNumber() to make this data able to be compared with user input numbers, and to be also output.
I'm saying that you can implement things out of order so long as you have the appropriate prototypes before all the calls are made, which is close, yes.

This is, I'm sorry to say, the third time I have attempted to explain why your Draw function isn't working (as it is the remaining thing that isn't working). I'm patient, though.

Here's what's actually happening.

You create your MyObject, which is an instance of Cash3, which also creates your three private variables, which are assigned to some random garbage left over by another program. You then call the routine Draw, which initializes three variables named just like the private variables in MyObject, but on a function-wide scope alone. They are discarded when Draw finishes, and the variables you wanted to be changed are left untouched. Then, when you call your Get functions, they successfully get the values of the private variables, which are that random garbage.

Do you see the problem now?

-Albatross
Last edited on
In my defense Ill say that its 1 AM here, and I wanted you to understand how little I understood what I was doing. Else wise you'd never be able to break it down into words I can understand. Ive spent most of my time doing that, which is of course, also helpful to me, explaining aloud the things that I understand and the things that I do not understand, so
Its a good thing that you are patient.

My understanding now is this: The fact that the implementations are not in sensible order in the .cpp file is not important because all thats really going on in there is defining implementations of the header prototypes. Any actual values generated by whats going on in there....are just incidental....

However, the order in which they are CALLED in the driver file is important because here is where the logical, computational, work is really done, and I dont see a "Draw" call of the object anywhere in the driver file. The Draw call of the object needs to be made before I attempt to use the get call in the driver file, else, I just get garbage.
Last edited on
Pages: 12