Something Wrong With My Program

Started by
9 comments, last by bert2 21 years, 10 months ago
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]
Hi everyone!
Advertisement

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

Your prototype does not match the actual function.
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Thanks! I didn''t know they had to match.
Hi everyone!

  #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]  
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>
Prototyping is a WASTE of time.
---START GEEK CODE BLOCK---GCS/M/S dpu s:+ a---- C++ UL(+) P(++) L+(+) E--- W++ N+ o K w(--) !O !M !V PS- PE+Y+ PGP+ t 5 X-- R tv+ b+ DI+ D G e* h! r-- !x ---END GEEK CODE BLOCK---
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.

of course prototypes have to match!!!!!! Don''t ever EVER try anything like that again.
What do you mean Prototyping is a waste of time? Care to explain the comment?
What do you mean Prototyping is a waste of time? Care to explain the comment?

This topic is closed to new replies.

Advertisement