Solved

C++ Simple Console Calculator

Posted on 2006-11-21
7
4,436 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 500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

635 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