Thread: How to let the user input values for an array...

  1. #1

    How to let the user input values for an array...

    I learned to program in BASIC, and have just started useing C++. Having a little trouble with the syntax.

    I am writing a basic program that averages numbers. In the first part, the user inputs a whole number greater than 1, which is stored as the "number" variable. Then...

    int array(number);
    cout<<"\nEnter a number: ";
    cin>> //What do I put here?

    I want the user to enter any number, and have it stored as the first slot (x) in the array, then have another line that increases the value of x by one, checks to see if it equals "number," and loop back around if it doesn't.

    Something like this, but I don't know the correct syntax:

    int array(number);
    cout<<"\nEnter a number: ";
    cin>> array(x)
    If (x==number)
    goto the rest of the program
    goto the start and let the user input another number.

    I heard that using goto statements wasn't good programming, and that there were other ways of doing it. What are these ways?


  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    why would you need an array at all?

    here's my suggestion:
    #include <iostream>
    int main() {
      int t=0,x=0,c=0;
      cout << "type a list of numbers, and end the list with a negative number" << endl;
      while (x >= 0) {
        t += x;
        cout << "Insert # " << ++c << endl;
        cin >> x;
      cout << "Out of " << c-1 << " numbers, the average is " << (double)t / (double)(c-1) << endl;

  3. #3
    Registered User
    Join Date
    Aug 2002
    hey, even if u use an array, then u can use this code:

    void main()
    int array[100];
    // 1008 is for the maximum no of the numbers that user can enter
    int n;
    cout<<"Enter how many numbers u want to enter: ";
    for(int i=0;i<n;i++)
    cout<<"Enter the Number: ";
    // the rest is the average which can be easily done.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing values from Edit Box into an array
    By E_I_S in forum C++ Programming
    Replies: 10
    Last Post: 06-05-2008, 06:24 AM
  2. User input for array size.
    By coolmoniker in forum C++ Programming
    Replies: 27
    Last Post: 08-24-2006, 11:34 PM
  3. Replies: 11
    Last Post: 03-24-2006, 11:26 AM
  4. array and user input
    By enlinux in forum C Programming
    Replies: 2
    Last Post: 07-20-2003, 02:08 AM
  5. comparing user input
    By lambs4 in forum C Programming
    Replies: 5
    Last Post: 12-15-2002, 10:28 AM