C++ Array
Hi everyone, I have a question on my code. English is my second language sorry if that's confusing to you.
when I run my code I have to enter 11 numbers in order to get out of the loop,
than it will show 10 numbers.
so , how can I enter only 10 numbers and show 10 numbers. Thanks for your help.
Example my output:
Enter 10 integers (separated by a space): 1 2 0 6 5 8 9 8 7 5 1
You entered: 1 2 0 6 5 8 9 8 7 5
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 39 40 41 42 43 44 45
|
#include<iostream>
using namespace std;
const int ARRAY_SIZE = 10;
void getData(int list[], int &size);
void printData(const int list[], int size);
int main()
{
int num[ARRAY_SIZE];
int size = 0;
cout << "Enter 10 integers (separated by a space): ";
getData(num, size);
printData(num, size);
return 0;
}
void getData(int list[], int &size)
{
int index = 0;
int getIntergers;
cin >> getIntergers;
while (index < 10) {
size++;
list[index] = getIntergers;
cin >> getIntergers;
index++;
}
}
void printData(const int list[], int size)
{
int number = 0;
cout << "\n\tYou entered: ";
for (int index = 0; index < size; index++)
{
cout << list[index] << " ";
}
}
| |
const int ARRAY_SIZE = 10;
It should be :
const int ARRAY_SIZE = 11;
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 39 40 41 42 43 44
|
#include<iostream>
using namespace std;
const int ARRAY_SIZE = 10;
void getData(int list[], int &size);
void printData(const int list[], int size);
int main()
{
int num[ARRAY_SIZE];
int size = 0;
cout << "Enter 10 integers (separated by a space): ";
getData(num, size);
printData(num, size);
return 0;
}
void getData(int list[], int &size)
{
int index = 0;
int getIntergers = 0; // <--
//cin >> getIntergers;
while (index < 10) {
cin >> getIntergers; // <--
size++;
list[index] = getIntergers;
index++;
}
}
void printData(const int list[], int size)
{
//int number = 0; // <--
cout << "\n\tYou entered: ";
for (int index = 0; index < size; index++)
{
cout << list[index] << " ";
}
}
| |
Last edited on
Thanks kemort. It works now. :)
Topic archived. No new replies allowed.