Help with my calculater program.

• 10-11-2002
kogh
Help with my calculater program.
Code:

```///////////////////////////// //////////////////////    /// //                //    /// //  Calculater    //    /// //  Program v 1.1  //    /// //  Coded by:      //    /// //  James AKA Kogh //    /// //                //    /// /////////////////////    /// ///////////////////////////// #include <iostream.h> int mult(int x, int y); int add(int x, int y); int div(int x, int y); int sub(int x, int y); int main() {         char desc;         int x,y;         for (;;)         {         cout<<"Calc Program 1.1 Menu:";         cout<<endl;         cout<<"A.Add";         cout<<endl;         cout<<"B.Subtract";         cout<<endl;         cout<<"C.Multiply";         cout<<endl;         cout<<"D.Divide";         cout<<endl;         if(desc=='a'|| desc=='A')         {         cout<<"Enter two numbers to be added: ";         cin>>x>>y;         cout<<add(x,y);         }         if(desc=='b'|| desc=='B')         {         cout<<"Enter two numbers to be subtacted: ";         cin>>x>>y;         cout<<sub(x,y);         }         if(desc=='c'|| desc=='C')         {         cout<<"Enter two numbers to be multiplied: ";         cin>>x>>y;         cout<<mult(x,y);         }         if(desc=='d'|| desc=='D')         {         cout<<"Enter two numbers to be divided: ";         cin>>x>>y;         cout<<div(x,y);         }         return 0; } int add(int x, int y) {         return x+y; } int sub(int x, int y) {         return x-y; } int mult(int x, int y) {         return x*y; } int div(int x, int y) {         return x/y; }```
I'm a fairly new C++ programmer, and I was wondering why this code doesn't work... :0
If anyone could point out where i screwed it up at, it'd be greatly appretiated!

-kogh
• 10-11-2002
ggs
you never allow your users to choose which option they want (so the value of desc is never set) :O
• 10-11-2002
Mister C
Yep ggs is correct so:

Code:

```   cin >> desc;```
before the first if should work.
• 10-11-2002
kogh
Ohh. yeah, I didn't catch that, thanks ;D

but, I meant I got a compiling error:

Code:

```--------------------Configuration: startingover1 - Win32 Debug-------------------- Compiling... startingover1.cpp E:\Programming\Visual Studio 6\MSDev98\MyProjects\startingover1\startingover1.cpp(75) : error C2601: 'add' : local function definitions are illegal E:\Programming\Visual Studio 6\MSDev98\MyProjects\startingover1\startingover1.cpp(80) : error C2601: 'sub' : local function definitions are illegal E:\Programming\Visual Studio 6\MSDev98\MyProjects\startingover1\startingover1.cpp(85) : error C2601: 'mult' : local function definitions are illegal E:\Programming\Visual Studio 6\MSDev98\MyProjects\startingover1\startingover1.cpp(90) : error C2601: 'div' : local function definitions are illegal E:\Programming\Visual Studio 6\MSDev98\MyProjects\startingover1\startingover1.cpp(93) : fatal error C1004: unexpected end of file found Error executing cl.exe. startingover1.exe - 5 error(s), 0 warning(s)```
I just don't understand how those functions cause the errors.
• 10-11-2002
Klinerr1
cin>>x>>y;

i dont think visual likes that try doing cin >>x then a sepereate cin >> y (i know someone is going to say something against my word)
• 10-11-2002
kogh
Nope, I still get the same errors. =(
• 10-11-2002
Mister C
Also, that code:

Code:

``` for (;;)         {```

where do you close it (missing a })

and why do you need the for loop?
• 10-11-2002
kogh
OHHH. Ok, I forgot to close it, heh. Thanks Mister C. =)

I needed the "for" loop so it would go back to the menu after you do something. And I put the return 0 inside the for loop, which messed it up. so I just fixed it ^^.

here's the finished code now:

Code:

```///////////////////////////// //////////////////////    /// //                //    /// //  Calculater    //    /// //  Program v 1.1  //    /// //  Coded by:      //    /// //  James AKA Kogh //    /// //                //    /// /////////////////////    /// ///////////////////////////// #include <iostream.h> int mult(int x, int y); int add(int x, int y); int div(int x, int y); int sub(int x, int y); int main() {         char desc;         int x,y;         for (;;)         {         cout<<"Calc Program 1.1 Menu:";         cout<<endl;         cout<<"A.Add";         cout<<endl;         cout<<"B.Subtract";         cout<<endl;         cout<<"C.Multiply";         cout<<endl;         cout<<"D.Divide";         cout<<endl;         cin >> desc;         if(desc=='a'|| desc=='A')         {         cout<<"Enter two numbers to be added: ";         cin>>x;         cin>>y;         cout<<add(x,y);         }         if(desc=='b'|| desc=='B')         {         cout<<"Enter two numbers to be subtacted: ";         cin>>x;         cin>>y;         cout<<sub(x,y);         }         if(desc=='c'|| desc=='C')         {         cout<<"Enter two numbers to be multiplied: ";         cin>>x;         cin>>y;         cout<<mult(x,y);         }         if(desc=='d'|| desc=='D')         {         cout<<"Enter two numbers to be divided: ";         cin>>x;         cin>>y;         cout<<div(x,y);         }                 }         return 0; } int add(int x, int y) {         return x+y; } int sub(int x, int y) {         return x-y; } int mult(int x, int y) {         return x*y; } int div(int x, int y) {         return x/y; }```
• 10-11-2002
Mister C
It may work -but I do not like using an infinite for loop.
Why don't you use a do/while loop with the menu?
• 10-11-2002
Nakeerb
Code:

```#include <iostream.h> #include <conio> #include <ctype> double mult(double x, double y); double add(double x, double y); double divi(double x, double y); double sub(double x, double y); double main() {         char desc, yesOrNo;         double x,y;                 do{                 cout<<"Calc Program 1.1 Menu:\n"                     <<"A.Add\n"                     <<"B.Subtract\n"                     <<"C.Multiply\n"                     <<"D.Divide\n";                         cin >> desc;                 desc = tolower(desc);                         if(desc=='a')                 {                 cout << "Enter two numbers to be added: ";                 cin >> x >> y;                 cout << "ANSWER IS: ";                 cout << add(x,y);                 }                         if(desc=='b')                 {                 cout << "Enter two numbers to be subtacted: ";                 cin >> x >> y;                 cout << "ANSWER IS: ";                 cout << sub(x,y);                 }                         if(desc=='c')                 {                 cout << "Enter two numbers to be multiplied: ";                 cin >> x >> y;                 cout << "ANSWER IS: ";                 cout << mult(x,y);                 }                         if(desc=='d')                 {                 cout << "Enter two numbers to be divided: ";                 cin >> x >> y;                 cout << "ANSWER IS: ";                 cout << divi(x,y);                 }                                 getch();                 clrscr();                 cout << "Wish to retry? (y or n): ";                 cin >> yesOrNo;                 yesOrNo = tolower(yesOrNo);                 clrscr();         }while(yesOrNo == 'y');                         return 0; } double add(double x, double y) {         return x+y; } double sub(double x, double y) {         return x-y; } double mult(double x, double y) {         return x*y; } double divi(double x, double y) {         return x/y; }```
Hope this helps :P
• 10-11-2002
Nakeerb
BTW I am using Borland compiler with Quincy so some syntax may not work if you are using Visual C++ or something. If it does not work, take out all my getch and clrscr statements