• Advertisement

Archived

This topic is now archived and is closed to further replies.

Something Wrong With My Program

This topic is 5781 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I tried to make an addition calcualator, but when I complied it I got 2 errors. Here's the code. Can you see what's wrong with it?
    
#include <iostream>
#include <stdlib.h>
#include <string>
using namespace std;

float addition(float);
    float additionFirst, additionSecond, additionAnswer;

int main()
{  
    cout << "Please enter the first number in your addition equaltion: \n";
    cin >> additionFirst;
    cout << "Please enter the second number in your addition equaltion: \n";
    cin >> additionSecond;
    additionAnswer = addition(additionFirst, additionSecond);
    cout << additionFirst << " + " << additionSecond << " = " << additionAnswer << "\n";
    
    system("pause");
    return 0;
}

float addition(float additionFirst, float additionSecond)
{
    float additionAnswer;
    additionAnswer = additionFirst + additionSecond;
    
    return additionAnswer;
}
    
[edited by - bert2 on June 27, 2002 11:54:45 PM]

Share this post


Link to post
Share on other sites
Advertisement

  
float addition(float)
...
float addition(float additionFirst, float additionSecond)

Your prototype does not match the actual function.

Share this post


Link to post
Share on other sites

  
#include <iostream>
#include <stdlib.h>
#include <string>

using namespace std;

float addition(float, float);
float additionFirst, additionSecond, additionAnswer;

int main()
{
cout << "Please enter the first number in your addition equaltion: \n";
cin >> additionFirst;
cout << "Please enter the second number in your addition equaltion: \n";
cin >> additionSecond;
additionAnswer = addition(additionFirst, additionSecond);
cout << additionFirst << " + " << additionSecond << " = " << additionAnswer << "\n";
system("pause");
return 0;
}

float addition(float addOne, float addTwo)
{
return addOne + addTwo;
}
[\source]

Share this post


Link to post
Share on other sites
why dont you just:

additionAnswer = additionOne + additionTwo;


??

<marquee width=100% direction=right><table nostyle="filter:glow(color=red,strength=3)"><table nostyle="filter:shadow(color=blue,direction=left)">Can someone be nice and help me on my way to be the next Hideo Kojima?</table></table></marquee>

Share this post


Link to post
Share on other sites
It would also seem that all three variables, additionFirst, additionSecond, and additionAnswer should be local variables within main. When passing them in as parameters to add them there is no need to have them declared globally anymore. There actual values are not being changed or accessed outside of the main function except through parameters. Not a huge problem but just more efficient code.

I also agree with jarod83 to do the math right within the return statement of the addition function. No need for the additionAnswer at all within that function. Again it should be local to main so you wouldn''t have access to it anyway.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
of course prototypes have to match!!!!!! Don''t ever EVER try anything like that again.

Share this post


Link to post
Share on other sites

  • Advertisement