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.

Recommended Answers

All 4 Replies

>>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";

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?

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'
commented: It's nice to know you're still at it, helping people =) +1
commented: Very well explained ! +1

Thank you for your help. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.