Hi,
I thought i'd have a play around in C++. I'm a complete newbie (in C++ not programming). I'm trying to make a simple calculator to get a grasp of the syntax. (i'm used to python and VB).
What trying to do is declare the functions with prototype functions up the top. Get from the user if they want to do addition,subtracton,div or multi and then do it. This is where my first problem comes in. I don't understand how to do.. elifs.. or elseifs.. if c++ even does it?. And also I don't know how to compare properly using the logical operator || .. It keeps giving me the error ANSI C++ forbids comparison between pointer and integer. I'll post the gcc output and my attempt to do the program.
Thanks very much for any help!
Code:#include <iostream> // Simple add, subtract, mult,divide calculator int funcAdd(int,int); int funcSub(int,int); float funcDiv(int,int); long funcMulti(int,int); int main() { char decide; int first; int second; int result; std::cout << "What would you like to do: (A)dd, (S)ubtract, (D)ivide or (M)ultiply ?\n"; std::cin >> decide; if ( (decide == "A") || (decide == "a") || (decide == "S") || (decide = "s") || (decide == "D") || (decide == "d") || (decide == "M") || (decide == "m") ) { if ((decide == "A") || (decide == "a")) std::cout << "\nAddition\n"; std::cout << "\nEnter the first number:"; std::cin >> first; std::cout << "\nEnter the second number:"; std::cin >> second; result = funcAdd(first,second); std::cout << "\nAnswer for " << first << " + " << second << " = " << result << std::endl; else if ( (decide == "S") || (decide = "s") ) std::cout << "\nSubtraction\n"; std::cout << "\nEnter the first number:"; std::cin >> first; std::cout << "\nEnter the second number:"; std::cin >> second; result = funcSub(first,second); std::cout << "\nAnswer for " << first << " - " << second << " = " << result << std::endl; else if ( (decide == "D") || (decide == "d") ) std::cout << "\nDivision\n"; std::cout << "\nEnter the first number:"; std::cin >> first; std::cout << "\nEnter the second number:"; std::cin >> second; result = funcDiv(first,second); std::cout << "\nAnswer for " << first << " / " << second << " = " << result << std::endl; else if ( (decide == "M") || (decide == "m") ) std::cout << "\nMultiplication\n"; std::cout << "\nEnter the first number:"; std::cin >> first; std::cout << "\nEnter the second number:"; std::cin >> second; result = funcMulti(first,second); std::cout << "\nAnswer for " << first << " * " << second << " = " << result << std::endl; } else std::cout "\n You did not enter a valid option"; } int funcAdd(int x,int y) { return x + y; } int funcSub(int x, int y) { return x - y; } float funcDiv(int x, int y) { return x / y; } long funcMulti(int x, int y) { return x * y; }
Code:andrew@me:~$ g++ calc.cpp -o calc calc.cpp: In function `int main()': calc.cpp:19: ANSI C++ forbids comparison between pointer and integer calc.cpp:19: ANSI C++ forbids comparison between pointer and integer calc.cpp:19: ANSI C++ forbids comparison between pointer and integer calc.cpp:19: assignment to `char' from `const char *' lacks a cast calc.cpp:19: ANSI C++ forbids comparison between pointer and integer calc.cpp:19: ANSI C++ forbids comparison between pointer and integer calc.cpp:19: ANSI C++ forbids comparison between pointer and integer calc.cpp:19: ANSI C++ forbids comparison between pointer and integer calc.cpp:21: ANSI C++ forbids comparison between pointer and integer calc.cpp:21: ANSI C++ forbids comparison between pointer and integer calc.cpp:29: parse error before `else' calc.cpp:38: parse error before `else' calc.cpp:45: warning: assignment to `int' from `float' calc.cpp:47: parse error before `else' calc.cpp:58: parse error before string constant calc.cpp:59: confused by earlier errors, bailing out




Reply With Quote