Jump to content

Hello guys, I have a program for class that I am trying to finish. For the third choice I am trying to figure out how to make this loop back to the beginning but I am just not sure how and I have been sitting here for a couple hours and it is starting to frustrate me.

 

Once I figure out how to re-loop back to the beginning for choice number 3 if they pressy then I can just copy and paste it for the other 2 options. Right now when I press y after it asks Y/N, nothing happens. Thank you.

 #include <iostream>#include <string>#include <iomanip>using namespace std;int main(){    // 11/12/2014    bool travelAgain = true;    string name = "";    int month = 0;    int day = 0;    int year = 0;    int timeChoice = 0;    int timePeriod = 0;        cout << "Welcome to the Time Machine" << endl;    cout << "What is your name? ";    getline(cin, name);    cout << "Hello " << name << ". ";        while (travelAgain){        cout << "Select an option to get started:" << endl;        cout << "1. Enter a Specific Date" << endl;        cout << "2. Select a Time Period" << endl;        cout << "3. Wildcard - I'm feeling lucky\n" << endl;        cin >> timeChoice;        while (timeChoice < 1 || timeChoice > 3){            cout << "Sorry, must be a 1, 2 or 3.\n";            cout << "1. Enter a Specific Date" << endl;            cout << "2. Select a Time Period" << endl;            cout << "3. Wildcard - I'm feeling lucky\n" << endl;            cin.clear();            cin.ignore(256, '\n');            cin >> timeChoice;        }        if (timeChoice == 1){            cout << "What is the month ? (enter 1 - 12) ";            cin >> month;            while (month < 1 || month > 12){                cout << "Must be between 1 - 12" << endl;                cout << "What is the month ? (enter 1 - 12)\n ";                cin.clear();                cin.ignore(256, '\n');                cin >> month;            }            cout << "What is the day? (Enter 1-31) ";            cin >> day;            while (day < 1 || day > 31){                cout << "Must be between 1 - 31" << endl;                cout << "What is the day? (Enter 1-31)\n ";                cin.clear();                cin.ignore(256, '\n');                cin >> day;            }            cout << "What is the year? ";            cin >> year;            while (year <= 0){                cout << "Year must be a number: " << endl;                cin.clear();                cin.ignore(256, '\n');                cin >> year;            }            cout << "Ok, so we will send you to " << month << "/" << day << "/" << year << endl << endl;            cout << "Restarting the time machine program \n";        }        else if (timeChoice == 2){            cout << "Choose from one of these time periods.\n";            cout << "1. Prehistoric Dinosaur Era" << endl;            cout << "2. 500 BC" << endl;            cout << "3. Renaissance" << endl;            cout << "4. American civil war" << endl;            cin >> timePeriod;            while (timePeriod < 1 || timePeriod > 4){                cout << "1. Prehistoric Dinosaur Era" << endl;                cout << "2. 500 BC" << endl;                cout << "3. Renaissance" << endl;                cout << "4. American civil war" << endl;                cin.clear();                cin.ignore(256, '\n');                cin >> timePeriod;            }            cout << "Ok, we will send you to choice number " << timePeriod << endl << endl;            cout << "Restarting the time machine program \n";        }        else if (timeChoice == 3){            cout << "Ok, I'll choose where to send you" << endl << endl;            cout << "Travel again? Y/N : ";            cin >> travelAgain;            travelAgain = toupper(travelAgain); //convert to uppercase            if (travelAgain != 'Y'){            travelAgain = false;        }            else if (travelAgain = 'Y'){                travelAgain = true;            }            //clear the cin object before starting loop again            cin.clear();            cin.ignore(256, 'n');                    }        else {            cout << "Sorry, that's not a valid choice!" << endl << endl;            cout << "Restarting the time machine program \n";        }    }        system("PAUSE");    return 0;} 
Link to comment
https://linustechtips.com/topic/250184-c-help/
Share on other sites

Link to post
Share on other sites

Do you mean to the beginning of the While loop or the beginning of the whole program?

|| Processor: AMD Ryzen 5 2600 || RAM: 32GB (4x8GB) Corsair DDR4 Vengance (3000) || Motherboard: ASUS Prime B450-Plus || Graphics Card: Gigabyte RTX2070 || Storage: 750GB SSD (2 Drives), 3TB HDD (2 Drives) || Case: NZXT H500 || Power Supply: be quiet! Pure Power 11 600W || 

 

Link to comment
https://linustechtips.com/topic/250184-c-help/#findComment-3430379
Share on other sites

Link to post
Share on other sites

