C++ inputting multiple strings

Hi All,

We've been given the exercise below and I'm stumbling at the first block because we have to take in 20 student names and I don't know how to store them! :frowning:

I know that I can create (initialize) 20 different char arrays but this seems wrong somehow...

What's the best way to store these string inputs?

Thanks, p.

Name Mark Grade
===== ===== =====
Sam 78 Distinction
Joe 54 Pass
���..
  1. The attempts at a solution (include all code and scripts):
#include "stdafx.h"
#include <iostream>
#include <string.h>
using namespace std;

int main ()
{
	char aName[15];
	int aMark[15], iIndex;
	char cName;

	cout << "Enter Name and Mark: " << endl;

	for (iIndex = 0; iIndex < 15; iIndex++)
	{
		cout << "Enter Name: ";
		cin >> aName;


	}

	// test output
	for (iIndex = 0; iIndex < 6; iIndex++)
	{
		cout << "Name Array output: " << aName[iIndex] << endl;
		cout << "Mark Array output: " << aMark[iIndex] << endl;
	}

	return 0;
}

(obviously the above doesn't work...)

  1. School (University) and Course Number:

Farnborough Tech - UK (C++ practitioners diploma)... Note this is NOT coursework it's just one of the examples we're given to work thru...

Thanks!

Quick and dirty hint:

#include <iostream>
#include <string.h>
using namespace std;

struct student {
	char name[80];
	int mark;
} grade[20];


int main ()
{

	int i;
	
	for (i = 0; i < 20 ; i++)
	{
	
		cout << "Student name" << endl;
		cin >> grade.name;
		cout << "Mark: " << endl;
		cin >> grade.mark;
	}
	
}

Ooh I thought structs may help but I haven't used them yet - I'll have a play with this and let you know how I get on :smiley:

Thanks! :smiley:

---------- Post updated at 04:41 PM ---------- Previous update was at 04:09 PM ----------

Sorted! :smiley:

Thanks for your help jp2542a!

Here's my final code:

#include "stdafx.h"
#include <iostream>
#include <string.h>
using namespace std;

struct student {
	char name[80];
	int mark;
	char gradeText[12];
} grade[5];


int main ()
{

	int i;
	
	for (i = 0; i < 5 ; i++)
	{
	
		cout << "Student name (" << i << ") ";
		cin >> grade.name;
		cout << "Mark: ";
		cin >> grade.mark;

		if ( grade.mark <= 100 && grade.mark >= 70 )
		{
		
			strcpy(grade.gradeText, "Distinction");

		} else if ( grade.mark <= 69 && grade.mark >= 55 ) {

			strcpy(grade.gradeText, "Merit");

		} else if ( grade.mark <= 54 && grade.mark >= 45 ) {

			strcpy(grade.gradeText, "Pass");

		} else {

			strcpy(grade.gradeText, "Fail");

		}

	}

	cout << endl;
	cout << "\t\tName\t\tMark\t\tGrade" << endl;
	cout << "\t\t====\t\t====\t\t=====" << endl;

	for (i = 0; i < 5; i++)
	{
	
		cout << "\t\t" << grade.name << "\t\t" << grade.mark << "\t\t" << grade.gradeText << endl;

	}
	
	return 0;
}