Thread: Another Simple Problem

  1. #1
    For Narnia! Sentral's Avatar
    Join Date
    May 2005
    Location
    Narnia
    Posts
    719

    Talking Another Simple Problem

    Here is my first addition program:
    Code:
    // Addition Program
    #include <iostream>
    using namespace std;
    int main()
    {
        int num1, num2, sum;
        cout << "Enter first number to be added:";
        cin >> num1;
        cout << "Enter second number to be added:";
        cin >> num2;
        cout << "Your sum is " << sum << endl;
        cin >> sum;
        sum = num1+num2;
        cin.get();
        return 0;
    }
    It compiles and runs, but when it gives me an answer it is always 2293672. Please help me. If you could, explain why it was giving me the same answer.
    Videogame Memories!
    A site dedicated to keeping videogame memories alive!

    http://www.videogamememories.com/
    Share your experiences with us now!

    "We will game forever!"

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    36
    You have some basic errors

    here is the correct code:

    Code:
    // Addition Program revised
    #include <iostream>
    using namespace std;
    int main()
    {
        int num1, num2, sum;
        cout << "Enter first number to be added:";
        cin >> num1;
        cout << "Enter second number to be added:";
        cin >> num2;
    
        sum = num1+num2;
    
        cout << "Your sum is " << sum << endl;
        
        /* remove this line */ cin >> sum;  
        
        
        cin.get();
        return 0;
    }
    What happened earlier is that the line that does the math was after printing the output. You need to put that line before printing the output. You also used cin after the output. I have no idea why u did that.

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    722

    Remember.. all code executes sequentialy.. not back and forward

  4. #4
    Registered User
    Join Date
    Oct 2004
    Posts
    15
    To avoid problems like that, it is MUCH easier to declare what it is before you get into the code.

    Code:
    #include <iostream>
    using namespace std;
    int main
    {
    int num1,num2,sum
    
    sum = num1 + num2
    
    ...
    
    }
    that way you don't have to define something in the middle of your program...

    You do know that cin is asking for input from the user.

    Code:
    cin>>sum;
    is telling the user that THEY need to define what sum is.
    Last edited by Mornic_Programm; 05-30-2005 at 12:46 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 02:45 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 09:10 PM
  4. Simple Initialization Problem
    By PsyK in forum C++ Programming
    Replies: 7
    Last Post: 04-30-2004, 07:37 PM
  5. Simple OO Problem
    By bstempi in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2004, 05:33 PM