continue statement with more

im working on an assignment for class and i am completely stuck i try different things and get different errors. what i am doing is writing a program for guessing how many cars pass through the bridge and if the user guesses right it breaks the statement or if they guess wrong it breaks the statement to for close. but i need to have an if else with a continue in there and break.

heres what i got so far


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
26
27
28
29
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;

int main()
{
	srand(time(0));
	int randomNum = rand() % 10;
	char ch;
	int number = 0;
         do
	{
		cout <<"Guess how many cars pass through the bridge?:";
		cin >>number;
		
		if (number == randomNum)
		break;
		{
			cout <<"You guessed the right amount of cars to pass through the bridge.!"<<endl;
	    }
		else if (number != randomNum)
		{
			cout <<"Sorry you guessed the wrong amount of cars to pass through the bridge.Try again?(Y/N)"<<endl;
              cin>>ch;	
		}
        }while(ch=='y' || ch=='Y');
return 0;
}


over my review of this program so far i can see where it would end automatically but unfortunately i cant have it like that
just reorder your blocks like this.
1
2
3
4
5
6
7
8
9
10
11
if (number == randomNum)
        break;
else if (number != randomNum)
{
	cout <<"Sorry you guessed the wrong amount of cars to pass through the bridge.Try again?(Y/N)"<<endl;
        cin>>ch;	
}
else
{
        cout <<"You guessed the right amount of cars to pass through the bridge.!"<<endl;
}
Last edited on
thanks but how does the continue statement work into that?
how come when i try to guess a right number it wont tell me if i got it right, it just breaks the statement?
the only thing im noticing now is no matter what is under else if it will say you guessed the wrong number if u have that statement there and for the under else statement it says the same thing if u switch them.
Try this:

I didn't test it, but it should work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
do
	{
		cout <<"Guess how many cars pass through the bridge?:";
		cin >>number;
		
		if (number == randomNum)
		
		{
			cout <<"You guessed the right amount of cars to pass through the bridge.!"<<endl;
                        break; //you need the break after the cout statement if you want it to show output
	        }
		else if (number != randomNum)
		{
			cout <<"Sorry you guessed the wrong amount of cars to pass through the bridge. Try again?(Y/N)"<<endl;
              cin>>ch;	
		}
        }while(ch=='y' || ch=='Y');
now with that working could i put in a continue anywhere?
bump
am i doing something wrong? i just need a continue in there but if i cant id like to know so i can make a new program.
Topic archived. No new replies allowed.