I'm in a beginning C++ class and I am having a little problem with redirection.I'm trying to find a way to redirect program logic back to a certain part in the code without using the evil GOTO.
In particular, it's in the 2nd half of my console program that deals with the 12 months. The case statements work fine as well as the default. But, I want to ask the user to "Please try again" when they enter a number out of range and to redirect them to the "cout <<"Pick a number between 1-12: ";
Would there be a cheap alternative to using goto? Below is my code:
#include <iostream>
usingnamespace std;
int main()
{
int num1, num2, monthNum;
cout << "This program will determine which number is larger than or smaller than between the two numbers you choose. \n\n";
cout << "Pick two numbers between 0-100: ";
cin >> num1 >> num2;
if (num1 > num2)
{
cout <<num1<< " is larger than "<<num2<< endl;
cout <<"Your first variable is the biggest.\n\n\n" ;
}
elseif (num1 < num2)
{
cout <<num1<< " is less than "<<num2<< endl;
cout <<"Your second variable is the biggest.\n\n\n" ;
}
else
{
cout <<num1<< " is equal to "<<num2<< "\n\n";
}
system ("PAUSE");
cout <<"\n\n\n";
cout <<"This portion of the program determine the month based on your numerical input.\n\n\n";
cout <<"Please pick a number between 1-12: ";
cin >> monthNum;
switch(monthNum)
{
case 1:
cout <<"January" <<endl;
break;
case 2:
cout <<"Feburary" <<endl;
break;
case 3:
cout <<"March" <<endl;
break;
case 4:
cout <<"April" <<endl;
break;
case 5:
cout <<"May" <<endl;
break;
case 6:
cout <<"June" <<endl;
break;
case 7:
cout <<"July" <<endl;
break;
case 8:
cout <<"August" <<endl;
break;
case 9:
cout <<"September" <<endl;
break;
case 10:
cout <<"October" <<endl;
break;
case 11:
cout <<"November" <<endl;
break;
case 12:
cout <<"December" <<endl;
break;
default:
cout <<"Invalid number" <<endl;
//redirection code goes here to restart month user input.
}
cin.get();
cin.get();
return 0;
}
I'm trying to understand how to form the while loop. I added "string response = "y" as a variable in hopes that when the user chooses "y", it will redirect the program to the beginning of the code for them to re-choose a number between 1-12.
However, I'm not sure how to formulate what I need in order for it to redirect properly.
1 2 3 4 5 6 7
default:
cout <<"You did not pick a number between 1-12."<<endl;
cout <<"Do you want to try again? (y/n) ";
cin >> response;
while (response == "y");
// do something to redirect the code to allow user to to re-enter number 1-12
#include <iostream>
usingnamespace std;
int main()
{
int my_int1=0,my_int2=0,my_int3=0;
////////////////////////////////////////////////////////////////////////////////
//in a while loop the condition is checked at the beginning of the loop
//while loop example (THIS LOOP WILL NOT RUN!)
my_int1=-2;
while (my_int1>=0) //this loop won't run as (my_int1>=0) is false!
{
cout << "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl;
cout << "inside while loop!" << endl;
cout << "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n" << endl;
cout << "you entered: " << my_int1 << endl;
cout << "enter an integer (negative to quit): ";
cin >> my_int1;
cout << endl;
}
////////////////////////////////////////////////////////////////////////////////
//in a do-while loop the condition is checked at the end of the loop
//do-while loop example (this loop will run until you give negative input)
my_int1=-5;
do
{
cout << "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl;
cout << "inside do-while loop!" << endl;
cout << "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n" << endl;
cout << "enter an integer (negative to quit): ";
cin >> my_int1;
cout << "you entered: " << my_int1 << endl;
cout << endl;
} while (my_int1>=0);
////////////////////////////////////////////////////////////////////////////////
//sometimes, when you have more than one conditions and all must be
//true in order to continue looping, using a while(true) statement
//and manually checking the conditions in the body of the loop is better
//while (true) example
while (true) //this will loop forever unless you break
{
cout << "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=" << endl;
cout << "inside while(true) loop!" << endl;
cout << "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n" << endl;
cout << "enter int1 (negative to quit): ";
cin>>my_int1;
if (my_int1<0) break;
cout << "enter int2 (negative to quit): ";
cin>>my_int2;
if (my_int2<0) break;
cout << "enter int3 (negative to quit): ";
cin>>my_int3;
if (my_int3<0) break;
cout << "you entered: ";
cout << my_int1 << ' ' << my_int2 << ' '<<my_int3 << endl;
cout << endl;
}
////////////////////////////////////////////////////////////////////////////////
system("pause");
return 0;
}
And I want you to know that it's a very good thing that you avoid using goto...
Look what might happen to you if you used it... http://xkcd.com/292/ xD
I think I am closer to what I am needing. However, something still isn't quite right with my do-while loop. When I compile it, I get one syntax error somewhere in the bottom 3 cin.get(). When I comment the bottom 3 cin.get(), it gives me another error saying the syntax for the return 0 is wrong. What could be wrong with my do-while loop? Any help in clarifying my error to me would be greatly appreciated. Thanks again guys!
cout <<"This portion of the program determine the month based on your numerical input.\n\n\n";
cout <<"Please pick a number between 1-12: ";
cin >> monthNum;
do
{
switch(monthNum)
{
case 1:
cout <<"January" <<endl;
break;
case 2:
cout <<"Feburary" <<endl;
break;
case 3:
cout <<"March" <<endl;
break;
case 4:
cout <<"April" <<endl;
break;
case 5:
cout <<"May" <<endl;
break;
case 6:
cout <<"June" <<endl;
break;
case 7:
cout <<"July" <<endl;
break;
case 8:
cout <<"August" <<endl;
break;
case 9:
cout <<"September" <<endl;
break;
case 10:
cout <<"October" <<endl;
break;
case 11:
cout <<"November" <<endl;
break;
case 12:
cout <<"December" <<endl;
break;
default:
cout <<"You did not pick a number between 1-12." <<endl;
cout <<"Please try again and choose a number between 1-12: ";
cin >> monthNum;
} while (monthNum > 12);
}
//cin.get();
//cin.get();
//cin.get();
return 0;
}
When you use std::cin to get user input, a '\n' character is left in the stream. You need to ignore that character. Take one of those std::cin.get()'s and place it after every std::cin statement.
I used your suggestion and got the response I was looking for however, the month associated with it's number executes but it is a repeating. How do I prevent this infinite loop?
Here is the code in it's entirety. The 2nd half of the code is where my problem lies.
#include <iostream>
usingnamespace std;
int main()
{
int num1, num2;
int monthNum = 0;
cout << "This program will determine which number is larger than or smaller than between
the two numbers you choose. \n\n";
cout << "Pick two numbers between 0-100: ";
cin >> num1 >> num2;
cin.get();
if (num1 > num2)
{
cout <<num1<< " is larger than "<<num2<< endl;
cout <<"Your first variable is the biggest." <<endl;
}
elseif (num1 < num2)
{
cout <<num1<< " is less than "<<num2<< endl;
cout <<"Your second variable is the biggest." <<endl;
}
else
{
cout <<num1<< " is equal to "<<num2<< endl;
}
system ("PAUSE");
cout <<"\n\n\n";
cout <<"This portion of the program determine the month based on your numerical
input." <<endl;
cout <<"Please pick a number between 1-12: ";
cin >> monthNum;
cin.get();
do
{
switch(monthNum)
{
case 1:
cout <<"January" <<endl;
break;
case 2:
cout <<"Feburary" <<endl;
break;
case 3:
cout <<"March" <<endl;
break;
case 4:
cout <<"April" <<endl;
break;
case 5:
cout <<"May" <<endl;
break;
case 6:
cout <<"June" <<endl;
break;
case 7:
cout <<"July" <<endl;
break;
case 8:
cout <<"August" <<endl;
break;
case 9:
cout <<"September" <<endl;
break;
case 10:
cout <<"October" <<endl;
break;
case 11:
cout <<"November" <<endl;
break;
case 12:
cout <<"December" <<endl;
break;
default:
cout <<"You did not pick a number between 1-12." <<endl;
cout <<"Please try again and choose a number between 1-12: ";
cin >> monthNum;
cin.get();
}
} while (monthNum > 0);
cin.get();
return 0;
}
#include <iostream>
usingnamespace std;
int main()
{
int num1, num2;
int monthNum = 0;
cout << "This program will determine which number is larger than "
<<"or smaller than betweenthe two numbers you choose. \n\n";
cout << "Pick two numbers between 0-100: ";
cin >> num1 >> num2;
cin.get();
if (num1 > num2)
{
cout <<num1<< " is larger than "<<num2<< endl;
cout <<"Your first variable is the biggest." <<endl;
}
elseif (num1 < num2)
{
cout <<num1<< " is less than "<<num2<< endl;
cout <<"Your second variable is the biggest." <<endl;
}
else
{
cout <<num1<< " is equal to "<<num2<< endl;
}
system ("PAUSE");
cout <<"\n\n\n";
cout <<"This portion of the program determine the month based on "
<<"your numerical input." <<endl;
do{
cout <<"Please pick a number between 1-12: ";
cin >> monthNum;
cin.get();
} while (monthNum<1 || monthNum>12);
switch(monthNum)
{
case 1:
cout <<"January" <<endl;
break;
case 2:
cout <<"Feburary" <<endl;
break;
case 3:
cout <<"March" <<endl;
break;
case 4:
cout <<"April" <<endl;
break;
case 5:
cout <<"May" <<endl;
break;
case 6:
cout <<"June" <<endl;
break;
case 7:
cout <<"July" <<endl;
break;
case 8:
cout <<"August" <<endl;
break;
case 9:
cout <<"September" <<endl;
break;
case 10:
cout <<"October" <<endl;
break;
case 11:
cout <<"November" <<endl;
break;
case 12:
cout <<"December" <<endl;
break;
}
cin.get();
return 0;
}
You got caught in the infinite loop because a negative value was never entered :) It looks like you are trying to do a menu style code, and in that case, your exit from the while loop would be one of your switch options.
/**
*Program name: cut.n.paste.me.baby.cpp
*Written by: steven.riedel@gmail.com
*Discussion: simple do while switch "menu" style
*/
#include <iostream>
usingnamespace std;
int main( void ) {
int option = 0;
// our control variable
do {
cout << "\nPlease enter a number 1 or 2. Use 3 to exit. : ";
cin >> option;
// We'll stay in this loop, always asking for numbers
// until 3 is entered.
switch ( option ) {
case 1:
cout << "Oh the number 1 is lovely." << endl;
break;
case 2:
cout << "Ohh number 2! Even better! One for me one for you! " << endl;
break;
case 3:
// option now equals 3, so when it checks the while condition, you'll now be
// able to exit the loop
cout << "Have a great day! George Jetson would be proud! " << endl;
break;
default:
// option still does not equal 3, so the while loop condition will still be TRUE and send you back to the do {
cout << "Wrong Option, try again." << endl;
}
} while ( option != 3 ); // read as "do this block of code while option is not equal to 3."
return 0;
}
If you wanted to do it that way, then you would ask for a specific constant to exit the loop with. "Please enter a number between 1-12 ( use 0 to exit ):"
Then you would have a case 0: telling the user they are getting out of the program, and default will catch all non 0-12 cases.