-1
#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 ==========

Justin Chiang
  • 175
  • 1
  • 1
  • 8

5 Answers5

2

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;
}
Caleb
  • 124,013
  • 19
  • 183
  • 272
  • -1 because he acknowledges this and he is saying his problem involves the second. – Rapptz Sep 18 '12 at 05:00
  • 1
    @rapptz I don't see any such acknowledgement, and what I do see seems to indicate that both are being used together: "First function works fine until I add the second one..." – Caleb Sep 18 '12 at 05:06
  • @Caleb now that I reread it, I see this is true. However my downvote is locked until you edit. – Rapptz Sep 18 '12 at 05:08
  • Yes, 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. – Caleb Sep 18 '12 at 05:09
  • oh ic, ok let me try what you say, thanks! post right back – Justin Chiang Sep 18 '12 at 05:12
  • maybe it works now but there are some other problems if you see output errors. – Justin Chiang Sep 18 '12 at 05:15
  • Now you're not calling `happy()` from within `main()`. More generally, we don't have a clear understanding of what your trying to do, or even what problem you're seeing. We're happy to help, but don't make us guess. You're clearly just starting out -- nothing wrong with that, but if you're a student you'll probably learn more quickly and thoroughly if you ask you instructor or TA for some help. – Caleb Sep 18 '12 at 05:31
0

You're problem is most likely on this line:

(a*b)c

What are you trying to accomplish here? Your compiler should complain that there is a missing semicolon. It's not valid c++. You need to specify another operand (+, -, * etc) between (a*b) and c

Aesthete
  • 18,622
  • 6
  • 36
  • 45
0

(a*b)c <-- Error

If you want to multiply, it should be (a*b)*c

EDIT - Seems like you want to do this (a*b) - c, which can be written as a*b - c as well.

Vinayak Garg
  • 6,518
  • 10
  • 53
  • 80
0

I agree with caleb:

you have two main functions. And since your errormessage points to line 21 which coincidently is where a second definition of main starts I would say this is the first error. After that you have the multiplication error where you ommitted the operator as stated by others.

Explanation of the difference of definition and declaration

Just a style hint: Personally I find it convenient to write function(param1, param2, ...) rather than function (param1, param2, ...) (notice the missing space) since there is a difference between myDefinedSomething (variableaccess) and myDefinedSomething() (functioncall). (You can also access functionspointers, in that case you ommit the paranthese but you will have a bit to tlearn before you should care about that).

And as stated in the comments, try indenting your code it makes it more readable, consider:

for(int i=1,i<10; i++){
if(i%3) {
if(flag){
cout<<"a";
}else{
cout<<"b";
}
}

versus

for(int i=1,i<10; i++){
    if(i%3) {
        if(flag){
            cout<<"a";
        }else{
            cout<<"b";
        }
}

In which one did you notice the missing } first?

Community
  • 1
  • 1
ted
  • 4,791
  • 5
  • 38
  • 84
  • Oh ic, what should I replace it with then? what should I call the second "main function"? – Justin Chiang Sep 18 '12 at 05:10
  • @JustinChiang Name your functions anything reasonable (the name should be short and describe what they do, e.g. `multiplication` if you have a function which takes care of the multiplication expressions of your calculator). Be wary though, do not use names twice. And `main` is a special name in c++, it indicates the starting point of your program – ted Sep 18 '12 at 05:19
  • your wisdom is beyond my understanding, I have copy and pasted it into notebook for future learning. Am I missing curly braces? Oh ic I understand most of what you have written EXCEPT your coding example lol! – Justin Chiang Sep 18 '12 at 05:20
  • @JustinChiang: I added a link for the difference between declaration and definition (you WILL need the declarations sooner or later). I am sorry if I can't express it in a manner that comes easy to you. If you could point out the spots you have trouble understanding and why (vacabulary, wording, "What is ...") I will edit my awnser to clarify. – ted Sep 18 '12 at 05:38
0

Like Caleb said, you have two main() declarations. The code without the second main function compiles just fine:

#include <iostream>
#include <iomanip>

int main()
{
    double a;
    double b;
    double c;
    a = (7.1);
    b = (8.3);
    c = (2.2);
    std::cout << std::fixed << std::setprecision(1) << std::endl;
    std::cout << "  " << a << "\n" << std::endl;
    std::cout << "* " << b << "\n" << std::endl;
    std::cout << "- " << c << "\n" << std::endl;
    std::cout << "------" << std::endl;
    std::cout << std::setprecision(2) << (a * b) - c << "\n" << std::endl;

    return 0;
}

If you wanted this to be a separate function, you could rename it to something else such as int calculator().

Rapptz
  • 20,807
  • 5
  • 72
  • 86