Solved

C++ Simple Console Calculator

Posted on 2006-11-21
7
4,419 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
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!

 

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

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

738 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