Please Help w/Arrays

This is a discussion on Please Help w/Arrays within the C++ Programming forums, part of the General Programming Boards category; Hi! I need some help with a VERY simple program I'm making. It's a calulator that adds two numbers and ...

  1. #1
    scary_freak_x
    Guest

    Please Help w/Arrays

    Hi! I need some help with a VERY simple program I'm making. It's a calulator that adds two numbers and uses arrays. I've just been doing this for a few days, so don't flame on me. This is the code:

    #include <stdio.h>
    #include <conio.h>
    #include <iostream.h>

    int x[3];
    x[2]=x[0]+x[1];

    int main(int argc, char *argv[])
    {
    //Prints instructions
    cout << "This is a simple calculator. All it does" << endl;
    << "is add two numbers." << endl << "Please insert "
    << "your first number, press Enter, " << endl
    << "then the second and Enter."
    //Asks for the first number and to press Enter
    cin >> x[0] >> getch();
    //Displays the plus sign
    cout << " + ";
    //Asks for the second number and to press Enter
    cin >> x[1] >> getch();
    //Displays the equal sign and the answer
    cout << " = " x[2] << endl
    //Prints instructions
    << "Please press Enter to exit this program.";
    //Waits until user presses Enter
    getch();
    //Closes the program
    return 0;
    }

    What's wrong? Could someone please help me? Happy holidays!

    -scary_freak_x

  2. #2
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    Why would you think that you would get flamed, first off....?


    I didn't look through everything, but your global declarations at the top will not work. You need to assign something to the array positions before you perform a calculation on it.

    int x[3];

    x[2] = 3;
    x[1] = 2;
    x[0] = x[2] + x[1];

    or something to that effect.

    Although... that happens before you even get to assign the numbers. Move your formula into the program for use after you grab the numbers and it will work....
    Blue

  3. #3
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    # include<iostream>
    using namespace std;

    int main()
    {

    int x[3];

    cout << "Number one: ";
    cin >> x[0];
    cout << "Number two: ";
    cin >> x[1];

    x[2] = x[0] + x[1];

    cout << "\n\n" << x[0] << " + " << x[1] << " = " << x[2] << endl;

    return 0;
    }
    Blue

  4. #4
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ROFL

    Well, don't consider this a flame = )
    The answer's pretty simple, you're not doing the calculations at the right time. You need to put the equation x[2] = (x[0] + x[1]); after you get both values, not at the beginning of the program where the values have no meaning really. = D

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21