Reputation: 9
This is an assignment for my high school programming class, and I didn't seem to do anything wrong. However, when I run the Windows debugger in VS express 2013, every case falls through to the default, even though I have break;
s on all of them. I have no idea what I did wrong and I can't look it up at all.
// mauroc_switch.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<iostream>
using namespace std;
int main()
{
char yesform;
cout << "Type ''yes,'' lowercased and romanized, in a non-English language." << endl;
cin >> yesform;
//***Here is the switch statement.***
switch (yesform){
case 'yes':
cout << "This is the English ''yes.'' Please type a non-English version of ''yes.'' " << endl;
break;
case 'ja':
cout << "This is the German form of ''yes.'' It is also the Swedish and Norwegian form of ''yes.'' " << endl;
break;
case 'si':
cout << "This is both the Spanish and the Italian form of ''yes.'' " << endl;
break;
case 'oui':
cout << "This is the French form of ''yes.'' " << endl;
break;
case 'hai':
cout << "This is the Japanese form of ''yes.'' " << endl;
break;
case 'da':
cout << "This is the Russian form of ''yes.'' " << endl;
break;
case 'ne':
case 'ye':
cout << "This is a Korean form of ''yes.'' " << endl;
break;
case 'naam':
case 'aiwa':
cout << "This is an Arabic form of ''yes.'' " << endl;
break;
case 'sim':
cout << "This is the Portuguese form of ''yes.'' " << endl;
break;
case 'haan':
cout << "This is the Hindi form of ''yes.'' " << endl;
break;
default:
cout << "You either made a typo or you typed ''yes'' in an unsupported language. Please try again. ";
break;
}
system("pause");
return 0;
}
Upvotes: 0
Views: 175
Reputation: 176
If you absolutely have to use switch case for this assignment, then prior to your switch statement you will have to 'convert' your string into an integer value. An example of this would be...
enum yesLanguage {YES, JA, OUI, ..., CI};
yesLanguage yesAnswer = YES;
if (yesform == 'yes'){
yesAnswer = YES;
}
else if(yesform == 'ja'){
yesAnswer = JA;
}
And so on, and then in your switch case you'd have
switch(yesLanguage)
case YES:
Your YES output here;
break;
case JA:
.........
And so on with the rest of your code as above in your original post.
However if you don't need to use a switch case, then just do the if/else method as stated by the post above me.
EDITED to add enum. http://en.cppreference.com/w/cpp/language/enum
Upvotes: 0
Reputation: 56893
You are mixing characters and strings. Sadly, C++ allows a "character" like 'yes'
, but that is not really what you think it is. The other problem is that as soon as you switch to strings (std::string
), you can not use switch
anymore, but you need a sequence of if-else
statements or some other way to match the strings.
Here's a simple example that should work:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string yesform;
cout << "Type ''yes,'' lowercased and romanized, in a non-English language." << endl;
cin >> yesform;
if( yesform == "yes" )
cout << "This is the English ''yes.'' Please type a non-English version of ''yes.'' " << endl;
else if( yesform == "ja" )
cout << "This is the German form of ''yes.'' It is also the Swedish and Norwegian form of ''yes.'' " << endl;
else
cout << "You either made a typo or you typed ''yes'' in an unsupported language. Please try again. ";
return 0;
}
In your case, once you read the input into a string, try using a std::map
to map the input to an output string. That will likely be sufficient for the above example and it'll make the code way more readable.
Upvotes: 7