Why is the varible not changed?

Hello, for some reason when i try to run my script, there are no errors but when i try to get the people to build houses, it screws up, i have no idea take a look



loop_play:
cin >> play;

if (play=='y' ||play=='Y')
{

while ( food != 0 and people>houses )
{
loop_game:
cout << "You have "<< people << " people in your city, "<< food << " food for your people, and " << houses << " houses for your people. \n";
cout << "What do you want your people to do? \n";
cout << "They can advertise/farm/build houses. \n";
cout << "\n";
cout << "\n";

loop_error:
cin >> work;
if (work=="advertise")
{
people=people+5;
food=food-1;

cout << "Your people are working to get more people to your city! \n";
cout << "They have managed to get 5 more people to your city! \n";
cout << "Now you have " << people << " people! \n";

cout << "\n";
cout << "\n";

goto loop_game;
}
if (work=="farm")
{
food=food+10;
food=food-1;
cout << "Your people are farming for you! \n";
cout << "They farmed 10 more food! \n";
cout << "You now have "<< food <<" food! \n";

cout << "\n";
cout << "\n";

goto loop_game;
}
if (work=="build houses")
{
houses=houses+2;
food=food-1;
cout << "They are working to build you houses. \n";
cout << "They just finished building 2 houses! \n";
cout << "You now have " << houses << " houses for your workers! \n";

cout << "\n";
cout << "\n";

goto loop_play;
}


so when i run it, and try to get them to work on the houses, i type that in and it says "you have put an invalid variable" like i told it to if it didn't recognize the variable, thanks
up front:

1. Excessive use of goto. Only use gotos if you seriously have no other option and I've only ever had to use a goto once in 20 years or so.
2. Use the [code] tags with the corresponding /code within brackets to keep indentation and number the lines to make it more readable.
3. The variables used in your build houses conditional are defined somewhere other than this code fragment.

In short, it's hard to say what is wrong because there isn't the right pieces of info posted and what is posted is hard to read.

What do you use in order to get back to a certain line?
What else do you need me to add to the info, I actually gave up and the project and started a new one but its good for reference

and when trying to use it as code do you put it like, example_text or example_text sorry about the wait for a replay
I assume work is of type std::string. cin >> work; This only read input until the next whitespace character so word will never contain more than one word. If you want to read the whole line you can use std::getline.
Last edited on
so when applying this, do i need to put at the beginning std::getline and then rewrite it? could you give an example please
Replace cin >> work; with getline(cin, work);.

std::getline reads input until it finds a new line character '\n'. If you have used operator>> to read input there will probably be a newline character left in the stream. When you then use getline it will immediately find the newline character and work will be empty. To fix this problem you can put cin.ignore(numeric_limits<streamsize>::max(), '\n'); after cin >> play;. What it does is just that it ignores the rest of the line so that input after starts on a new line.
ok so i want to try the first thing you mentioned, but will it still get input if getline(cin, work); if so what do i type to get it if there is more that 1 getline such as

1
2
3
4
    getline(cin, work);
    getline(cin, play);

getline// do i need to add something to the name like getline_1(cin,work);? 

? also, to the second do i need to include any functions to get this to work?

sorry about it being hypothetical, due to lack of the program still
std::getline only works with strings and it puts the input in the string passed as the second argument. If you only use getline to read input you don't need to call ignore.

The function is called getline so you don't need to add anything to the function name.
what about if i want to print it somewhere or to reuse it again and again? should i only create one then use it while i need it and then rewrite it again?
getline is a function. You can call it whenever you like.

What do you use in order to get back to a certain line?


You could chain your if statements so one and only one will execute at any one time.

1
2
3
4
5
6
7

if (work=="advertise")
   do advertising work;
else if (work == "farm")
   do farming work;
else if (work == "build")
   do building work;


Then only one code block will execute and flow drops down to the end of the while loop without testing any conditionals that follow. Also, considering your work choices are exclusive, if work == "advertise", that means work != "build houses" so none of the conditionals following the advertise block will test true. They will still execute the test, but they will test false and flow will eventually fall down to the end of the while loop.

For loop_play, I would nest the whole of that within another loop.
Last edited on
so is this correct?

1
2
3
4
5
6
cin >> work;
cout << "Blah blah blah \n";


getline(cin, work);//does this return to that line?

or
1
2
3
4
getline (cin, work);
cout << "Blah blah blah \n";

getline (cin,work);//do i type it in again to reuse it? 
Last edited on
No, replace your cin input statement with getline.

It would be simply

getline(cin, work);

One time, within a loop that continues to get the player's choice each turn.
so how do you end it? or continue, and couldnt you just create a while loop to do that?

please give an example showing the output of it
1
2
3
4
5
6
7
8
do
{
    
    while ( food != 0 and people>houses )
   {
   }
    getline( cin, play );
} while ( play == 'Y' || play == 'y' );


As long as the player presses y, play will continue with the albeit that they will have to play at least one turn. You could also test for n instead so play will continue as long as the player does NOT press n.
i thought about doing that but it seemed to be alot of extra work, i was wondering if there was a more effiecient way
okay ill give that a try thanks for the help much appreiciated
Topic archived. No new replies allowed.