Thread: OMG can some please help?

  1. #1
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57

    Unhappy OMG can some please help?

    My code wont work even though it looks like it should.
    Could some one please help me?
    I'm learning some functions so this will help me practice.

    Code:
    #include <iostream>
    #include <stdlib.h>
    
    using namespace std;
    
    int answer;
    
    int add (int a, int b)
    {
        answer = a + b;
        return (answer);
    }
    int subtract (int c, int d)
    {
        answer = c - d;
        return (answer);
    }
    int multiply (int e, int f)
    {
        answer = e * f;
        return (answer);
    }
    int divide (int g, int h)
    {
        answer = g / h;
        return (answer);
    }
    
    int main ()
    {
        int pick1, pick2;
        cin >> pick1, pick2;
        cout << add (pick1, pick2);
        cout << subtract (pick1, pick2);
        cout << multiply (pick1, pick2);
        cout << divide (pick1, pick2);
        
        system("pause");
        return 0;
    }
    Please help.
    THanks

  2. #2
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    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
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  3. #3
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57
    THe code works fine but it displays the wrong answers.

    Im using the newest version of Dev c++

  4. #4
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57
    ok its works now geeze.
    in the cin part I had :

    cin >> pick1, pick2

    should have had :

    cin >> pick1 >> pick2

    lol

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    >My code wont work even though it looks like it should.
    Code:
    #include <iostream>
    #include <conio.h>
    
    using namespace std;
    
    int answer;
    
    int add (int a, int b)
    {
        int _answer = a + b;
        return _answer;
    }
    int subtract (int c, int d)
    {
        int _answer = c - d;
        return _answer;
    }
    int multiply (int e, int f)
    {
        int _answer = e * f;
        return _answer;
    }
    int divide (int g, int h)
    {
        int _answer = g / h;
        return _answer;
    }
    
    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.

    EDIT:Nvm, you are correct in your answer.
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  6. #6
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Where are the function prototypes?
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  7. #7
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    Originally posted by caroundw5h
    Where are the function prototypes?
    They're not needed if the function definitions are placed before main()
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  8. #8
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Originally posted by alphaoide
    They're not needed if the function definitions are placed before main()
    Really? is that the same for C?
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  9. #9
    unleashed alphaoide's Avatar
    Join Date
    Sep 2003
    Posts
    696
    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().
    source: compsci textbooks, cboard.cprogramming.com, world wide web, common sense

  10. #10
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57
    Yup im a noob at programming but I undersand that .
    That completely true alpha.

    Im not sure about C though

  11. #11
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    You always need to in C.

  12. #12
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You sure? I've never received complaints from GCC when I don't prototype my functions, even if I use -Wall.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  13. #13
    Registered User
    Join Date
    Mar 2004
    Posts
    220
    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..
    OS: Windows XP Pro CE
    IDE: VS .NET 2002
    Preferred Language: C++.

  14. #14
    Programmer in Training TWIXMIX's Avatar
    Join Date
    Feb 2004
    Posts
    57
    Its ok Tronic.
    Your code worked but I just made mine do the same thing with less code.

  15. #15
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    And that is a waste of a post. Try to avoid doing that, because the mods will consider it post-whoring.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Omg Last Time I Swear!
    By unejam2005 in forum C++ Programming
    Replies: 3
    Last Post: 12-13-2005, 12:49 AM
  2. OMG!!! You guys!!!
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-04-2003, 10:17 PM
  3. omg lmao go to this link, it's funnay!
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 12-07-2002, 06:12 AM