7 Replies - 2451 Views - Last Post: 25 October 2012 - 09:14 AM Rate Topic: -----

#1 jbatphoto  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 24-October 12

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!

#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;
}


Is This A Good Question/Topic? 0
  • +

Replies To: Switch inside do while loop not Working

#2 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2990
  • Posts: 10,329
  • Joined: 08-August 08

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.
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

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;
	}
}


Was This Post Helpful? 0
  • +
  • -

#4 jbatphoto  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 24-October 12

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

cin >> num1;
while (num1)
{


How would I go about that? Have a counter at the end?
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: Switch inside do while loop not Working

Posted 25 October 2012 - 08:02 AM

Perhaps:
int num2 = 1;
while(num2<=num1) { // you'll want to increment num2 inside here



I'd prefer a for loop for this, however.
Was This Post Helpful? 0
  • +
  • -

#6 jbatphoto  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 24-October 12

Re: Switch inside do while loop not Working

Posted 25 October 2012 - 08:05 AM

View Postbaavgai, on 25 October 2012 - 08:02 AM, said:

Perhaps:
int num2 = 1;
while(num2<=num1) { // you'll want to increment num2 inside here



I'd prefer a for loop for this, however.

Yeah me too but my teacher is making us use a while. I wouldn't even be using the switch, it would be all if else
Was This Post Helpful? 0
  • +
  • -

#7 jbatphoto  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 24-October 12

Re: Switch inside do while loop not Working

Posted 25 October 2012 - 08:24 AM

How does this look for a counter?

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?
Was This Post Helpful? 0
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: Switch inside do while loop not Working

Posted 25 October 2012 - 09:14 AM

View Postjbatphoto, 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.

View Postjbatphoto, 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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1