Array input and output help

I have to write a code that lets you input up to 100 numbers and when press 0 when your done entering your numbers. Then it tells you how many numbers you entered and tells you what numbers you entered. I am having trouble with figuring out how to write the code to cout the "you have entered x numbers." Any help would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int num_list[100];
    int y;


    cout << "Please provide some integers (up to 100). Enter 0 to exit." << endl;
    //Loop that obtains the numbers
    for(int x = 0; x < 100; x++)
    {
        cout << "Enter number " << x << ":" << endl;
        //obtains the numbers and stores
        //them in the array num_list
        cin >> num_list[x];
    }

    cout << "You entered " <<  << "numbers." << endl;
}
Try this for you FOR loop...

1
2
3
4
5
6
7
8
9
10
//use variable belonging to main() scope rather than for() scope
//Check in for loop that your using 100 numbers && that the previous number is not 0
for(y = 0; y<100 && num_list[y-1]!=0; y++){
    //Get numbers and store in array num_list
    cout << "Enter number " << y << ":" << endl;
    cin >> num_list[y];
}
//y now equals the number of times looped
//output y
cout << "You entered " << y << "numbers." << endl;


If you've been using x in your output it shouldn't even compile because the variable does not exist once you exit the for loop, also it appears you have no way to exit this loop if value is 0
When I use this code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int num_list[100];
    int y;


    cout << "Please provide some integers (up to 100). Enter 0 to exit." << endl;
    //Loop that obtains the numbers
    for(int y = 0; y < 100 && num_list[y-1] !=0; y++)
    {
        cout << "Enter number " << y << ":" << endl;
        //obtains the numbers and stores
        //them in the array num_list
        cin >> num_list[y];
    }

    cout << "You entered " << y << " numbers." << endl;
}


It says I entered 78 numbers no matter what. It should say how many numbers you entered which should be between 1 and 100.
y is already declared on line 8, remove that and you should not have issues anymore. You should also start your for loop at 1 since 0-1 is out of bounds for an array.

Edit, oops, leave line 8, remove the int from your for loop. I didn't realize you were using y outside of your loop.
Last edited on
Thanks for the help. I changed the code. When I enter zero to exit, it is including that as an entered number in the total and it should, ie 1,2,3,4,0 shourld cout as "You entered 4 numbers." and right now it couts as "You entered 5 numbers."

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int num_list[100];
    int y;


    cout << "Please provide some integers (up to 100). Enter 0 to exit." << endl;
    //Loop that obtains the numbers
    for(y = 0; y < 100 && num_list[y-1] !=0; y++)
    {
        cout << "Enter number " << y << ":" << endl;
        //obtains the numbers and stores
        //them in the array num_list
        cin >> num_list[y];
    }

    cout << "You entered " << y << " numbers." << endl;
    cout << "Here they are:" << endl;
    cout << "Index " << y << ": " << endl;
}


I know the last part where it is suppose to cout all the numbers that were originally input is wrong. I just want to get the first part right before I move one.
Last edited on
With what you're doing, the only way to do it would be to subtract 1 from y after the for loop if the array's position at y-1 is 0. Remember, if all 100 numbers are entered, the last number won't be 0, and you don't want to subtract from that.

Edit: You also didn't fix your out of bounds error for your array.
Last edited on
When I change the y=0 to y=1 the output changes from number 0 to number 1 and I need to start at number 0. I changed it a little. It now reads the right output but when I enter either 1 number or two numbers it will automatically end and read out all 100 numbers in the indexes. I need it to let me enter as many numbers as I want and then enter 0 to stop and then say how many numbers I entered and the show the same numbers that were entered.

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
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int num_list[100];
    int y;


    cout << "Please provide some integers (up to 100). Enter 0 to exit." << endl;
    //Loop that obtains the numbers
    for(y = 0; y < 100 && num_list[y] !=0; y++)
    {
        cout << "Enter number " << y << ":" << endl;
        //obtains the numbers and stores
        //them in the array num_list
        cin >> num_list[y];
    }

    cout << "You entered " << y << " numbers." << endl;
    cout << "Here they are:" << endl;
    if(num_list[y] == 0)
    {
        for(y = 0; y <= 100; y++)
        {
            //prints all the original numbers to the screen
            cout << "Index " << y << ": " << num_list[y] << endl;
        }
    }

}
This is what you were trying to do, I believe. It also prevents checking an out of bounds value for the array:
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
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
   // Create array to hold numbers
   int num_list[100];
   // Create variable to hold the number of numbers
   int amountOfNumbers = 0;
   // Inform user to input up to 100 numbers and enter 0 to exit
   cout << "Please provide some integers (up to 100). Enter 0 to exit." << endl;
   // Loop 100 times
   for(int i = 1; i <= 100; i ++) {
      // Inform the user to enter number i
      cout << "Enter number " << i << ":";
      // Store the user's input in num_list at index i
      cin >> num_list[i];
      // Check to see if they entered 0
      if(num_list[i] == 0)
         // If so, break out of the loop
         break;
      // Increment amountOfNumbers by 1
      amountOfNumbers ++;
   }

   // Display the number of numbers entered
   cout << "You entered " << amountOfNumbers << " numbers." << endl;
   // Inform the user of their numbers
   cout << "Here they are:" << endl;
   // Loop through the indexs until you reach the number of variables entered
   for (int i = 0; i < amountOfNumbers; i ++) {
      //prints all the original numbers to the screen
      cout << "Index " << i << ": " << num_list[i] << endl;
   }
}
Thanks. That is basically what I was trying to do. I had to tweak a few things from that but it worked. Thanks again for your help. I appreciate it.
This is essentially the same as my code however i made 1 mistake in the for loop
 
for(y=0; y<100 && num_list[y-1] !=0; y++)

As people have pointed out this creates an array out of bound error and does not fix your program counting the 0 as a number.
I meant to subtract the 0 from the num_list array but simply place this in the wrong area, so my code will work fine like this...
1
2
3
4
5
6
7
8
for(y=0; y<100 && num_list[y] !=0; y++){
    //contents of for loop
}
y -= 1;
cout << "You entered " << y << "numbers." << endl;
for(int i=0; i<y; i++){
    cout << "Index " << i << ": " << num_list[i] << endl;
}
Last edited on
Topic archived. No new replies allowed.