Author Topic: [c++] my code is not working  (Read 4092 times)

0 Members and 1 Guest are viewing this topic.

Offline Dark Nebulae

  • Peasant
  • *
  • Posts: 117
  • Cookies: -79
  • Unleash the Hacker within you
    • View Profile
[c++] my code is not working
« 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.
Trust is like a piece of paper.Once it is crumbled,it can never be perfect.

Offline p_2001

  • Royal Highness
  • ****
  • Posts: 684
  • Cookies: -64
    • View Profile
Re: [c++] my code is not working
« Reply #1 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..
« Last Edit: September 22, 2012, 11:46:48 am by p_2001 »
"Always have a plan"

Offline Dark Nebulae

  • Peasant
  • *
  • Posts: 117
  • Cookies: -79
  • Unleash the Hacker within you
    • View Profile
Re: [c++] my code is not working
« Reply #2 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
Trust is like a piece of paper.Once it is crumbled,it can never be perfect.

Offline p_2001

  • Royal Highness
  • ****
  • Posts: 684
  • Cookies: -64
    • View Profile
Re: [c++] my code is not working
« Reply #3 on: September 22, 2012, 03:44:38 pm »
Look, get an e book... Compare your code.. I won't explain this anymore.
"Always have a plan"

Offline s3my0n

  • Knight
  • **
  • Posts: 276
  • Cookies: 58
    • View Profile
    • ::1
Re: [c++] my code is not working
« Reply #4 on: September 22, 2012, 04:06:31 pm »
Easter egg in all *nix systems: E(){ E|E& };E

Offline Daemon

  • VIP
  • Baron
  • *
  • Posts: 845
  • Cookies: 153
  • A wise man fears a gentle mans anger
    • View Profile
Re: [c++] my code is not working
« Reply #5 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

« Last Edit: September 23, 2012, 12:48:19 am by Daemon »
This lifestyle is strictly DIY or GTFO - lucid

Because sexploits are for h0edays - noncetonic


Xires burns the souls of HF skids as a power supply

Offline Dark Nebulae

  • Peasant
  • *
  • Posts: 117
  • Cookies: -79
  • Unleash the Hacker within you
    • View Profile
Re: [c++] my code is not working
« Reply #6 on: September 22, 2012, 05:49:57 pm »
Suggest me some good ebooks guys. I was just trying to write a program using functions.
« Last Edit: September 22, 2012, 05:51:56 pm by $ »
Trust is like a piece of paper.Once it is crumbled,it can never be perfect.

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: [c++] my code is not working
« Reply #7 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;
}
« Last Edit: September 22, 2012, 11:08:27 pm by Xires »
-Xires

Offline Daemon

  • VIP
  • Baron
  • *
  • Posts: 845
  • Cookies: 153
  • A wise man fears a gentle mans anger
    • View Profile
Re: [c++] my code is not working
« Reply #8 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.
This lifestyle is strictly DIY or GTFO - lucid

Because sexploits are for h0edays - noncetonic


Xires burns the souls of HF skids as a power supply

Offline s3my0n

  • Knight
  • **
  • Posts: 276
  • Cookies: 58
    • View Profile
    • ::1
Re: [c++] my code is not working
« Reply #9 on: September 23, 2012, 06:23:13 am »
Good ebook imho is "C++ Primer Plus", if you can't find it I'll upload.
Easter egg in all *nix systems: E(){ E|E& };E

ajmal.josh

  • Guest
Re: [c ] my code is not working
« Reply #10 on: September 23, 2012, 06:54:45 pm »
Brothers
Code: [Select]
getch();
is C style. In CPP use
Code: [Select]
cin.get();
EnjoY.
« Last Edit: September 23, 2012, 06:56:26 pm by ajmal.josh »

Offline Daemon

  • VIP
  • Baron
  • *
  • Posts: 845
  • Cookies: 153
  • A wise man fears a gentle mans anger
    • View Profile
Re: [c ] my code is not working
« Reply #11 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.
This lifestyle is strictly DIY or GTFO - lucid

Because sexploits are for h0edays - noncetonic


Xires burns the souls of HF skids as a power supply

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: [c++] my code is not working
« Reply #12 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.
-Xires

Offline Dark Nebulae

  • Peasant
  • *
  • Posts: 117
  • Cookies: -79
  • Unleash the Hacker within you
    • View Profile
Re: [c++] my code is not working
« Reply #13 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.
Trust is like a piece of paper.Once it is crumbled,it can never be perfect.