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.
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace 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;
}
elseif (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
if (number == randomNum)
break;
elseif (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;
}
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.
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
}
elseif (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');