Thread: Noob Script Help ~ Simple Script Error

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    3

    Noob Script Help ~ Simple Script Error

    Hey guys,

    New to the forumn, and have found great information about C++ esp from "Jumping into C++"... im very new to this, so bare with me... below is a very simple script i have created though i have an issue with it, the answer to the math equation is waaay off!!! lol...

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        int x,z;
        int c = x+z;
        cout << "Please enter a number x: ";
        cin >> x;
        cout << "Please enter number z: ";
        cin >> z;
        cout << "x + z = " << c << "\n"; 
    }
    It can be executed with no problems but when the answer comes out... it comes out to be like -203102302130291 or 234923042 etc... test it out if you want, could someone please advise?

    Much Appreciated

    ~ WA

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should move the calculation to after the input has been read.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    3
    Eureka! Thanks a bunch...

    This is what i did:

    Code:
    int main()
    {
        int x,z;
    
        cout << "Please enter a number x: ";
        cin >> x; // "cin >>" means accept input from the user!
        cout << "Please enter number z: ";
        cin >> z;
    
        int c = x + z;
        cout << "x + z = " << c << "\n"; // "<<" means user enters data and ">>" means computer outputing data! and remember to give each variable its own "<<" insertion or you will get an error
    }
    Is their a specific reason why it should be placed in that order and is it the only way?? because it looks a little messy now..(i might just be overdoing the tidiness of the codes)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by WiredAnomaly
    Is their a specific reason why it should be placed in that order and is it the only way?
    Well, is there a specific reason why you bought the eggs to make the omelette and then made the omelette rather than make the omelette first and then bought the eggs to make it?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Code also specifies the order in which things are done. Statements at the top of a function body are executed before statements below. In your code, it is necessary to input the values of x and z before calculating c.

    You seem to be assuming that the statement "c = x+z" sets up some magical machinery so the value of c changes whenever x changes or whenever y changes. That assumption is completely incorrect.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    Registered User
    Join Date
    Oct 2013
    Posts
    3
    Well, is there a specific reason why you bought the eggs to make the omelette and then made the omelette rather than make the omelette first and then bought the eggs to make it? http://im.cprogramming.com/images/smilies/wink.png
    Interesting. But what if you had eggs already and just needed to buy replacement eggs for the ones you're already using.. thinking outside the box here. lol

    Jokes aside, i appreciate the help.

    Code also specifies the order in which things are done. Statements at the top of a function body are executed before statements below. In your code, it is necessary to input the values of x and z before calculating c.
    Oh! haha i get it now, hahaha i don't like to get answers without knowing how it was done... that my friend... was specifically what i was questioning, thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 06-16-2011, 05:17 AM
  2. [MAC]Simple script, Complex error...
    By kokomodrums in forum C Programming
    Replies: 0
    Last Post: 02-24-2010, 07:25 AM
  3. Replies: 0
    Last Post: 02-05-2009, 06:59 PM
  4. Simple Script causing error
    By CougarElite in forum C++ Programming
    Replies: 9
    Last Post: 12-22-2004, 10:26 PM
  5. help me with this simple script!
    By ryansutton in forum C++ Programming
    Replies: 2
    Last Post: 09-09-2002, 08:42 PM