Issue with switch and do-while

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 626 Views - Last Post: 17 February 2013 - 07:11 AM Rate Topic: -----

#1 popa  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 15-February 13

Issue with switch and do-while

Posted 15 February 2013 - 08:45 PM

My code needs to output a char for the user's integer input. Basically, when the user types 1, the output will be one -- only in the range of 0 to 9 though. Here's what I have thus far, but for some reason when I type any char such as "zero, one, two, ..." it goes on an infinite loop, basically breaking the logic of the program. In keeping within the integrity of learning and the forum's motto of "Don't tell us to give you code, we won't do your homework, etc.", can anyone point me in the direction on how I could solve this on my own? So far I've tried adding a return 1 within the do branch, as well as at the end but I know that probably won't do anything useful for my problem.



#include<iostream>

using namespace std;

int main()
{
	int grade;
	do
	{	
		cout << "Please input a single-digit positive integer.\n";
		cout << "Input any negative number to quit the program.\n";
		cin >> grade;
		switch (grade)
		{
	case 0:
		cout << "Your number is zero.\n";
		break;
	case 1:
		cout << "Your number is one.\n";
		break;
	case 2:
		cout << "Your number is two.\n";
		break;
	case 3:
		cout << "Your number is three.\n";
		break;
	case 4:
		cout << "Your number is four.\n";
		break;
	case 5:
		cout << "Your number is five.\n";
		break;
	case 6:
		cout << "Your number is six.\n";
		break;
	case 7:
		cout << "Your number is seven.\n";
		break;
	case 8:
		cout << "Your number is eight.\n";
		break;
	case 9:
		cout << "Your number is nine.\n";
		break;
	default:
		cout << "";
		
	}

	//While statement to end the program if user inputs a number less than 0.
	}while(grade >= 0);
	cout << "Your number is negative. Now exit.";
	//End while statment.

	return 0;







Is This A Good Question/Topic? 0
  • +

Replies To: Issue with switch and do-while

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: Issue with switch and do-while

Posted 15 February 2013 - 09:13 PM

Your code is expecting an integer, if you input another character the cin stream will fail.

12	        cin >> grade;




You should be able to check the stream state with cin.fail().

Then you can clear the error state, and remove other characters in the stream with sync.

    if(cin.fail())
    {
      cout << "cin stream failed!" << endl;
      cin.clear();
      cin.sync();
      continue;
    }


Was This Post Helpful? 0
  • +
  • -

#3 Zereo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 108
  • Joined: 08-November 12

Re: Issue with switch and do-while

Posted 15 February 2013 - 09:50 PM

I am not really sure what you are having a problem with?

The only mistake I see is your missing a closing bracket ( } ) for your main block, but that is most likely a copy and paste error.

Other then that I have ran your program and it runs as expected. For example if I input '1' I get a output of "One", input '2' I get "Two", and when I input a negative number it exits.

I see what you mean by when you enter "one" (which is not a char its a string) it enters a infinite loop. But I'm not clear on why you would be inputting a string when you are suppose to input a integer.

What you can do is use default in the switch statement to handle errors of this type. For example you could do something like this.

default:
    cout << "You did not enter a number 0-9, or you did not enter a number. Please try again.";
    return 0;


What that does is if the user enters any number other then 0-9 it exits the program with a 0 code. This works even if the user enters a char lie 'a' or a string like 'one'. Hope this helps a bit and obviously you can rework this to fit your needs better.
Was This Post Helpful? 1
  • +
  • -

#4 pulp_fiction  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 36
  • Joined: 03-February 13

Re: Issue with switch and do-while

Posted 15 February 2013 - 10:20 PM

You have declared grade as an integer .Now when you input any string in that,how can you expect it to work?Moreover, <case x:> has x selected from only the set of {integers only} or {characters only}.
Maybe you could create a structure,
struct sample
{
   int grade;
   char str[20];
}


Now you could create 2 separate switch cases ,one for grade and another one for str[] to get your job done.
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8013
  • View blog
  • Posts: 13,719
  • Joined: 19-March 11

Re: Issue with switch and do-while

Posted 15 February 2013 - 11:25 PM

I think the question has been answered pretty clearly: you need to remember that ints and chars are different animals.

Let me offer another suggestion which you might want to consider, which is to replace the switch with an array. In fact, this is likely to be a good move whenever you can do it, because it eliminates hard-coded data and replaces it with actual data. (for example, this would allow you to read in values from a file, and map from digits to any language for which you have the right set of strings, without changing any executable code - this is convenient)
Was This Post Helpful? 0
  • +
  • -

