Quick help with C++ coding

Ravioli

Microboner
Joined
Nov 1, 2004
Messages
5,097
Reaction score
2
Decided to learn a bit of C++ coding by myself just for the heck of it. Right now im trying to create a very simple "game" if you can call it that. Basically it asks you how much money you have, and you put the number in. Then you get to choose one of 3 houses, all with different prices. Then it subtracts the price of the house with whatever money you said you had, and then tell you how much money you have left.

The program executes, however, it doesnt do the calculations for the remaining money. Instead, at the end it just states whatever money you first said you had, instead of subtracting it by the price of the house. So i must have coded the actual calculation wrong, heres the code:



// house game.cpp : main project file.

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

int main()
{
int money;
int house1;
int house2;
int house3;
char choice;

house1 = -3000; //Price of the house
house2 = -5000;
house3 = -2000;

cout << "How much money do you have?\n";
cin >> money; //The money you have

cout << "What house would you like to buy?\n";
cout << "House1 for 3000\n";
cout << "House2 for 5000\n";
cout << "House3 for 2000\n";
cin >> choice;

switch(choice) { // case 1 represents House1 etc
case '1':
money + house1; // subtracts 3000 from money, the actual calculation, but it doesnt seem to do it.
cout << "You have " << money << " left\n";
break;
case '2':
money + house2;
cout << "You have " << money << " left\n";
break;
case '3':
money + house3;
cout << "You have " << money << " left\n";
break;
default:
cout << "Invalid Choice"; // if a choice other than 1,2,3 is made
}


return 0;
}

I even tried replacing money + house1; with money - 3000; but it still didnt work.






Also, on a side note. Whenever i run the program as a finished compiled .exe, it exits the DOS prompt as soon as the program is finished, how do i make it remain on screen?
 
/me peers into code and compiles it inside his head

This game sucks.
 
/me peers into code and compiles it inside his head

This game sucks.

hey, atleast you have the choice of starting of as rich!

EDIT: AHA, i got it working. Had to put += instead of just +, i dont know why, but thats just how it is.
 
I know it is a really lame way to do it, but what I would do to keep the DOS window open is just make a random variable and do a "cin >> randomVar" right before the return 0 at the end. That way it stays open until any keyboard input is done. Pretty much a "Press any key to exit" type of deal.

Also, the reason it needed a += is because you want to add AND assign the new value to the money variable. The way its done in that switch is just doing an addition but not assigning that new added value to anything. I'm also curious why you went with negative values for the houses instead of just using a subtraction for the cases.
 
I know it is a really lame way to do it, but what I would do to keep the DOS window open is just make a random variable and do a "cin >> randomVar" right before the return 0 at the end. That way it stays open until any keyboard input is done. Pretty much a "Press any key to exit" type of deal.

Also, the reason it needed a += is because you want to add AND assign the new value to the money variable. The way its done in that switch is just doing an addition but not assigning that new added value to anything. I'm also curious why you went with negative values for the houses instead of just using a subtraction for the cases.

Thx for the tip and the explanation for the =
I made the houses negative because it makes me feel like a math genius, so that i can feel better about myself.
 
I for one approve of your negative numbers, but it doesn't make a difference either way.

As for the +=, a usual way of doing it would be:
money = money + house1;
which adds money and house1 and assigns the result to money.

Doing something to a variable and assigning it to the same variable is a common enough operation that they made operators like "+=", "-=", "&=", etc. to facilitate it.

money + house1;
just adds the two together as discards the result.

Why does the compiler let you do something so stupid? Because as far as error-checking goes, this is just an int value and a semicolon, so the following are equivalently valid:

money + house1;
money;
2 + 4;
4;
FunctionReturningAnInt(money, house1);

Only the last one of these could be useful, because the function could have user input, write to files, and otherwise have "side effects", and we may only want the side effects and not care about its return value. It's useful enough to say that just a value on a line shouldn't be an error.

Actually, your "Press any key to continue" problem could be done without an extra variable using this idea.

char GetChar() {
char input;
cin >> input;
return input;
}

"cin >> choice;" could then be "choice = GetChar();". (Not an improvement, but a possibility.)
To make your program wait at the end, you could add the line "GetChar();". You just care about the side effect -- the computer waiting for user input -- but not the return value, so you just let it be discarded.

Or you could just run your program from the command line.
 
Back
Top