Arrays, basic Input/Output program

Ask the user how many numbers to enter.

Enter those numbers into an Array, and then Show them.



Sample Run:

How many numbers would you like to enter? 8

Enter number 1: 5

Enter number 2: 3

Enter number 3: 10

Enter number 4: 15

Enter number 5: 25

Enter number 6: 21

Enter number 7: 99

Enter number 8: 16

The numbers you entered are: 5, 3, 10, 15, 25, 21, 99, 16

Here's what I have so far. How would I make it continue the loop based on how many numbers the user entered? Mine goes to whatever I put for "i" in my for loop?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <stdlib.h>
using namespace std;

int main ()
{

    int i;
    int numArray[4000];
    float distance[] = {10, 23.4, 20, 40.86};




    for (i = 0; i < 5; i++)
    {
        cout << "Enter number " << i+1 << " : ";
        cin >> numArray[i];
    }

    cout << "the numbers you entered: ";
    for (i=0; i< 5; i++)
    {
        cout << numArray[i] ;
        if (i<4)
            cout << ",";
    }

    cout << endl;

    system ("pause");
    return 0;
}

Last edited on
Hi,

1. Declare an array big enough for good
int numArray[4000];

2. Count the number of values the user has entered

3. Print out the content of the array
Last edited on
Hi,
Well, best place is at the point where you realise this isn't a homework site and showing us where you are having problems etc with work you have put in. Cut and paste the assignment doesn't count.

Everybody here is glad to help when you get off the starting blocks :)
How would I make it continue the loop based on how many numbers the user entered? Mine goes to whatever I put for "i" in my for loop?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <stdlib.h>
using namespace std;

int main ()
{

    int i;
    int numArray[4000]; 
    float distance[] = {10, 23.4, 20, 40.86};




    for (i = 0; i < 5; i++)
    {
        cout << "Enter number " << i+1 << " : ";
        cin >> numArray[i]; 
    }

    cout << "the numbers you entered: ";
    for (i=0; i< 5; i++)
    {
        cout << numArray[i] ; 
        if (i<4)
            cout << ",";
    }

    cout << endl;

    system ("pause");
    return 0;
}
Last edited on
Firstly, you need the variable int count = 0;. It is used to represent the number of values in the array (numArray). Input it first and you continue.

So :
> How many numbers would you like to enter?
( cin >> count; )

I do not see you did it in your code.
Last edited on
Where would it go?
From the beginning, before you input the numbers.
float distance[] = {10, 23.4, 20, 40.86};
What is the variable (distance) doing in your code? If it is not needed, you remove it.
Try this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <stdlib.h>

using namespace std;
int main ()
{
    int i, count = 0;
    int numArray[4000];
    
    cout << "How many numbers would you like to enter? : ";
    cin >> count;

    for (i = 0; i < count; i++)
    {
        cout << "Enter number " << i+1 << " : ";
        cin >> numArray[i]; 
    }

cout << "The numbers you entered: ";
    for (i=0; i< count; i++)
    {
        cout << numArray[i]; 
        if (i < count - 1)
            cout << ", ";
    }
    cout << endl;
    system("pause");
    return 0;
}
Does that help you? :)
I don't know how to make it print "Enter number ' ' as many times as the user wants. I just prints it how many times I loop it for.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <iostream>
#include <stdlib.h>
using namespace std;

int main ()
{

    int i;
    int numArray[4000];
    int count = 0;
    int numberAmount;



    cout << "How many numbers would you like to enter? "; // not sure how to do this
    cin >> numberAmount;



    for (i = 0; i < 5; i++)
    {
        cout << "Enter number " << i+1 << " : ";
        cin >> numArray[i];
    }

    cout << "the numbers you entered: ";
    for (i=0; i< 5; i++)
    {
        cout << numArray[i] ;
        if (i<4)
            cout << ",";
    }

    cout << endl;

    system ("pause");
    return 0;
}
Last edited on
@closed account 5a8Ym39o6 that works...thank you.
Last edited on
Glad it helped :)
Topic archived. No new replies allowed.