If you want to go through the whole program again you can just call the main function on itself (assuming of course you don't want to retain any data in the variables).

|| Processor: AMD Ryzen 5 2600 || RAM: 32GB (4x8GB) Corsair DDR4 Vengance (3000) || Motherboard: ASUS Prime B450-Plus || Graphics Card: Gigabyte RTX2070 || Storage: 750GB SSD (2 Drives), 3TB HDD (2 Drives) || Case: NZXT H500 || Power Supply: be quiet! Pure Power 11 600W || 

 

Link to comment
https://linustechtips.com/topic/250184-c-help/#findComment-3430384
Share on other sites

Link to post
Share on other sites

there are a number of ways to do it. 

 

one way (probably the less conventional method) is to make a looping function that is activated once 'y' is pressed. i'll edit this post with code to show you what I mean. give me a minute

Thank you

 

Do you mean to the beginning of the While loop or the beginning of the whole program?

In the grade criteria it says, "Entire program loops. 0/10 points."

I assume she just wants us to loop it from the numbers, not the name entering part because it is probably the same user so there would be no need to ask for the name again.

 

If you want to go through the whole program again you can just call the main function on itself (assuming of course you don't want to retain any data in the variables).

No, I want the variables to reset after hitting "y" to start over

Link to comment
https://linustechtips.com/topic/250184-c-help/#findComment-3430430
Share on other sites

Link to post
Share on other sites

well, I found your issue.

 

travelAgain == true and you are asking the user to make travelAgain true when it is already true in the loop, so it won't restart. I tried a few things, but I can't seem to get it to re-loop. i suspect it's something really small we're both overlooking, and it's driving me insane lol

I'm still tryna figure it out, it probably something is small. Once I find out what it is I'll be good to go because I can just copy and paste it for option 1 and option 2, but until then I am just giving myself a headache lol.

 

edit: I can't make travelAgain false because the program won't run at all.

Link to comment
https://linustechtips.com/topic/250184-c-help/#findComment-3430569
Share on other sites

Link to post
Share on other sites

Yeah, I kinda get it.

 

yeah, you can't. I suppose you could make two variables.

 

can you use functions? You can make the stuff in the main method a function and run it. then, when another boolean is true, the function will run again. get what i'm saying?

Your problem is in this line:

cin >> travelAgain;

travelAgain is a boolean, you need to create a new char and put the input in that variable and then change the if statements to the char.

 

Also, i'm not really sure what the cin ignore and clear things do but it wouldn't finish compiling when i had them in so i removed them and it worked. Maybe someone else can explain that.

 

Full code:

 

#include <iostream>#include <string>#include <iomanip>using namespace std;int main(){    // 11/12/2014    bool travelAgain = true;    string name = "";    int month = 0;    int day = 0;    int year = 0;    int timeChoice = 0;    int timePeriod = 0;	char t;        cout << "Welcome to the Time Machine" << endl;    cout << "What is your name? ";    getline(cin, name);    cout << "Hello " << name << ". ";        while (travelAgain){        cout << "Select an option to get started:" << endl;        cout << "1. Enter a Specific Date" << endl;        cout << "2. Select a Time Period" << endl;        cout << "3. Wildcard - I'm feeling lucky\n" << endl;        cin >> timeChoice;        while (timeChoice < 1 || timeChoice > 3){            cout << "Sorry, must be a 1, 2 or 3.\n";            cout << "1. Enter a Specific Date" << endl;            cout << "2. Select a Time Period" << endl;            cout << "3. Wildcard - I'm feeling lucky\n" << endl;            cin.clear();            cin.ignore(256, '\n');            cin >> timeChoice;        }        if (timeChoice == 1){            cout << "What is the month ? (enter 1 - 12) ";            cin >> month;            while (month < 1 || month > 12){                cout << "Must be between 1 - 12" << endl;                cout << "What is the month ? (enter 1 - 12)\n ";                cin.clear();                cin.ignore(256, '\n');                cin >> month;            }            cout << "What is the day? (Enter 1-31) ";            cin >> day;            while (day < 1 || day > 31){                cout << "Must be between 1 - 31" << endl;                cout << "What is the day? (Enter 1-31)\n ";                cin.clear();                cin.ignore(256, '\n');                cin >> day;            }            cout << "What is the year? ";            cin >> year;            while (year <= 0){                cout << "Year must be a number: " << endl;                cin.clear();                cin.ignore(256, '\n');                cin >> year;            }            cout << "Ok, so we will send you to " << month << "/" << day << "/" << year << endl << endl;            cout << "Restarting the time machine program \n";        }        else if (timeChoice == 2){            cout << "Choose from one of these time periods.\n";            cout << "1. Prehistoric Dinosaur Era" << endl;            cout << "2. 500 BC" << endl;            cout << "3. Renaissance" << endl;            cout << "4. American civil war" << endl;            cin >> timePeriod;            while (timePeriod < 1 || timePeriod > 4){                cout << "1. Prehistoric Dinosaur Era" << endl;                cout << "2. 500 BC" << endl;                cout << "3. Renaissance" << endl;                cout << "4. American civil war" << endl;                cin.clear();                cin.ignore(256, '\n');                cin >> timePeriod;            }            cout << "Ok, we will send you to choice number " << timePeriod << endl << endl;            cout << "Restarting the time machine program \n";        }        else if (timeChoice == 3){            cout << "Ok, I'll choose where to send you" << endl << endl;            cout << "Travel again? Y/N : ";            cin >> t;            t = toupper(t); //convert to uppercase            if (t != 'Y'){            travelAgain = false;        }            else if (t = 'Y'){                travelAgain = true;            }                    }        else {            cout << "Sorry, that's not a valid choice!" << endl << endl;            cout << "Restarting the time machine program \n";        }    }        system("PAUSE");    return 0;}

Link to comment
https://linustechtips.com/topic/250184-c-help/#findComment-3431135
Share on other sites

Link to post
Share on other sites

You guys are right, it works now. I just applied it to options 1 & 2 now. Thanks a lot guys, hopefully it won't be long until I can do this without assistance.

 

Also, i'm not really sure what the cin ignore and clear things do but it wouldn't finish compiling when i had them in so i removed them and it worked. Maybe someone else can explain that.

cin.clear and cin.ignore clears the console in incase the user types random shit. the 256 means it erased up to 256 characters that they type in.

 

edit: Just wanted to say thanks again guys.

Link to comment
https://linustechtips.com/topic/250184-c-help/#findComment-3432403
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×