+ 2
7 Respostas
+ 4
#include <iostream>
using namespace std;
int main() {
int i;
string arr[3]; //line 1
cin>>i; //line 2
for(i=0;i<3;i++) // line 3
cin>>arr[i]; //line 4
cout<<arr[i]; //line 5
return 0;
}
Line 1- no need for size specification when using string data type in c++.
Line 2- input arr, not i
Line 3 - when using string data type, you can get the size of the string as arr.size() in c++.
Line 4 & 5 - these should be inside {} as they are inside for loop.
So the final code
#include <iostream>
using namespace std;
int main() {
int i;
string arr;
cin>>arr;
for(i=0;i<arr.size();i++)
{
cin>>arr[i];
cout<<arr[i];
}
return 0;
}
+ 3
Your cin >> i is useless because after cin you assign 0 to it in for loop.
Also you didn't use { } in for loop. When there are 2 or more statements in the loop. Use for(...) { /*code*/ }
+ 1
Thanks alot sandeep .this code is very helpful
0
I want to input three subjects marks
0
Using array
0
Anas kayani if you want to store three subjects. You don't need to let user input i. Take cin >> i out.
And you need { } after for loop to make sure the output is also in for loopm
0
Ok