Solved

C++ Simple Console Calculator

Posted on 2006-11-21
7
4,390 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
  • 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

746 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now