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.
#include <iostream>
#include <iomanip>
usingnamespace 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;
}
//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
#include <iostream>
#include <iomanip>
usingnamespace 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.
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."
#include <iostream>
#include <iomanip>
usingnamespace 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.
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.
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.
#include <iostream>
#include <iomanip>
usingnamespace 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;
}
}
}
#include <iostream>
#include <iomanip>
usingnamespace 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;
}
}
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;
}