Switch in a do while loop

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 8280 Views - Last Post: 24 January 2011 - 03:59 AM Rate Topic: -----

#1 Ajani  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-January 11

Switch in a do while loop

Posted 13 January 2011 - 09:24 AM

Hi, I'm fairly new at this. Trying to learn C++ mostly on my own to start with then build on that with other languages. I would eventually like to be able to program just about anything. But can't figure out what I'm doing wrong here and have read several posts and tutorials. Can someone help me out please? The program opens and closes as it should, you can put in numbers, but the switch doesn't execute at all.

int main()
    {
          int a = 4;
          int b = 30;
          int c = 28;
          int d = 81380;
          int e = 30706;
          int f = 90482;
          int g = 10110;
          int x;
          
          cout << "Let's play guess my numbers." << endl;
          cout << "Hint: Seven very important numbers." << endl;
          
          do 
          {
               cout << "Please enter a number. Type 0 to exit." << endl;
               cin >> x;
			  switch (x)
		  {
		  if (x == a,b,c)
          cout << "You guessed an age!" << endl; 
          break;
		  if (x == d,e,f)
          cout << "You guessed a birthday!" << endl;
          break;
		  if (x == g);
          cout << "Great Day! :D/>" << endl;
		  break;
		  if (x != a,b,c,d,e,f,g)
			  cout << "Sorry please try again." << endl;
		  }      
		  }
          while (x != 0); 
		  	
		  return 0;
          }




'Nikki.exe': Loaded 'C:\Documents and Settings\Freedan\My Documents\Visual Studio 2010\Projects\Nikki\Debug\Nikki.exe', Symbols loaded.
'Nikki.exe': Loaded 'C:\WINDOWS\SYSTEM32\ntdll.dll', Cannot find or open the PDB file
'Nikki.exe': Loaded 'C:\WINDOWS\SYSTEM32\kernel32.dll', Cannot find or open the PDB file
'Nikki.exe': Loaded 'C:\WINDOWS\SYSTEM32\msvcp100d.dll', Symbols loaded.
'Nikki.exe': Loaded 'C:\WINDOWS\SYSTEM32\msvcr100d.dll', Symbols loaded.
The thread 'Win32 Thread' (0xa28) has exited with code -1073741510 (0xc000013a).
The program '[1764] Nikki.exe: Native' has exited with code -1073741510 (0xc000013a).

This post has been edited by n8wxs: 13 January 2011 - 09:31 AM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Switch in a do while loop

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Switch in a do while loop

Posted 13 January 2011 - 09:28 AM

Welcome to Dream-In-Code!

1) When posting code please use the code tags:

:code:

2) what is wrong? Be specific with your description.

This post has been edited by n8wxs: 13 January 2011 - 09:29 AM

Was This Post Helpful? 1
  • +
  • -

#3 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: Switch in a do while loop

Posted 13 January 2011 - 09:32 AM

change
  if (x == a,b,c)


to
if(x==a || x==b || x==c)


|| means or

edit:
Do the same for the other if statements. You could use if and else if, and with the last one just use else, to save on typing.

This post has been edited by simeesta: 13 January 2011 - 09:34 AM

Was This Post Helpful? 2
  • +
  • -

#4 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Switch in a do while loop

Posted 13 January 2011 - 09:38 AM

Your switch statement is malformed. switch() does not use if() statements but rather case: labels.

See Control Structures

Skip down to the "The selective structure: switch." section.

This post has been edited by n8wxs: 13 January 2011 - 09:40 AM

Was This Post Helpful? 1
  • +
  • -

#5 Ajani  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-January 11

Re: Switch in a do while loop

Posted 13 January 2011 - 10:06 AM

Ok, I read over it again, maybe the if else equivalent confused me. I'll try it with cases and see if I can get it to work. Thanks for the help everyone.
Was This Post Helpful? 0
  • +
  • -

#6 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: Switch in a do while loop

Posted 13 January 2011 - 10:17 AM

Sorry, I forgot you were you using a switch statement, so use case as n8wxs said, but if you were using if else instead, you can't have if (x == a,b,c).
Was This Post Helpful? 1
  • +
  • -

#7 Ajani  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-January 11

Re: Switch in a do while loop

Posted 13 January 2011 - 10:50 AM

Ok got the switch partialy working but it does streight to default instead of checking the other cases. What should I do now?

int main()
    {
          int a = 4;
          int b = 30;
          int c = 28;
          int d = 81380;
          int e = 30706;
          int f = 90482;
          int g = 10110;
          int x;
          
          cout << "Let's play guess my numbers." << endl;
          cout << "Hint: Seven very important numbers." << endl;
          
          do 
          {
               cout << "Please enter a number. Type 0 to exit." << endl;
               cin >> x;
			  switch (x)
		  {
			  case 1: (x == a || x == b || x == c);
          cout << "You guessed one of our ages!" << endl; 
          break;
			  case 2: (x == d || x == e || x == f );
          cout << "You guessed someone's birthday!" << endl;
          break;
			  case 3: (x == g);
          cout << "That's the day we got together! :D/>" << endl;
		  break;
			  default: (x != a || x != b || x != c || x != d || x != e || x != f || x != g);
			  cout << "Sorry please try again." << endl;
	      break;
		  }      
		  }
          while (x != 0); 
		  	
		  return 0;
          }