#6 popa  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 15-February 13

Re: Issue with switch and do-while

Posted 16 February 2013 - 10:29 AM

View Postjon.kiparsky, on 15 February 2013 - 11:25 PM, said:

I think the question has been answered pretty clearly: you need to remember that ints and chars are different animals.

Let me offer another suggestion which you might want to consider, which is to replace the switch with an array. In fact, this is likely to be a good move whenever you can do it, because it eliminates hard-coded data and replaces it with actual data. (for example, this would allow you to read in values from a file, and map from digits to any language for which you have the right set of strings, without changing any executable code - this is convenient)


Unfortunately, the assignment calls for demonstrating the usage of the switch-statement and while-statement. I know there's other ways to go about this but this is what I'm limited to.

Here's an update of the code with the default statement, still exhibiting the error I described above. Keep in mind, the program has to be relatively basic.

#include<iostream>

using namespace std;

int main()
{
	int grade;
	do
	{	
		cout << "Please input a single-digit positive integer.\n";
		cout << "Input any negative number to quit the program.\n";
		cin >> grade;
		switch (grade)
		{
	case 0:
		cout << "Your number is zero.\n";
		break;
	case 1:
		cout << "Your number is one.\n";
		break;
	case 2:
		cout << "Your number is two.\n";
		break;
	case 3:
		cout << "Your number is three.\n";
		break;
	case 4:
		cout << "Your number is four.\n";
		break;
	case 5:
		cout << "Your number is five.\n";
		break;
	case 6:
		cout << "Your number is six.\n";
		break;
	case 7:
		cout << "Your number is seven.\n";
		break;
	case 8:
		cout << "Your number is eight.\n";
		break;
	case 9:
		cout << "Your number is nine.\n";
		break;
	default:
		cout << "You didn't enter a number 0-9, or you did not enter a number.\n" 
			  << "Please try again.";
		return 0;
		}

	}
	//While statement to end the program if user inputs a number less than 0.
	while(grade >= 0);
	cout << "Your number is negative. Now exit.";
	//End while statment.

	return 0;
}





I've also been given this as a means to exit the program, I think I've implemented it decently but I'm sure there's something missing as I'm only an amateur to C++ and programming in general.

/*
 * while_loop_example.cpp
 *
 */

#include <iostream>
using namespace std;

int main()
{
	int num=0;
	while (num >=0)
	{
		cout << "Please input a single-digit positive integer. \n"
				<<"Input any negative number to quit the program.\n" ;

		cin >> num;

		if ( num < 0 )
		{
			cout <<"Your number is negative. Now exit.\n";
			return 0;
		}

		cout << "Your number is "<< num << "\n";
	}

	return 0;

}







Here's the error when you type anything that's considered a char (one, two, three, etc.).
Posted Image
Was This Post Helpful? 0
  • +
  • -

#7 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 841
  • View blog
  • Posts: 2,472
  • Joined: 20-March 10

Re: Issue with switch and do-while

Posted 16 February 2013 - 10:57 AM

Hi after your return 0; inside the

Switch statement there should be a break;

basically switch statements go

switch(something)

{

case 1:

do something
break;

.....

case whatever:
do something
break;


break;
}/// break to end switch statement...

Snoopy.

This post has been edited by snoopy11: 16 February 2013 - 10:57 AM

Was This Post Helpful? 0
  • +
  • -

#8 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: Issue with switch and do-while

Posted 16 February 2013 - 01:46 PM

Would you be allowed to use a character for the input?

11	    char num = '0';



.

This post has been edited by #define: 16 February 2013 - 01:47 PM

Was This Post Helpful? 0
  • +
  • -

#9 popa  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 15-February 13

Re: Issue with switch and do-while

Posted 16 February 2013 - 05:15 PM

View Postsnoopy11, on 16 February 2013 - 10:57 AM, said:

Hi after your return 0; inside the

Switch statement there should be a break;

basically switch statements go

switch(something)

{

case 1:

do something
break;

.....

case whatever:
do something
break;


break;
}/// break to end switch statement...

Snoopy.


I added break; after the return 0; within the do {}, and I tried doing it before the return 0; as well. No luck, still the same error.
Was This Post Helpful? 0
  • +
  • -

#10 Zereo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 108
  • Joined: 08-November 12

Re: Issue with switch and do-while

Posted 16 February 2013 - 08:19 PM

I am still not quite clear why you are doing through this trouble when the user is suppose to enter a integer? Is your professor asking you to handle errors for when the user enters a string or a char instead of a integer?

