EvilZone
Programming and Scripting => C - C++ => : Dark Nebulae September 22, 2012, 11:41:40 AM
-
i wrote the following code.
#include<iostream>
#include<conio>
int mult(int a,int y);
int sum(int a,int y);
void main()
{
int a,y;
int b;
cout<<"Hello, Enter two numbers: ";
cin>>a>>y;
cout<<"\n1. Add \n 2.Multiply\n";
if(b==1)
{
cout<<"The sum is: "<<sum;
}
else
{
cout<<"The product is: "<<mult;
}
getch();
}
int sum(int a,int y)
{
return a+y;
}
int mult(int a,int y)
{
return a*y;
}
And am getting the output as
Hello, enter two numbers: 4
5
1.Add
2.Multiply
The product is:0x004011bd
And when i enter my choice the screen goes.
Help me.
-
Lol lol lol lol... Ok.. I must not laugh, but still i am..
Watch where you have called the functions...
Also see that the choice was never taken.. Variable..
-
Watch where you have called the functions...
Also see that the choice was never taken.. Variable..
Don't understand what u mean
-
Look, get an e book... Compare your code.. I won't explain this anymore.
-
(http://25.media.tumblr.com/tumblr_ma5vdqthnb1qdyg71o1_1280.jpg)
-
Okay, since these jokers don't have much else to say :P
Better appreciate this, i got onto my computer just so I could help you do this as my phone is a bitch to read code on.
Second off, why do you call int mult(a, y) then int sum(a, y)?
what is the getch() for? i really see no need for it.
and the if statement.... you have it saying if b == 1 do something, but how can b == 1 if you don't do anything to b before that? also, this program is really too short to worry about having a seperate function, its making things too complicated and it seems as though you would be better off learning a bit more about c++ before trying to make something simple like math into a more complicated program. I'm not trying to be rude here, but remember, K.I.S.S (keep it simple, stupid)
*edit*
didn't think about this code being part of a larger program, so there might be reason to use functions here, but for all readers out there. try to stick to KISS. not only will it make your life easier, it'll make your program faster (in most cases, not just functions and shizz)
*/edit*
I'll let you see what you can do now to fix it, please update this with your working one so we can see what you finally did. good luck mate
-
Suggest me some good ebooks guys. I was just trying to write a program using functions.
-
i wrote the following code.
#include<iostream>
#include<conio>
int mult(int a,int y);
int sum(int a,int y);
void main()
{
int a,y;
int b;
cout<<"Hello, Enter two numbers: ";
cin>>a>>y;
cout<<"\n1. Add \n 2.Multiply\n";
if(b==1)
{
cout<<"The sum is: "<<sum;
}
else
{
cout<<"The product is: "<<mult;
}
getch();
}
int sum(int a,int y)
{
return a+y;
}
int mult(int a,int y)
{
return a*y;
}
Your code is attempting to return the address from which the chosen function is stored. You are not passing calling the function correctly nor passing any arguments. Those arguments are required. As well, you are not requesting a value for 'b'.
#include <iostream>
#include <conio>
int mult(int, int);
int sum(int, int);
using namespace std;
void main(void) {
int a, y, b;
cout << "Hello, Enter two numbers: ";
cin >> a >> y;
cout << "1. Add\n2. Multiply" << endl;
cin >> b; // do not forget to retrieve the user's choice
if (b == 1) {
cout << "The sum is: " << sum(a, y) << endl; // notice that 'sum()' is called with two arguments, as it is declared.
} else {
cout << "The product is: " << mult(a, y) << endl; // this is known as 'passing' arguments to a function
}
getch(); // this is not the best way to do this; you may consider researching better alternatives
return;
}
int sum(int a, int y) {
return a + y;
}
int mult(int a, int y) {
return a * y;
}
-
good work xires, I wish i had the time this morning to have gone into more detail like that. and i edited my original post as i wasn't all that awake and i noticed a few things in there that are unneeded. >.<
@OP, as far as ebooks go. Id check out the Absolute C++ books in the ebook section, thats what I used to learn it.
-
Good ebook imho is "C++ Primer Plus", if you can't find it I'll upload.
-
Brothers
getch();
is C style. In CPP use
cin.get();
EnjoY.
-
Brothers
getch();
is C style. In CPP use
cin.get();
EnjoY.
that clears it up, thank you!
and OP, that means you don't need cin.get() here. just use cin >> as it's numbers, not a string.
-
cin.get() is not the same as cin.getline(). cin.get() is meant to grab a single character so it's perfectly appropriate. It should be noted, however, that one may need to 'flush' input buffers & clear any error flags to make this work properly every time.
-
In my compiler cin.get() doesn't work so i use getch(). I am using Borland C++ 5.02.