cin not appearing in compiler
Apr 1, 2015 at 3:23pm UTC
The second cin statement doesn't allow me to enter anything when I compile the code. The first one where you enter yes or no works fine. But the cin statement to enter a symbol won't show up in the compiled code. Why is that?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
#include <iostream>
char yes;
char no;
char symbol;
double number1, number2;
int main()
{
std::cout << "Welcome to my calculator\n"
<< "Would you like to use the calculator?\n"
<< "Please enter yes or no\n" ;
std::cin >> yes, no;
if (yes)
{
std::cout << "Please enter a symbol to indicate which type of problem we will be solving\n"
<< "The choices are +, -, /, *, or % to find the modulus of two numbers\n" ;
std::cin >> symbol;
}
else if (no)
{
std::cout << "Goodbye\n" ;
}
else
{
std::cout << "\nInvalid operator entered\n" ;
}
if (symbol == '+' )
{
std::cout << "Please enter two numbers\n" ;
std::cin >> number1 >> number2;
std::cout << number1 << " plus " << number2 << " is " << number1 + number2 << '\n' ;
}
else if (symbol == '-' )
{
double number1, number2;
std::cout << "Please enter two numbers\n" ;
std::cin >> number1 >> number2;
std::cout << number1 << " minus " << number2 << " is " << number1 - number2 << '\n'
<< number2 << " minus " << number1 << " is " << number2 - number1 << '\n' ;
}
else if (symbol == '/' )
{
double number1, number2;
std::cout << "Please enter two numbers\n" ;
std::cin >> number1 >> number2;
std::cout << number1 << " divided by " << number2 << " is " << number1 / number2 << '\n'
<< number2 << " divided by " << number1 << " is " << number2 / number1 << '\n' ;
}
else if (symbol == '*' )
{
double number1, number2;
std::cout << "Please enter two numbers\n" ;
std::cin >> number1 >> number2;
std::cout << number1 << " times " << number2 << " is " << number1 * number2 << '\n' ;
}
else if (symbol == '%' )
{
int number1, number2;
std::cout << "Please enter two integers\n" ;
// Can only use whole numbers for modulus
std::cin >> number1 >> number2;
if (number2 != 0) std::cout << number1 << " modulus " << number2 << " is " << number1 % number2 << '\n' ;
if (number1 != 0) std::cout << number2 << " modulus " << number1 << " is " << number2 % number1 << '\n' ;
}
}
Apr 1, 2015 at 4:09pm UTC
you need to include <string> to use strings/chars with streams.
#include <string>
EDIT: scratch that nonsense :)
Last edited on Apr 1, 2015 at 4:18pm UTC
Apr 1, 2015 at 4:14pm UTC
std::cin >> yes, no;
What are you trying to do here? You're using the comma operator incorrectly here.
I would instead have a char variable named "response" or something, and you should see whether response == 'y' or 'n'.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
char response;
std::cin >> response;
if (response == 'y' )
{
...
}
else if (response == 'n' )
{
...
}
else
{
// invalid input.
}
Last edited on Apr 1, 2015 at 4:16pm UTC
Apr 1, 2015 at 4:57pm UTC
@Ganado. You just helped me so much. As you can probably tell I'm new to C++. I was stuck on that for awhile, but now my whole program works. Thanks!
Topic archived. No new replies allowed.