Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C++ Simple Console Calculator

Posted on 2006-11-21
7
Medium Priority
?
4,449 Views
Last Modified: 2012-06-22
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";


}



0
Comment
Question by:mayan1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 17990032
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!
0
 
LVL 30

Expert Comment

by:Axter
ID: 17990041
#include <iostream>
#include <string>

using namespace std; //You'll need this if you don't prefix your obects with std (std::string, std::out)
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 17993779
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

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:mayan1
ID: 18000168
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;
}

0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 18001241
>>>> float multiply(float firstnumber, float secondnumber)

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

Regards, Alex

0
 

Author Comment

by:mayan1
ID: 18003971
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;
}
0
 
LVL 30

Accepted Solution

by:
Axter earned 2000 total points
ID: 18004190
float multiply(float firstnumber, float secondnumber);
using namespace std;

{
      return firstnumber*secondnumber;
}

You have a using namespace clause after the function declaration, and a semicolon.
Should be the following:

using namespace std;

float multiply(float firstnumber, float secondnumber)

{
      return firstnumber*secondnumber;
}
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question