0
Hey there, i just made a simple program in c++, but i am getting an error!HELPPP!
Well, I just made a program to input an array but I am getting an error... Couldnt understand it... #include <iostream> #include <string> using namespace std; int main() { int a [5]; getline(cin, int a[5]); cout<<"The entered array is"<<a[5]; return 0; } The error is 'possible use of int before int' (in the getline line).
5 Answers
+ 3
I would like to point out few errors :
1. You have declared an integer array of size 5 and you are trying to read the input into 5th index a[5] when the greatest index is only 4.
2. getline() is a string library function that expects a string. It reads string or line of string from input stream.
Here you are passing an integer and the way you've passed is also incorrect. We don't write func(int x) ; while calling. Simply use func(x) ;
3. I think what you're trying here is to read the entire array of int . If so do sth like this
for (int i = 0; i < 5; i++)//note index upto (5 - 1)
{
cin >> a[i] ;
}
If you're trying to read the string. For this you should declare a as string. Then this can be achieved using
cin >> a;
If you're trying to read a sentence i.e including whitespace use:
getline (cin, a) ; //ignores whitespaces
+ 1
you dont need to write
getline(cin, int a[5])
instead you should
getline(cin, a[5])
+ 1
for (int i=0;
i<sizeof(array)/sizeof(int);
i++)
{
cin >> a[i];
}
the number of elements in an array is sizeof(array) /sizeof(datatype of array)
0
That gives a syntax error
getline(cin, a[5]) đ
0
You can't output all array elements in this way. You need a loop for this.
This should help:
https://code.sololearn.com/c0nobHTD8f71/?ref=app