Switch Cases

Switch Cases

Post by mad-jackass on Tue Sep 16, 2008 4:18 pm
([msg=11855]see Switch Cases[/msg])

Hey I just started learning c++ and am onto the page on switch cases on cprogramming.com but cant get it to work.

Can somebody please expain where I've gone wrong here:

#include <iostream>
using namespace std;
int main ()
{
int x;
cout<< "Pick a number: 1 or 2: ";
cin>> x;
switch ( x ) {
case 1:
cout<< "You chose 1, correct!";
break;
case 2:
cout<< "You chose 2, fail!";
break;
default:
cout<< "doesnt work\n";
break;
}
cin.get();
}
mad-jackass
New User
New User
 
Posts: 3
Joined: Fri Jul 04, 2008 10:23 am
Blog: View Blog (0)


Re: Switch Cases

Post by BhaaL on Wed Sep 17, 2008 11:08 am
([msg=11910]see Re: Switch Cases[/msg])

Nothing wrong there, what does it do? Or better, what does it *not* do?
BhaaL
Poster
Poster
 
Posts: 270
Joined: Sun Apr 13, 2008 11:16 am
Blog: View Blog (0)


Re: Switch Cases

Post by mad-jackass on Wed Sep 17, 2008 5:02 pm
([msg=11937]see Re: Switch Cases[/msg])

Well I'm using bloodshed dev c++ and what ever I enter cmd just closes instantly.

Its not meant to do anything usefull really, I was just trying to learn how to use swich cases.
mad-jackass
New User
New User
 
Posts: 3
Joined: Fri Jul 04, 2008 10:23 am
Blog: View Blog (0)


Re: Switch Cases

Post by 193zaitsev on Thu Sep 18, 2008 1:38 pm
([msg=11979]see Re: Switch Cases[/msg])

That code is indeed correct. Have you successfully compiled and ran other programs with DevC++?
193zaitsev
New User
New User
 
Posts: 36
Joined: Wed May 21, 2008 10:28 pm
Location: USA
Blog: View Blog (0)


Re: Switch Cases

Post by mad-jackass on Thu Sep 18, 2008 4:05 pm
([msg=11986]see Re: Switch Cases[/msg])

Yes, I have compiled and ran many other programs with dev. Its strange I've checked the code multiple times and can't se why it won't work.
mad-jackass
New User
New User
 
Posts: 3
Joined: Fri Jul 04, 2008 10:23 am
Blog: View Blog (0)


Re: Switch Cases

Post by Kurushimi on Thu Oct 30, 2008 3:23 pm
([msg=14286]see Re: Switch Cases[/msg])

You need to add "system("pause")" to the end of your code. This is sends a command to the command prompt to pause and wait for keyboard input. Some compilers do this automatically. DevC++ does not.
Kurushimi
New User
New User
 
Posts: 17
Joined: Thu Oct 23, 2008 6:44 pm
Blog: View Blog (0)


Re: Switch Cases

Post by Zelth on Thu Oct 30, 2008 8:11 pm
([msg=14290]see Re: Switch Cases[/msg])

Cool, system ("pause") works perfectly. I learned something new today :D (started learning C++ for a week) I couldn't find any errors in the code either... but of course because it wasn't an error D:
I thought that cin.get() would stop the cmd prompt from closing by itself >.> I think I was wrong....
Z~Epsilon
Newbie Programmer
Learned: HTML, XHTML, CSS
Learning: PHP, C++
Zelth
Poster
Poster
 
Posts: 149
Joined: Mon Sep 08, 2008 7:45 pm
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests