Hi people. By posting in Beginner section you can see i just started learning C++. Ofc i use this site's tutorial for learning, i think it's great so i bookmarked it. I am a waiter in night club, but i have passion for computers so i want to try this. I have read "Before post MUST DO" but to tell you the truth as a newbie i pretty much don't know what to search for. Anyway, i made a simple calculator that can make only 1 operation, now i want User to input Digit1, Digit2 and Operation as a letter. I finished code and then i tried to run application. Shows no errors either warnings, but it doesn't perform operation requested by the user. It should go like this: Input 2 digits, A,B,C,D for operation (+,-,*,/) and then to calculate. Since it would be possible that user enter something other instead of declared letters, i did error handling to make cout showing that input is invalid. It's just a start off. So if someone can help me with this, code is at the end of post. Sorry for bad English and if i made post hard to understand. All the best
#include <iostream>
usingnamespace std;
int main()
{
// I like descriptive names (or descriptive enough names.)
int num1, num2; // You actually only needed two ints, num1 and num2.
int answer; // An integer to hold the answer
char choice; // descriptive choice char!
cout << "Enter number 1 : ";
cin >> num1;
cout << "Enter number 2 : ";
cin >> num2;
cout << "(choose) Operation to perform:\n"
<< "Addiction: (A)\n"
<< "Substraction: (B)\n"
<< "Multiplication: (C)\n"
<< "Division: (D)\n"; // This will look like your text before but now
// Becomes easier to see your options and what you have.
cin >> choice;
if(choice == 'a') // You need to set it up like this.
// before you where comparing the literal char 'h' with integer variable a
// This was the problem and you did it throughout
// The correct is a character variable like "choice" compared to a character
// literal like: 'a' 'b' ';' 'q' '=' and so on.
{
answer = num1 + num2;
cout << answer << endl;
}
elseif('h' == b) // Fix this to be like above if condition.
{
g=e-f;
cout << g;
}
elseif('h' == c) // Fix this to be like above if condition.
{
g=e*f;
cout << g;
}
elseif('h' == d) // Fix this to be like above if condition.
{
g=e/f;
cout << g;
}
// Problem here. ELSE needs no conditions to evaluate. It works if all other if and else if fails.
else //('h' != a || 'h' != b || 'h' != c || 'h' != d)
//; <-- BAD SEMICOLON
cout << "Unknown character!"; // This will now print if the above is fixed
return 0;
// This still will give errors. You need to repair what I showed you.
}