What is the problem with the simple calculator program?

advertisements
#include <iostream>
#include <iomanip>
using namespace std;

int calculate ()
{
double a;
double b;
a =(3.0);
b =(5.0);
cout << "  " << fixed << setprecision (1) << a << "\n" << endl;
cout << "* " << fixed << setprecision (1) << b << "\n" << endl;
cout << "------" << endl;
cout << fixed << setprecision (2) << a*b << "\n" << endl;
system("PAUSE");

return 0;
}
int calculate(int a, int b, int c)
{
double a;
double b;
double c;
a =(7.1);
b =(8.3);
c =(2.2);
cout << "  " << fixed << setprecision (1) << a << "\n" << endl;
cout << "* " << fixed << setprecision (1) << b << "\n" << endl;
cout << "- " << fixed << setprecision (1) << c << "\n" << endl;
cout << "------" << endl;
cout << fixed << setprecision (2) << (a*b)-c << "\n" << endl;
system("PAUSE");

return 0;
}

Question: Why do I have to change doubles--->ints? Does anything standout that is wrong?

Here is the output: 1>------ Build started: Project: HW1-2, Configuration: Debug Win32 ------ 1> HW1-2.cpp 1>c:\users\asus\desktop\hw1-2\hw1-2\hw1-2.cpp(21): error C2082: redefinition of formal parameter 'a' 1>c:\users\asus\desktop\hw1-2\hw1-2\hw1-2.cpp(22): error C2082: redefinition of formal parameter 'b' 1>c:\users\asus\desktop\hw1-2\hw1-2\hw1-2.cpp(23): error C2082: redefinition of formal parameter 'c' 1>c:\users\asus\desktop\hw1-2\hw1-2\hw1-2.cpp(24): warning C4244: '=' : conversion from 'double' to 'int', possible loss of data 1>c:\users\asus\desktop\hw1-2\hw1-2\hw1-2.cpp(25): warning C4244: '=' : conversion from 'double' to 'int', possible loss of data 1>c:\users\asus\desktop\hw1-2\hw1-2\hw1-2.cpp(26): warning C4244: '=' : conversion from 'double' to 'int', possible loss of data ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


You've got two functions both named main(). I'm not sure what you're trying to accomplish, but functions must have unique names within a namespace.

You can name your functions whatever you like. main() is special, though -- it's the entry point for the program, the one that gets called when the program runs. If you have another function, you'll need to call it yourself from inside main() if you want it to be executed:

int foo(int c)
{
    return c + 2;
}

int main()
{
    int a = 1;
    int b = foo(a);
    return b;
}