Oct 2, 2010 at 10:02am Oct 2, 2010 at 10:02am UTC
your problems are on lines 13 and 17.
"+" and "*" are string literals that are allocated on the heap so there are addresses associated with them. choice is an automatic variable located on the stack. When you use ==, you are comparing the two addresses - they will never equal.
What you want is:
1 2 3 4 5 6 7
#include <string.h>
// ...
if ( !strcmp( choice, "+" )) {
// ...
} else if ( !strcmp( choice, "*" )) {
// ...
}
Or, you can declare choice as a character and compare against characters:
1 2 3 4 5 6 7
char choice;
// ...
if ( choice=='+' ) {
// ...
} else if ( choice=='*' ) {
// ...
}
If you still want to use strings, but avoid C-like code and strcmp(), you should google std::string and use it in place of char choice[1]
There are many ways to skin this cat.
Last edited on Oct 2, 2010 at 10:08am Oct 2, 2010 at 10:08am UTC
Oct 2, 2010 at 10:10am Oct 2, 2010 at 10:10am UTC
choice == "+"
This is not how you compare cstrings.
a few options:
choice is one char, so why not use one char?
1 2
char choice;
if (choice == '+' )...
If you ever want to do this with longer cstrings
1 2
char choice[50];
if ( strcmp(choice, "+" ) == 0 )...
Though it would be better to use std::string
1 2 3 4
#include <string>
...
std::string choice;
if (choice == "+" )...
edit: too slow..
Last edited on Oct 2, 2010 at 10:14am Oct 2, 2010 at 10:14am UTC