Thread: Coding help
View Single Post
The Unknown Guru
We Must Repeat
The Unknown Guru's Avatar
Location: D-E-V-O!
#8
Default

Originally Posted by Depression Moon ^
my troubles with the software have gone, but there's an issue here that i'm having with cases. I'm supposed to be adding a question to the code below that allows the user to choose if they want to do an addition subtraction, mulitiplication, or division problem.
Eww, I've always hated switch. You can do so much more with if statements...

But anyways, there are two routes you can go with this. You could have 4 separate Y/N questions that ask "Would you like to do an addition problem?" etc. Or you could do a strcmp and actually have the user type out what they want. That portion of the code could look something like this:

Side note: I'm weird and use printf and scanf instead of cin and cout. I would show you this with cout if I knew the syntax, but I don't. Sorry. I'm pretty sure it's similar, though.

Code:
 #include string.h //obviously, put in the carrot things

int main 
{
char questiontype[21]; //declare a string that takes users input

//your code

//when you get to the part where they choose:
printf("Would you like to do addition, subtraction, multiplication, or division?\n");
printf("Type in all lowercase letters, and don't misspell anything.\n");

scanf("%21[^\n]", questiontype); //user types what kind of question
flushall(); //required for scanf

if(strcmp(questiontype, "addition") == 0) //if string that took user input is identical to the phrase "addition"
{
//whatever you do to make the questions about addition
}

if(strcmp(questiontype, "subtraction") == 0) //if string that took user input is identical to the phrase "addition"
{
//whatever you do to make the questions about subtraction
}

//repeat for division and multiplication
If you really have to use switch, then you can create an int that will take the value of a strcmp and then put in something like this:
Code:
int compare;

//code

compare = strcmp(questiontype, "addition");

switch
{
case 0: //if questiontype is identical to "addition"
//do stuff
break;

default:
break;
}

//then repeat for the other operations.
If you have any more questions, feel free to PM me or something. I know C++ pretty well, and I would be happy to help
Old 07-06-2009, 10:24 AM
Reply With Quote
The Unknown Guru is offline