Link to home
Start Free TrialLog in
Avatar of mayan1
mayan1

asked on

C++ Simple Console Calculator

I am trying to create a calculator in C++  It works in a console window and allows the user to enter
the first number, a second number and then an operator such as +, -, *,
+.  It will then display the result to the screen but I can not get it to compile.  
The errors I am receiving are:  
12 C:\Dev-Cpp\Calculator.cpp `main' must return `int'

 C:\Dev-Cpp\Calculator.cpp In function `int main(...)':

15 C:\Dev-Cpp\Calculator.cpp `cout' undeclared (first use this function)

  (Each undeclared identifier is reported only once for each function it appears in.)

16 C:\Dev-Cpp\Calculator.cpp `cin' undeclared (first use this function)


Here is my code:

#include <iostream.h>
#include <string.h>
#include <conio.h>
#include <dos.h>
float multiply(float firstnumber, float secondnumber)

{ return
firstnumber*secondnumber;
}
void main()

{ float firstnumber, secondnumber, answer;
char symbol;
char name[15];;
cout << "Enter number:";
cin >>firstnumber;
cout << "Enter mathematic system:";
cin >>symbol; cout << "Enter number:";
cin >> secondnumber;
switch (symbol)

{

case '+':
      answer = firstnumber+secondnumber;
      strcpy(name, "sum");
      break;
case '-':
      answer = firstnumber-secondnumber;
      strcpy(name, "difference");
      break;
case '*':
      answer = multiply(firstnumber, secondnumber);
      break;
case 'x':
      answer = multiply(firstnumber, secondnumber);
      strcpy(name, "product");
      break;
case '/':
      answer = firstnumber/secondnumber;
      strcpy(name, "quotient");
      break;

}
cout << "The " << name << " of " <<firstnumber << " and " <<secondnumber << " is "<< answer << "\n";


}



Avatar of Axter
Axter
Flag of United States of America image

Hi mayan1,
> #include <iostream.h>

iostream.h is not part of the C++ standard, and will not compile on mondern compilers like VC++ 7.x or 8.0

I recommend you use the standard header, which does not have the .H extension
#include <iostream>
#include <string>

All STL classes are extensionless headers.

>>void main()

main needs to have a return type.
int main()
{
  return 0;
}


David Maisonave (Axter)
Cheers!
#include <iostream>
#include <string>

using namespace std; //You'll need this if you don't prefix your obects with std (std::string, std::out)
Adding to above information:

#include <iostream>
#include <string>
using namespace std;

That are the only includes and statements you need for the task (till now). Simply remove the includes of dos.h and conio.h.

>>>> char name[15];
>>>> strcpy(name, "quotient");

Use  

     string name;
     name = "quotient";

instead.

If you want the console program keep running for further calculations you may do that:

int main()
{
   float firstnumber, secondnumber, answer;  
   char symbol = ' ';
   string name;
   while (true)
   {
     cout << "Enter number:";
     cin >>firstnumber;
     cout << "Enter mathematic system:";
     cin >>symbol;
     if (symbol == 'q' || symbol == 'Q')
         return 0;
     cout << "Enter number:";
     cin >> secondnumber;
     switch (symbol)
     {
      case '+':
        answer = firstnumber+secondnumber;
        name = "sum";
        break;
     case '-':
        answer = firstnumber-secondnumber;
        name="difference";
        break;
     case '*':
        answer = multiply(firstnumber, secondnumber);
        break;
     case 'x':
        answer = multiply(firstnumber, secondnumber);
        name = "product";
        break;
     case '/':
        answer = firstnumber/secondnumber;
        name = "quotient";
        break;
      }
      cout << "The " << name << " of " <<firstnumber << " and " <<secondnumber << " is "<< answer << "\n";
   }
   return 0;
}


Regards, Alex

Avatar of mayan1
mayan1

ASKER

I am still getting errors:
17 C:\Dev-Cpp\main.cpp `cout' undeclared (first use this function)

18 C:\Dev-Cpp\main.cpp `cin' undeclared (first use this function)

29 C:\Dev-Cpp\main.cpp `name' undeclared (first use this function)

36 C:\Dev-Cpp\main.cpp `multiply' undeclared (first use this function)

 C:\Dev-Cpp\Makefile.win [Build Error]  [main.o] Error 1


Here is my code:


#include <iostream>
#include <string>
float multiply(float firstnumber, float secondnumber)
using namespace std;

{
      returnfirstnumber*secondnumber;
}

int main()
{
   float firstnumber, secondnumber, answer;  
   char symbol = ' ';
   string name;
   while (true)
   {
     cout << "Enter number:";
     cin >>firstnumber;
     cout << "Enter mathematic system:";
     cin >>symbol;
     if (symbol == 'q' || symbol == 'Q')
         return 0;
     cout << "Enter number:";
     cin >> secondnumber;
     switch (symbol)
     {
      case '+':
        answer = firstnumber+secondnumber;
        name = "sum";
        break;
     case '-':
        answer = firstnumber-secondnumber;
        name="difference";
        break;
     case '*':
        answer = multiply(firstnumber, secondnumber);
        break;
     case 'x':
        answer = multiply(firstnumber, secondnumber);
        name = "product";
        break;
     case '/':
        answer = firstnumber/secondnumber;
        name = "quotient";
        break;
      }
      cout << "The " << name << " of " <<firstnumber << " and " <<secondnumber << " is "<< answer << "\n";
   }
   return 0;
}

>>>> float multiply(float firstnumber, float secondnumber)

You need to add a semicolon ';' at end of that forward declaration.

Regards, Alex

Avatar of mayan1

ASKER

Alex,Thank you for your help on that.  I am having a terrible time understanding the compiler errors.  Can you tell me what is wrong now?
Here are my errors:

6 C:\Dev-Cpp\main.cpp expected unqualified-id before '{' token
6 C:\Dev-Cpp\main.cpp expected `,' or `;' before '{' token


Here is my code:

#include <iostream>
#include <string>
float multiply(float firstnumber, float secondnumber);
using namespace std;

{
      return firstnumber*secondnumber;
}
 
 

int main()
{
   float firstnumber, secondnumber, answer;  
   char symbol = ' ';
   string name;
   while (true)
   {
     cout << "Enter number:";
     cin >>firstnumber;
     cout << "Enter mathematic system:";
     cin >>symbol;
     if (symbol == 'q' || symbol == 'Q')
         return 0;
     cout << "Enter number:";
     cin >> secondnumber;
     switch (symbol)
     {
      case '+':
        answer = firstnumber+secondnumber;
        name = "sum";
        break;
     case '-':
        answer = firstnumber-secondnumber;
        name="difference";
        break;
     case '*':
        answer = multiply(firstnumber, secondnumber);
        break;
     case 'x':
        answer = multiply(firstnumber, secondnumber);
        name = "product";
        break;
     case '/':
        answer = firstnumber/secondnumber;
        name = "quotient";
        break;
      }
      cout << "The " << name << " of " <<firstnumber << " and " <<secondnumber << " is "<< answer << "\n";
   }
   return 0;
}
ASKER CERTIFIED SOLUTION
Avatar of Axter
Axter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial