EvilZone

Programming and Scripting => C - C++ => Topic started by: Dark Nebulae on September 22, 2012, 11:41:40 AM

Title: [c++] my code is not working
Post by: Dark Nebulae on September 22, 2012, 11:41:40 AM
i wrote the following code.
Code: [Select]

#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
Code: [Select]
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.
Title: Re: [c++] my code is not working
Post by: p_2001 on September 22, 2012, 11:45:40 AM
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..
Title: Re: [c++] my code is not working
Post by: Dark Nebulae on September 22, 2012, 03:35:35 PM

Watch where you have called the functions...
Also see that the choice was never taken.. Variable..
Don't understand what u mean
Title: Re: [c++] my code is not working
Post by: p_2001 on September 22, 2012, 03:44:38 PM
Look, get an e book... Compare your code.. I won't explain this anymore.
Title: Re: [c++] my code is not working
Post by: s3my0n on September 22, 2012, 04:06:31 PM
(http://25.media.tumblr.com/tumblr_ma5vdqthnb1qdyg71o1_1280.jpg)
Title: Re: [c++] my code is not working
Post by: Daemon on September 22, 2012, 04:46:24 PM
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

Title: Re: [c++] my code is not working
Post by: Dark Nebulae on September 22, 2012, 05:49:57 PM
Suggest me some good ebooks guys. I was just trying to write a program using functions.
Title: Re: [c++] my code is not working
Post by: Xires on September 22, 2012, 11:02:08 PM
i wrote the following code.
Code: [Select]

#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'.


Code: (c++) [Select]
#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;
}
Title: Re: [c++] my code is not working
Post by: Daemon on September 23, 2012, 12:44:52 AM
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.
Title: Re: [c++] my code is not working
Post by: s3my0n on September 23, 2012, 06:23:13 AM
Good ebook imho is "C++ Primer Plus", if you can't find it I'll upload.
Title: Re: [c ] my code is not working
Post by: ajmal.josh on September 23, 2012, 06:54:45 PM
Brothers
Code: [Select]
getch();
is C style. In CPP use
Code: [Select]
cin.get();
EnjoY.
Title: Re: [c ] my code is not working
Post by: Daemon on September 23, 2012, 07:47:22 PM
Brothers
Code: [Select]
getch();
is C style. In CPP use
Code: [Select]
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.
Title: Re: [c++] my code is not working
Post by: Xires on October 03, 2012, 07:55:10 PM
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.
Title: Re: [c++] my code is not working
Post by: Dark Nebulae on October 06, 2012, 09:18:30 AM
In my compiler cin.get() doesn't work so i use getch(). I am using Borland C++ 5.02.