Switch Case Help

need a little help with this switch case statement

Page 1 of 1

7 Replies - 1307 Views - Last Post: 01 December 2009 - 05:59 PM Rate Topic: -----

#1 ZOMBIE!!!  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 206
  • Joined: 28-October 09

Switch Case Help

Post icon  Posted 01 December 2009 - 03:19 PM

Hey guys, I just wanted to make a quick program that takes a letter you input and outputs a word that starts with that letter. This is my first time trying a Switch Case so Im not sure what i did wrong. Also note that this is not the whole thing, i ran a test of just the letter a and it didnt work.

Here is the code:


#include "stdafx.h"
#include <iostream>
using namespace std;


int main()
{
	char input;

	cout<< "Hello" <<endl;
	system("PAUSE");
	
	cout<<"Enter any letter in the English alphabet and I will make a word with that letter."<<endl;
	cin>> input;

	cout<<"So you entered " << input cout<<"Here is my response:"<<endl;
	system("PAUSE");

	switch (input)
	{

	case "a":
		
		cout<<"Anus";
		system("PAUSE");
		break;


	default:
		
		cout<<"Cant do that, try again";
		break;
	}
	return 0;
}




The errors i get are:
.\abc.cpp(16) : error C2146: syntax error : missing ';' before identifier 'cout'
.\abc.cpp(22) : error C2051: case expression not constant
.\abc.cpp(33) : warning C4065: switch statement contains 'default' but no 'case' labels

Thanks in advance,
ZOMBIE!!!

Is This A Good Question/Topic? 0
  • +

Replies To: Switch Case Help

#2 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: Switch Case Help

Posted 01 December 2009 - 03:23 PM

Use single quotes ' not double quotes " to surround single-characters, like this: case 'a': You might also want to convert input to either upper or lower-case so that the program doesn't have to bother matching both 'a' and 'A'.
Was This Post Helpful? 0
  • +
  • -

#3 zhanfeng  Icon User is offline

  • D.I.C Head

Reputation: 6
  • View blog
  • Posts: 75
  • Joined: 09-November 08

Re: Switch Case Help

Posted 01 December 2009 - 03:25 PM

case "a": 

"a" was enclosed with double quote and therefore, it is a string. What you want is single quote ' '. Also,
cout<<"So you entered " << input cout<<"Here is my response:"<<endl;

that is really weird to me. Why do you have two couts? One should do the trick.
Was This Post Helpful? 0
  • +
  • -

#4 ZOMBIE!!!  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 206
  • Joined: 28-October 09

Re: Switch Case Help

Posted 01 December 2009 - 03:34 PM

thanks it worked. Another question, how would i make it so after the user gets his response he can do it again without restarting the program?
Was This Post Helpful? 0
  • +
  • -

#5 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1395
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Switch Case Help

Posted 01 December 2009 - 03:43 PM

Read about loops.
Was This Post Helpful? 0
  • +
  • -

#6 ZOMBIE!!!  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 206
  • Joined: 28-October 09

Re: Switch Case Help

Posted 01 December 2009 - 05:02 PM

Thanks for all your help

here is the finished product

#include "stdafx.h"
#include <iostream>
using namespace std;