If that is the case why don't you use what #define said in his reply? All you need to do is insert this right after your cin statement.

if(cin.fail())
{
  // Enter whatever you want to tell the user here
  cin.clear();
  cin.sync();
  continue;
}


That should take care of the infinite loop you will get if the stream fails.
Was This Post Helpful? 0
  • +
  • -

#11 popa  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 15-February 13

Re: Issue with switch and do-while

Posted 16 February 2013 - 09:17 PM

Like this? I just updated the code to reflect the suggested change, however, it's still displaying the same error. I'm stuck in between a rock and a hard place right now. I have a pretty good feeling my professor doesn't want the program to operate in that fashion.

#include<iostream>

using namespace std;

int main()
{
	int grade;
	do
	{	
		cout << "Please input a single-digit positive integer.\n";
		cout << "Input any negative number to quit the program.\n";
		cin >> grade;
		if(cin.fail())
		{
			cout << "You can only enter an integer, ex. 0-9, not one-nine.\n";
			cin.clear();
			cin.sync();
			continue;
		}
		switch (grade)
		{
	case 0:
		cout << "Your number is zero.\n";
		break;
	case 1:
		cout << "Your number is one.\n";
		break;
	case 2:
		cout << "Your number is two.\n";
		break;
	case 3:
		cout << "Your number is three.\n";
		break;
	case 4:
		cout << "Your number is four.\n";
		break;
	case 5:
		cout << "Your number is five.\n";
		break;
	case 6:
		cout << "Your number is six.\n";
		break;
	case 7:
		cout << "Your number is seven.\n";
		break;
	case 8:
		cout << "Your number is eight.\n";
		break;
	case 9:
		cout << "Your number is nine.\n";
		break;
	default:
		cout << "You didn't enter a number 0-9, or you did not enter a number.\n" 
			  << "Please try again.";
 		}

	}
		
	//While statement to end the program if user inputs a number less than 0.
	while(grade >= 0);
	cout << "Your number is negative. Now exit.";
	//End while statment.
	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#12 popa  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 15-February 13

Re: Issue with switch and do-while

Posted 16 February 2013 - 09:27 PM

Now I know this isn't possible but I'm sure the idea is relevant, could I do something along the lines of:

while(grade >= 0 && grade == 'char');
cout << "Your number is negative. Now exit.";
//End while statement.
return 0;
}



Where grade == 'char', when compiling there's an error, however, what I'm trying to do is display the error message and exit the program if the user enters anything that's a negative integer or a character. I'm sure && grade == 'char' is bad logic and incorrect syntax though.
Was This Post Helpful? 0
  • +
  • -

#13 Zereo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 44
  • View blog
  • Posts: 108
  • Joined: 08-November 12

Re: Issue with switch and do-while

Posted 16 February 2013 - 09:41 PM

I am not sure how you are still getting a infinite loop with the code you just posted... I have just ran the code you posted and it worked completely fine for me I am using Codeblock with GNU GCC Compiler and also tested on VS2012. I inputted both strings like "hello" and char's like 'a' and it didn't infinite loop. So I am not sure how I can help sorry.
Was This Post Helpful? 0
  • +
  • -

#14 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1432
  • View blog
  • Posts: 4,968
  • Joined: 19-February 09

Re: Issue with switch and do-while

Posted 16 February 2013 - 10:08 PM

There are three states of input :

the input can be
1) a positive number
2) a negative number, which is taken as exit
3) another character, usually taken as an error

When an error occurs usually it is just reported and the program continues. The user did not signal to exit so it maybe an accident.

If grade is an int entering another character puts the stream into an error state, which continues until it is cleared. If the stream fails grade will not be updated.

So grade == 'char' is equivalent to cin.fail(),

and grade != 'char' is equivalent to cin.good().


So your code would be :-

do
{
  //...

} while( grade >= 0 && cin.good() );


Was This Post Helpful? 1
  • +
  • -

#15 popa  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 53
  • Joined: 15-February 13

Re: Issue with switch and do-while

Posted 16 February 2013 - 10:51 PM

View PostZereo, on 16 February 2013 - 09:41 PM, said:

I am not sure how you are still getting a infinite loop with the code you just posted... I have just ran the code you posted and it worked completely fine for me I am using Codeblock with GNU GCC Compiler and also tested on VS2012. I inputted both strings like "hello" and char's like 'a' and it didn't infinite loop. So I am not sure how I can help sorry.


If it helps any, I'm using G++ to compile. I wouldn't think that would do much of anything though, I could be wrong. I'm probably wrong.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2