I Need C++ Help

closed account (967L1hU5)
So, in a game I'm making, the players have the option to invest their money, using said code:
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
            if (money >= 200)
            {
                cout << "How much would you like to invest? You have $" << money << "." << endl;
                cin>> invest;
                if (invest > money)
                {
                    cout << "You don't have that much." << endl;
                }
                if (invest <= money)
                {
                    cout << "You're about to invest $" << invest << ". Are you sure?" << endl;
                    string investyn;
                    getline (cin, investyn);
                    if (investyn == "yes")
                    {
                        money = money - invest;
                        cout << "You have invested $" << invest << "." << endl;
                        investyn = 1;
                    }
                    if (investyn == "no")
                    {
                        cout << "Okay than." << endl;
                    }
                }
            }

(Sorry for the long spaces, it's quite a few "if's" in)

The only problem is that at cout << "You're about to invest $" << invest << ". Are you sure?" << endl; , it seems to end the "if" function their, and returning to my "while" loop. It is totally skipping the getline, so there's no chance that I just misspelled my command. Does anyone know what I did wrong? Becuase this is a big project for me (over 2,200 lines of code) and I'd hate to kill it or leave out a good feature because of this.
It's something to do with the state of cin. There are a lot of posts on this site that deals with left-over characters in the input stream. I think one of the options is to call ignore (cin.ignore() ). Try searching this site for those issues.
closed account (967L1hU5)
What would (cin.ignore() do?
closed account (967L1hU5)
So it cuts, for no better words, strings?
Extracts characters from the input sequence and discards them.

The extraction ends when n characters have been extracted and discarded or when the character delim is found, whichever comes first. In the latter case, the delim character itself is also extracted.

1
2
3
4
5
6
Parameters
n 
    Maximum number of characters to extract (and ignore).
    This is an integer value of type streamsize. 
delim 
    Delimiting character. 
closed account (967L1hU5)
I owe to you the future of my game. Which if you'd want to check it out... http://dl.dropbox.com/u/17356291/web/downloads/Actual%20Entity.exe That version is slightly outdated thou.
I'm glad I was able to help. Good luck, and keep us all posted on your progress!
closed account (967L1hU5)
Indeed I will. BTW, do you know how to make a linux/ubuntu executable file from Windows? I'm using Windows 7 and code::blocks
I don't know what code::blocks are, but the code you have posted should port just fine. You should have the GNU C++ compiler and GNU make (gmake) on the system, if not, they are free downloads (just Google them!)
closed account (967L1hU5)
Code::blocks is a free and open source IDE for C/C++. I think there's also plugins for python.
Last edited on
Topic archived. No new replies allowed.