954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need Help with a simple dos program

Hey guys, i am very new to C++ but i know some little things about it.
I am trying to make a simple dos program with simple arithmetic but i forgot how to do some things since it has been ages since i last coded.
I am trying to make a game sort of thing where you can buy houses, and when you buy a house, the cost of the house is taken away from your balance. I am also thinking about somehow making the value of the houses randomly rise and drop by its self.

Here is some unfinished code, i am going to make it better, but yeah.

#include <iostream>
using namespace std;

int main() {
    int balance;
    int House1;
    
    balance = 500,000;
    House1 = 150,000;
    
    cout << "Hi, Welcome to the game!\n";
    cout << "Your balance is $500,000.\n";
    cout << "Type 'House1' for The $150,000 house\n";
    cout << "\n";
    
    
    
         cin.get();
         return 0;
}


I also have a problem where, when i press enter, it closes the program, and the

cin.get()


is the only thing that is stopping it from closing automatically without me being able to read anything.

I forgot how to make it so when i type something and press enter, it displays what i wanted it to.
Thanks for any help.

Cyken
Newbie Poster
3 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

>>I forgot how to make it so when i type something and press enter, it displays what i wanted it to.

use cin to get keyboard input

int price;
cout << "Enter price of house\n";
cin >> price;
cout << "The price you entered is " << price << "\n";
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Ah thanks, i tried that, but when i press enter the program closes :(
So like, it appears and stays there but as soon as i press enter it closes. Is there any way to stop that?

Cyken
Newbie Poster
3 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

The reason it closes is because cin left the Enter key '\n' in the keyboard buffer, and the next cin.get() just removed it. To fix this problem you need to flush the keyboard buffer after entering numeric data. One way to do that is call ignore(). See Narue's article in the c++ forum stick post about details of how to flush the input keyboard buffer.

cin >> price;
cin.ignore(); // flush '\n'
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Thank you for your help. :)

Cyken
Newbie Poster
3 posts since Mar 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You