This post has been edited by JackOfAllTrades: 14 January 2011 - 11:46 AM
Reason for edit:: Added code tags

Was This Post Helpful? 0
  • +
  • -

#8 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Switch in a do while loop

Posted 13 January 2011 - 11:29 AM

Quote

What should I do now?


1) use the code tags when posting code:


:code:

2) look the example code in the documentation link I provided earlier.

This post has been edited by n8wxs: 13 January 2011 - 01:47 PM

Was This Post Helpful? 0
  • +
  • -

#9 simeesta  Icon User is offline

  • Deadly Ninja


Reputation: 219
  • View blog
  • Posts: 592
  • Joined: 04-August 09

Re: Switch in a do while loop

Posted 13 January 2011 - 11:34 AM

Don't forget :code:

It's used like this.
switch (x)

case 'a' : //if x == a, if no break statement is added it will execute everything includeing b and c until a break statement is reached
case 'b' : //if x==b etc...
case 'c' :
cout << "You guessed one of our ages!" << endl;
break;

case ...

//more code here
 ...



This post has been edited by simeesta: 13 January 2011 - 11:35 AM

Was This Post Helpful? 1
  • +
  • -

#10 Guest_Erik*


Reputation:

Re: Switch in a do while loop

Posted 14 January 2011 - 10:18 AM

int main()
{
int a = 4;
int b = 30;
int c = 28;
int d = 81380;
int e = 30706;
int f = 90482;
int g = 10110;
int x;

cout << "Let's play guess my numbers." << endl;
cout << "Hint: Seven very important numbers." << endl;

do 
{
    cout << "Please enter a number. Type 0 to exit." << endl;
    cin >> x;

    switch (x)
    {
    case a:
    case b:
    case c:
        cout << "You guessed one of our ages!" << endl; 
        break;
    case d:
    case e:
    case f:
        cout << "You guessed someone's birthday!" << endl;
        break;
    case g:
        cout << "That's the day we got together! " << endl;
        break;
    default:
        cout << "Sorry please try again." << endl;
        break;
    } 
}while (x != 0); 

return 0;
} 


Was This Post Helpful? -3

#11 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Switch in a do while loop

Posted 14 January 2011 - 04:01 PM

The do-while() loop used by both you and by Erik has a bug.
What happens if the user tries to exit the program by entering zero?

Do you present a invalid and confusing message to the user?
Yes you do.

How could you avoid this bug using what you have learned in this thread?
Was This Post Helpful? 0
  • +
  • -

#12 Ajani  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-January 11

Re: Switch in a do while loop

Posted 22 January 2011 - 10:31 PM

I've looked over alot of things and redone the switch several different ways and still have the same problem, it always puts out the default no matter what you type in other than 0. When you type 0, the program exits and closes the window, but I will work on a better way to do that once I find out what's wrong with the switch. Any suggestions anyone? Thanks

int main()
    {
          int a = 4;
          int b = 30;
          int c = 28;
          int d = 81380;
          int e = 30706;
          int f = 90482;
          int g = 10110;
          int x;
          
          cout << "Let's play guess my numbers." << endl;
          cout << "Hint: Seven very important numbers." << endl;
          
          do 
          {
               cout << "Please enter a number. Type 0 to exit." << endl;
               cin >> x;
			  
			   switch (x)
		  {
			  case 'a' : // x == a;
			  case 'b' : // x == b; 
			  case 'c' : // x == c;
          cout << "You guessed one of our ages!" << endl; 
          break;
			  case 'd' : // x == d;
			  case 'e' : // x == e;
			  case 'f' : // x == f;
          cout << "You guessed someone's birthday!" << endl;
          break;
			  case 'g' : // x == g;
          cout << "That's the day we got together! :D/>" << endl;
		  break;
			  default: // (x != a || x != b || x != c || x != d || x != e || x != f || x != g);
			  cout << "Sorry please try again." << endl;
	      break;
		  }      
		  }
          while (x != 0); 
		  	
		  return 0;
          }


Was This Post Helpful? 0
  • +
  • -

#13 I_cant_C  Icon User is offline

  • D.I.C Head

Reputation: 19
  • View blog
  • Posts: 67
  • Joined: 02-December 09

Re: Switch in a do while loop

Posted 22 January 2011 - 10:38 PM

In your switch statement you have '//' before the test in each case... this means they are commented out and will not be seen by the compiler, so it just jumps all the way through the switch statement without seeing the code.

Remove the //'s and it should work. :)

EDIT: typo

This post has been edited by I_cant_C: 22 January 2011 - 10:39 PM

Was This Post Helpful? 0
  • +
  • -

#14 Ajani  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 13-January 11

Re: Switch in a do while loop

Posted 22 January 2011 - 10:43 PM

Already tried that and just tried it again, same issue, I'm using visual C++ 2010 and Dev-C++.
Was This Post Helpful? 0
  • +
  • -

#15 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1623
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: Switch in a do while loop

Posted 22 January 2011 - 10:56 PM

i believe you are trying to use
'a'

as a variable. try just
a

i think simeesta may have confused you with this
case 'a' : //if x == a

when it should have been
case 'a' : //if x == 'a'

or
case a: //if x == a

if it's between quotes then it's checking the ASCII value of 'a' against x witch isn't what you want if im not mistaken.

This post has been edited by ishkabible: 22 January 2011 - 10:57 PM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2