Arrays and nested for loops

i am trying to write a c++ program that would accept string values from user at run time. eg, firstname, lastname,matric number etc. the user should terminate input with a letter, say T. the program should display the values when D is input in the format below;
e.g
| S/N | FIRST_NAME | LAST_NAME | MATRIC_ NUMBER |
1 Joel Pius 11n07/022

and the program should terminate when the user enters E.

Cant seem to get around it.Have tried 2D arrays and nested for loops, but still confused. Please any hint or help is appreciated. Thanks
Sorry, not quite sure what you mean.

Do you mean when your inputting the data you want it to get the string between the '|'?
Sorry. I mean the program should display the output in a tabular form. i.e a column for serial number, another column for first name etc. I appreciate your response. Thanks. have to solve this problem tonight
I made a example for out putting the data into how I think you mean.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
using namespace std;

int main()
{
	string firstName, secondName;

	cout << "First Name" << endl;
	getline(cin, firstName);
	cout << "Second Name" << endl;
	getline(cin, secondName);
	cout << endl;

	cout << "Name: " << firstName << " | " << secondName << endl;

	cin.get();
	return 0;
}
Thanks a lot. But since i would be accepting values based on user input(i.e declaring a variable, say n. the program should accept input n times(i.e firstname, lastname, matric number etc) following this order
Enter firstname for student1
Enter lastname for student1
Enter matric number for student1

that is, complete details for the first student. Then it should continue in this order for n times.
I think using a nested for loop to get user input and and storing the input in a 2D array would be best, but i'm having headache now, because i have spent most of my time in front of my PC without success. Thanks for your time and effort
You should look at <iomanip> for formatting output into columns. You can use setw() and setfill() to make neat and orderly columnar output.
Thanks a lot. Any idea how to go about it?. Pretty new to C++ ptogramming
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iomanip>
#include <iostream>

using namespace std;

const char *fp[3] = { "12.75", "19.99", "2.50" };
const char *n[3] = { "Tom Jerry", "Tokyo Rose", "Horace Grimwald" };
const char *c[3]  = { "A", "B", "C" };

int main()
{
  for(int idx = 0; idx < 3; idx++)
  {
    std::setfill(' ');
    std::cout << std::left << std::setw(24) << n[idx];
    std::cout << std::left << std::setw(12) << c[idx];
    std::cout << std::right << std::setw(7) << std::fixed << fp[idx];
    std::cout << std::endl;
  }
  return 0;
}


Output:
1
2
3
4
5
6
Tom Jerry               A             12.75
Tokyo Rose              B             19.99
Horace Grimwald         C              2.50

Process returned 0 (0x0)   execution time : 0.047 s
Press any key to continue.
Last edited on
Thanks a lot of the help, guess i can move on with formatting the output from here. But i still don't get how to read in the values with nested for loops.
Topic archived. No new replies allowed.