int main()
{
	char input;
	char response;
		
	
	cout<< "Hello" <<endl;
	system("PAUSE");
	
	do
{
	cout<<"Enter any letter in the English alphabet and" <<endl;
	cout<<"I will make a word beginning with that letter."<<endl;
	cout<<"And Make sure it is lowercase" <<endl;
	cin>> input;

	cout<<"So you entered " << input;
	cout<<"  Here is my response:"<<endl;
	system("PAUSE");

	system("cls");
	switch (input)
	{

	case 'a':
		
		cout<<"Anus" <<endl;
		system("PAUSE");

		break;
	
	case 'b':
		
		cout<<"Boobies :)" <<endl;
		system("PAUSE");
		break;

	case 'c':
		
		cout<<"Cornicopia" <<endl;
		system("PAUSE");
		break;

	case 'd':
		
		cout<<"Dingo (eatcha baby?)" <<endl;
		system("PAUSE");
		break;

	case 'e':
		
		cout<<"Erectile" <<endl;
		system("PAUSE");
		break;

	case 'f':
		
		cout<<"F is for fire that burns down the whole town" <<endl;
		system("PAUSE");
		break;

	case 'g':
		
		cout<<"Gigantormous" <<endl;
		system("PAUSE");
		break;

	case 'h':
		
		cout<<"Homo-erectus" <<endl;
		system("PAUSE");
		break;

	case 'i':
		
		cout<<"I" <<endl;
		system("PAUSE");
		break;

	case 'j':
		
		cout<<"JJ the Jet Plane" <<endl;
		system("PAUSE");
		break;

	case 'k':
		
		cout<<"kool" <<endl;
		system("PAUSE");
		break;

	case 'l':
		
		cout<<"lmao" <<endl;
		system("PAUSE");
		break;

	case 'm':
		
		cout<<"malleable" <<endl;
		system("PAUSE");
		break;

	case 'n':
		
		cout<<"N is for no survivors when you...." <<endl;
		system("PAUSE");
		break;

	case 'o':
		
		cout<<"Octopussy" <<endl;
		system("PAUSE");
		break;

	case 'p':
		
		cout<<"paleontologist" <<endl;
		system("PAUSE");
		break;

	case 'q':
		
		cout<<"quail" <<endl;
		system("PAUSE");
		break;

	case 'r':
		
		cout<<"roflcopter" <<endl;
		system("PAUSE");
		break;
	
	case 't':
		
		cout<<"titicaca" <<endl;
		system("PAUSE");
		break;

	case 'u':
		
		cout<<"U is for Uranium....bomb" <<endl;
		system("PAUSE");
		break;

	case 'v':
		
		cout<<"vendetta" <<endl;
		system("PAUSE");
		break;

	case 'w':
		
		cout<<"whaaaaaaaaaaaaaaaaaaale" <<endl;
		system("PAUSE");
		break;

	case 'x':
		
		cout<<"x mas" <<endl;
		system("PAUSE");
		break;

	case 'y':
		
		cout<<"You are still going through these?!" <<endl;
		system("PAUSE");
		break;

	case 'z':
		
		cout<<"zoinkers!" <<endl;
		system("PAUSE");
		break;

	default:
		
		cout<<"Cant do that, try again";
		system("PAUSE");
		break;
	}
	system("cls");
	cout<<"Would you like to go again?" <<endl;
	cout<<"		 (y,n)" <<endl;
	cin>> response;
	system("PAUSE");
	system("cls");
}


	while(response == 'y');

	return 0;
}




I know its pretty pointless, I was just trying to get used to some of the elements in it (do while, switch case, etc.)
tell me what you think
Was This Post Helpful? 0
  • +
  • -

#7 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Switch Case Help

Posted 01 December 2009 - 05:46 PM

Good Job! you could easily simplfy this with an array, but im guessing you have not cover that yet. But happy programming :)
Was This Post Helpful? 0
  • +
  • -

#8 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Switch Case Help

Posted 01 December 2009 - 05:59 PM

Your way is completly fine, just wanna show another solution so you can expand your horizons


#include <iostream>
#include <string>

using namespace std;




int main()
{
	char input;
	char letters[] = "abcdefghijklmnopqrstuvwxyz";
	string sayThis[26] = {"Anus","Boobies :)","Cornicopia","Dingo (eatcha baby?)","Erectile","F is for fire that burns down the whole town",
							"Gigantormous","Homo-erectus","I","JJ the Jet Plane" ,"kool","lmao" ,"malleable","N is for no survivors when you....","Octopussy",
							"paleontologist","quail" ,"roflcopter","titicaca" ,"U is for Uranium....bomb","vendetta","Whaaaaaaaaaaat","x mas","you are still going through these?,","Zoikers"};

	cout<<"Enter any letter in the English alphabet and" <<endl;
	cout<<"I will make a word beginning with that letter."<<endl;
	cout<<"And Make sure it is lowercase" <<endl;
	cin>> input;


	for(int i=0; i<26; i++)
	{
		if(letters[i] == input)
			cout<<sayThis[i]<<endl;
	}

	cin.ignore();
	cin.get();
	

	return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1