Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 03-20-2004
TWIXMIX
My code wont work even though it looks like it should.
I'm learning some functions so this will help me practice.

Code:

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

using namespace std;

int add (int a, int b)
{
}
int subtract (int c, int d)
{
}
int multiply (int e, int f)
{
}
int divide (int g, int h)
{
}

int main ()
{
int pick1, pick2;
cin >> pick1, pick2;
cout << subtract (pick1, pick2);
cout << multiply (pick1, pick2);
cout << divide (pick1, pick2);

system("pause");
return 0;
}

THanks
• 03-20-2004
SirCrono6
What do you mean it won't work? Also, what compiler? I used Dev-C++ 4.9.8.0 and it worked fine. But, you should add some line breaks so you can tell the answers apart (unless there's a reason for that). Also, what is the point of answer? You can just use:

Code:

return number operation number;
//Like
return A * B;

Here's some code that works fine on Dev-C++:

Code:

#include <iostream>

int Add( int A, int B )
{
return A + B;
}
int Subtract( int C, int D )
{
return C - D;
}
int Multiply( int E, int F )
{
return E * F;
}
int Divide( int G, int H )
{
return G / H;
}

int main ()
{
int Number1, Number2;
std::cout << "Enter the numbers: ";
std::cin >> Number1 >> Number2;
std::cout << Add( Number1, Number2 ) << std::endl;
std::cout << Subtract( Number1, Number2 ) << std::endl;
std::cout << Multiply( Number1, Number2 ) << std::endl;
std::cout << Divide( Number1, Number2 ) << std::endl;
std::cin.get();
std::cin.get();
return 0;
}

Note you can still use using namespace std; system( "PAUSE" ); and answer (although it doesn't need to be global). Also, you can, in the std::cin statement, using the comma instead of an additional >>. You should also probably put a prompt, as it makes more sense.

- SirCrono6
• 03-20-2004
TWIXMIX
THe code works fine but it displays the wrong answers.

Im using the newest version of Dev c++
• 03-20-2004
TWIXMIX
ok its works now geeze.
in the cin part I had :

cin >> pick1, pick2

cin >> pick1 >> pick2

lol
• 03-20-2004
Tronic
>My code wont work even though it looks like it should.
Code:

#include <iostream>
#include <conio.h>

using namespace std;

int add (int a, int b)
{
int _answer = a + b;
}
int subtract (int c, int d)
{
int _answer = c - d;
}
int multiply (int e, int f)
{
int _answer = e * f;
}
int divide (int g, int h)
{
int _answer = g / h;
}

int main ()
{
int pick1, pick2;
cin >> pick1 >> pick2;
cout << add (pick1, pick2) << "\n";
cout << subtract (pick1, pick2) << "\n";
cout << multiply (pick1, pick2) << "\n";
cout << divide (pick1, pick2) << "\n";

getch();
return 0;
}

>The code works fine but it displays the wrong answers.
It was because you added no whitespace between the functions, therefore you saw one big string of numbers, instead of numbers seperated by whitespace.

• 03-20-2004
caroundw5h
Where are the function prototypes?
• 03-20-2004
alphaoide
Quote:

Originally posted by caroundw5h
Where are the function prototypes?
They're not needed if the function definitions are placed before main()
• 03-20-2004
caroundw5h
Quote:

Originally posted by alphaoide
They're not needed if the function definitions are placed before main()
Really? is that the same for C?
• 03-20-2004
alphaoide
Quote:

Originally posted by caroundw5h
Really? is that the same for C?
Maybe, I guess, most likely, I don't do C.
It just that when the function is called within the main, the compiler need to check if the parameters match. That's way if you define the function after the main(), the prototype is required before main().
• 03-20-2004
TWIXMIX
Yup im a noob at programming but I undersand that :D.
That completely true alpha.

Im not sure about C though
• 03-20-2004
linuxdude
You always need to in C.
• 03-20-2004
XSquared
You sure? I've never received complaints from GCC when I don't prototype my functions, even if I use -Wall.
• 03-20-2004
Tronic
Ahh no i'm sorry. I misinterpreted an error, my sincerest apologies to out to you for leading you in the wrong direction...hate it when I do that..
• 03-20-2004
TWIXMIX
Its ok Tronic.
Your code worked but I just made mine do the same thing with less code.
• 03-20-2004
XSquared
And that is a waste of a post. Try to avoid doing that, because the mods will consider it post-whoring.
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last