storing and saving multiple inputs in an array
Apr 15, 2016 at 3:45pm UTC
Hello,
Can someone please show me an example on how to store several lines of data into an array.
Currently, my code can store only 1 field with the data types I created.
Output:
Enter User: Alex
Enter Score:16
User Score
Alex 16
Desired Output:
Enter User:Alex
Enter Score:16
User Score
Alex 16
Add another user? y/n?
y
Enter User:John
Enter Score:17
User Score
Alex 16
John 17
Thank You
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
#include <iostream>
#include <iomanip>
#include <string>
int main()
{
for (int i = 0; i<10; i++)
{
char name[40];
char value[40];
char choice = 'z' ;
cout<< "\nEnter User:" ;
cin.getline(name, 40);
cout<< "\nEnter value for User:" ;
cin.getline(value, 40);
cout << "User" << setw(13) << "Score" <<endl;
cout << name << setw(13) << value<<endl;
cout << "\nAdd another user y/n?" ;
while (choice!='n' )
{
//some array code
}
}
return 0;
}
Apr 15, 2016 at 4:12pm UTC
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>
#include <string>
using namespace std;
int main()
{ string name[10];
string value[10];
// Enter the data
for (int i = 0; i<10; i++)
{ cout<< "\nEnter User:" ;
getline (cin, name[i]);
cout<< "\nEnter value for User:" ;
getline (cin, value[i]);
}
// Print the data
cout << "User" << setw(13) << "Score" << endl;
for (int i = 0; i<10; i++)
cout << name[i] << setw(13) << value[i] <<endl;
return 0;
}
Last edited on Apr 15, 2016 at 4:15pm UTC
Topic archived. No new replies allowed.