#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int choice;
cout << "Hello! What would you like to do today?" << endl;
// Line Break
cout << endl;
do
{
// Get the users choice
cout << "1. Count" << endl;
cout << "2. Draw A Rectangle" << endl;
cout << "3. Factorial" << endl;
cout << "4. Exit The Program" << endl;
cout << "Please enter your choice (1-4): ";
// Get the selection From The User
cin >> choice;
switch (choice)
{
case '1': cout << "Count it is!" << endl;
case '2': cout << "Draw A Rectangle it is!" << endl;
case '3': cout << "Factorial it is!" << endl;
case '4': cout << "Exit The Program it is!" << endl;
}
} while (choice <= 4 || choice >= 1);
return 0;
}
Switch inside do while loop not Working
Page 1 of 17 Replies - 717 Views - Last Post: 25 October 2012 - 09:14 AM
#1
Switch inside do while loop not Working
Posted 24 October 2012 - 04:22 PM
I can't get the input from the user to output what they picked in the switch. What am I doing wrong? Any help would be greatly appreciated!
Replies To: Switch inside do while loop not Working
#2
Re: Switch inside do while loop not Working
Posted 24 October 2012 - 04:44 PM
choice is an int and '1' , '2', etc are characters, so they'll never match.
#3
Re: Switch inside do while loop not Working
Posted 25 October 2012 - 05:55 AM
You also need break in a switch. I'd use a done flag for something like this:
bool done = false;
cout << "Hello! What would you like to do today?" << endl
<< endl;
while(!done) {
// ...
case '4':
cout << "Exit The Program it is!" << endl;
done = true;
break;
default:
cout << "I don't know what '" << choice << "' means" << endl;
}
}
#4
Re: Switch inside do while loop not Working
Posted 25 October 2012 - 06:57 AM
Great! That was a lot of help! Another questions I have is how can I have a counter when using a while loop? For instance if the user types an of 6 I need to the program to display 1, 2, 3, 4, 5, 6
So I would get the user input
How would I go about that? Have a counter at the end?
So I would get the user input
cin >> num1;
while (num1)
{
How would I go about that? Have a counter at the end?
#5
Re: Switch inside do while loop not Working
Posted 25 October 2012 - 08:02 AM
Perhaps:
I'd prefer a for loop for this, however.
int num2 = 1;
while(num2<=num1) { // you'll want to increment num2 inside here
I'd prefer a for loop for this, however.
#6
Re: Switch inside do while loop not Working
Posted 25 October 2012 - 08:05 AM
#7
Re: Switch inside do while loop not Working
Posted 25 October 2012 - 08:24 AM
How does this look for a counter?
What's the best way to do defensive programming in case a user inputs a number less than 2 and more than 100? Is there a way to add an else onto the while?
case '1': cout << "Please enter a number to count to (2-100): ";;
cin >> N;
while(N < 2 || N > 200)
cin >> N;
while(cnt <= N)
{
cout << cnt << " ";
if(cnt % 8 == 0)
cout << endl;
++cnt;
}
cout << endl;
What's the best way to do defensive programming in case a user inputs a number less than 2 and more than 100? Is there a way to add an else onto the while?
#8
Re: Switch inside do while loop not Working
Posted 25 October 2012 - 09:14 AM
jbatphoto, on 25 October 2012 - 11:24 AM, said:
What's the best way to do defensive programming in case a user inputs a number less than 2 and more than 100?
You're doing it, to some extent. I'd put it in a function, though. However, non ints can crash you, as the stream struggles to deal with junk in the buffer but can't get there. Folks often parse a string, because a getline doesn't do odd things.
jbatphoto, on 25 October 2012 - 11:24 AM, said:
Is there a way to add an else onto the while?
Not really following here. A while is just a loop, based on the condition. You're asking about your input loop thingy? You can do a while(true) and then escape the loop when they enter the data you want.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|