Hello, I am working on a project for my C++ class. I don't want a complete step by step instruction of how to accomplish this, but if someone could point me in the right direction I would appreciate it.
I ask a yes or no question at the beginning. If the answer is yes (y), the program goes to a set of questions, if the answer is no (n), It goes to a separate set of questions. I got this to work fine, but thought I would try to go above and beyond what the instructor asked. I want the program to output an error and go back to the first question if the answer is anything except 'y' or 'n'.
So far I have:
if (input == 'y') ......
else if (input == 'n').......
else cout << 'error message'
I can't find anywhere in my book that can help me point the code back to the beginning. I tried searching the internet, but I don't really know what to search for. Any help would be greatly appreciated.
while (do_you_want_to_start_again == true)
Prompt user for'y' or 'n'
- if'y'
-- function_firstSet
- elseif'n'
-- function_secondSet
- else
-- Error. That was not a valid input.
Prompt user if they'd like to start again ('y' or 'n')
- if 'n'
-- do_you_want_to_start_again = false //if they don't want to go through it again, it exits the loop
- elseif !='y'
-- Error. That was not a valid input. //will start again
end while
Sample Output:
Do you like pie? <y> or <n>
> y
Why do you like pie?
...
...
...
Would you like to start again? <y> or <n>
> n
Goodbye...
Another Sample:
Do you like pie? <y> or <n>
> n
Why don't you like pie?
...
...
...
Would you like to start again? <y> or <n>
> y
Do you like pie? <y> or <n>
>
...
...
...
Of course this is missing a lot of elements (variable names, proper function use, syntax, ect) but it will hopefully prove to be some use.
Thank you for your help. This is the code I have so far. I am not even close to done, but I was just trying to get the questions and answers to work before I got in too deep. I don't really know the best way to code yet, but I've only been doing it for a couple of weeks. Anyway, I don't want to bother you too much, and this is a class project, so I don't want anyone to fix my code, but I would like to know if you think the 'while' expression is the best (or only) way to fix this. I've been messing around with this thing for a few hours, obviously not very long for a career programmer, but I am getting frustrated that I can't find it in my book. I thought it would be a simple little thing to have it ask the question again.
cout << "Welcome to the fare calculator." << endl;
cout << "Are you driving a vehicle onto the ferry? (y/n): ";
cin >> vehicle;
if (vehicle == "y")
{
cout << "Is the driver a senior citizen (65 or over) or disabled? (y/n): ";
cin >> driver;
cout << "How many passengers are in your vehicle (excluding the driver)" << endl;
cout << "Adults (age 19 to 64): ";
cin >> adult;
cout << "Senior Citizens (65 or older), or Disabled Persons: ";
cin >> senior;
cout << "Youth (5 to 18 years old): ";
cin >> youth;
cout << "Is your vehicle over 7 feet, 6 inches in height? (y/n): ";
cin >> height;
cout << "How long is your vehicle in feet? ";
cin >> length;
}
elseif (vehicle == "n")
{
cout << "How many adults (age 19 to 64): in your group? ";
cin >> adult;
cout << "How many senior citizens (65 or older), or disabled persons in your group? ";
cin >> senior;
cout << "How many youth (5 to 18) in your group? ";
cin >> youth;
cout << "How many bicycles in your group? ";
cin >> bikes;
}
else
{
cout << "I don't understand, please try again." << endl;
cout << "Are you driving a vehicle onto the ferry? (y/n): ";
cin >> vehicle;
}
I left out as much as I could, but hopefully left enough so you can see what I'm doing. I was hoping that at the end when it asks the question again and gets the input 'vehicle', that it would go back to the top and start the questions, but it just ends.
If you are storing the input in a char you should probably change the if-statement to use single quotes instead of double quotes (as that is a string).