   Newbie needs help in basic c++ calculator code
  Hey guys, I just started learning C++ and I got stuck with one of my practise codes. It just bugs me cause I know it's just a little problem I'm missing and I don't know what it is! Please help.#include <iostream>#include <string>using namespace std;int num1, num2, result;int Multiply();int Divide();int Subtract();int Add();int task;int main()// Calculator which performs the basic Add, Multiply, Divide and \\Subtract duties using functions{cout << "Enter a number: " << endl;cin >> num1;cout << "\nEnter a second number: ";cin >> num2;cout << "\nchoose a task: \n";cout << "\n1. Multiply\n";cout << "\n2. Divide\n";cout << "\n3. Subtract\n";cout << "\n4. Add\n\n";cin >> task;while (task == '1')Multiply();while (task == '2')Divide();while (task == '3')Subtract();while (task == '4')Add();cout << "\nThe answer is " << result << ". \n\n";cout << "\n Thank you and have a nice day!\n\n\n";cin.get();system("PAUSE");return 0;}int Multiply(){result = (num1 * num2);return result;}int Divide(){result = (num1 / num2);return result;}int Subtract(){result = (num1 - num2);return result;}int Add(){result = (num1 + num2);return result;}I'm learning functions and for some reason, my result keeps coming up to 0 when I compile and run the code. I would really appreciate any help.   
  A couple of things.Your problem is that you are reading "task" as an integer, and then you are comparing its values to character literals. The integers 1, 2, 3 and 4 are not equal to the characters '1', '2', '3' and '4'.If you change this, you will probably find that your program never outputs anything. This is because you are using loops for your operations.If you say "while(task == 1) { /* code */ }", and the code doesn't change the value of "task", then you have an infinite loop.You probably want a series of if...else if ... else if.. else statements to determine what to do for a particular value of "task" (don't forget to include a final else statement in case the user doesn't input a correct task).There are other things that we can clean up in your code, but for the moment I'll let you work on the above, until the code works correctly.   ;  One reason, if the task is ever equal to a valid value (such as 1,2,3, or 4), then you are going to go into an infinite loop that continues to call one of the four respective functions. Secondly, you're returning a global variable, but you aren't even using the return value. (So you could either eliminate the return value, or eliminate the global.)Try something like this:[source="cpp"]#include <iostream>#include <string>using namespace std;int Multiply( int val1, int val2 );int Divide( int val1, int val2 );int Subtract( int val1, int val2 );int Add( int val1, int val2 );int main(){int num1, num2, result;   char task;cout << "Enter a number: " << endl;cin >> num1;cout << "\nEnter a second number: ";cin >> num2;cout << "\nchoose a task: \n";cout << "\n1. Multiply\n";cout << "\n2. Divide\n";cout << "\n3. Subtract\n";cout << "\n4. Add\n\n";cin >> task;switch( task ) {case '1':result = Multiply( num1, num2 );break;case '2':result = Divide( num1, num2 );break;case '3':result = Subtract( num1, num2 );break;case '4':result = Add( num1, num2 );break; default: // invalid task}cout << "\nThe answer is " << result << ". \n\n";cout << "\n Thank you and have a nice day!\n\n\n";cin.get();system("PAUSE");return 0;}int Multiply( int val1, int val2 ){result = (num1 * num2);return result;}int Divide( int val1, int val2 ){result = (num1 / num2);return result;}int Subtract( int val1, int val2 ){result = (num1 - num2);return result;}int Add( int val1, int val2 ){result = (num1 + num2);return result;}EDIT: ninja'd   ;  Thanks guys!!. What did the trick was changing the task datatype from 'int' to 'char'. Its working now. And my code IS a mess. I'll work on that too. Thanks a lot!   ;Quote:Original post by liberatiThanks guys!!. What did the trick was changing the task datatype from 'int' to 'char'. Its working now. And my code IS a mess. I'll work on that too. Thanks a lot!You changed the infinite while loops as well though too, didn't you?   ;  Hey manI'm sorry that I don't have the time to go through all the theory with you right now as I have problems of my own (just wait till you get to shader programming now theres a world of fun :p). But try this code instead:#include <iostream>#include <string>using namespace std;int Multiply(int no1, int no2);int Divide(int no1, int no2);int Subtract(int no1, int no2);int Add(int no1, int no2);int num1 = 0;int num2 = 0;int result = 0;// Task is a character not an intchar task = '0';int main()// Calculator which performs the basic Add, Multiply, Divide and\\Subtract duties using functions{cout << "Enter a number: " << endl;cin >> num1;cout << "\nEnter a second number: ";cin >> num2;cout << "\nchoose a task: \n";cout << "\n1. Multiply\n";cout << "\n2. Divide\n";cout << "\n3. Subtract\n";cout << "\n4. Add\n\n";// You can only input characters (you can convert them to ints later on, but lets leave this for now)cin >> task;// When dealing with multiple options such as this, use a switch statement instead of "whiles". // It will simplify your code, make it more readable and reduce the change of error.switch(task){// (Task == '1')case '1':result = Multiply(num1, num2);break;// (Task == '2')case '2':result = Divide(num1, num2);break;// (Task == '3')case '3':result = Subtract(num1, num2);break;// (Task == '4')case '4':result = Add(num1, num2);break;default:cout << "\nThe task is no recognised\n";break;}cout << "\nThe answer is " << result << ". \n\n";cout << "\n Thank you and have a nice day!\n\n\n";cin.get();system("PAUSE");return 0;}// This function takes in two different numbers (int) and then returns the multiplication of the two numbersint Multiply(int no1, int no2){return (no1 * no2);}// This function takes in two different numbers (int) and then returns the division of the two numbersint Divide(int no1, int no2){return (no1 / no2);}// This function takes in two different numbers (int) and then returns number one minus number twoint Subtract(int no1, int no2){return (no1 - no2);}// This function takes in two different numbers (int) and then returns number one plus number twoint Add(int no1, int no2){return (no1 + no2);}Look through what I've done and think about how this makes things better. I through in alittle documentation too to help. Again sorry I don't have time right now to talk through all the theory will you.I would recommend reading a book like "Teach yourself C++ in 21 days" (one of the better fast learning books), you can find this specific book here: http://www.amazon.com/Sams-Teach-Yourself-Days-5th/dp/0672327112/ref=sr_1_1?ie=UTF8&s=books&qid=1245936579&sr=8-1Hope this helps   ;Quote:Original post by Halifax2Quote:Original post by liberatiThanks guys!!. What did the trick was changing the task datatype from 'int' to 'char'. Its working now. And my code IS a mess. I'll work on that too. Thanks a lot!You changed the infinite while loops as well though too, didn't you?Yeah I did. Its now in if and else if.  RoadToRuin, I only just started c plus plus. It'll take me a while to get that good, but neat code tho'. I'll be taking that in too. And yeah, it helps loads. Like i said, my code's a mess.   ;You don't need to call system("PAUSE") to artificially pause execution.   ;  How would you keep the console window open in the case of when the program is not ran from an IDE? Say from an executable ran from windows explorer?Perhaps I missed that in the article.;Quote:Original post by MegahertzHow would you keep the console window open in the case of when the program is not ran from an IDE? Say from an executable ran from windows explorer?Perhaps I missed that in the article.Quote:From the article“real” console mode applications typically are designed for batch pipelined processingAs he tells you, "real" console applications don't pause at the end of their execution.