Helpp arrays files and functions

How to use getline in a while loop that reads names and average from a file and fill it up in array
If you want to extract a name and average from a line, use a std::istringstream.
How this will apply in your own program depends on what your code looks like so far, and on the format of the file data.
http://www.cplusplus.com/reference/sstream/istringstream/istringstream/
Im new to c++ and we cannot use std. basically is basic c++ .
what i have so far is
Two arrays in main string names and double average
I Need to write a function that reads a file and store data onto those two arrays and its stop if the array is filled or no more data from the file.
And it returns how many students are in the array

The code that i have so far is
Int getInput(string names[], double averages[])
Int count = 0;
Then opening a file

While( count < CLASS_SIZE && inputFile >> names[count] >> averages[count])
Count++;


I know i need to write getline but i dont know where so that it could read the name then the average

Last edited on
Im new to c++ and we cannot use std.
You already use it, I see this in your previous code:
 
    using namespace std;
It means when you refer to things such as string or ifstream, the program will actually use std::string, std::ifstream and so on.
But let's not dwell on that, it doesn't matter for the time being.


This part of your current code:
1
2
While( count < CLASS_SIZE && inputFile >> names[count] >> averages[count])
Count++;
might look instead like this:
1
2
3
4
5
6
7
    string line;
    while (count < CLASS_SIZE && getline(inputFile, line))
    {
        istringstream ss(line);
        if (ss >> names[count] >> averages[count])
            count++;
    }

You'll need the header #include <sstream> to use the stringstream.


Or are you asking a different question. Does the name contain spaces?
Could you show an example of the input file please.
ok heres what i have to work with.I have void in this function but i need to return an int of how many students are there (array of studentaverages i have to fill it up later in another function but right now i need to fill these two arrays)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

// Function prototypes go here:
// inputData
void inputData(string[], double[]);

const int MAX_CLASS_SIZE = 10;

int main()
{
	// Constant for maximum class size

	// Array holding students' names
	string studentNames[MAX_CLASS_SIZE];
	// Array holding the letter grades
	char studentGrades[MAX_CLASS_SIZE];
	// Array holding the averages
	double studentAverages[MAX_CLASS_SIZE];

	// Highest average in the class

	cout << "YOUR NAME" << endl << endl;

	// Input names and averages)
	// Call inputData
	inputData(studentNames, studentAverages);
	return 0;
}
void inputData(string names[], double averages[])
{

	int count = 0;
	ifstream inputFile;

	inputFile.open("StudentData.txt");// opening file
	if (!inputFile)// checking if the file opened successfully.
	{
		// displaying error if file doesnt open
		cout << "Error opening file" << endl;
		exit(1);
	}
	while (count < MAX_CLASS_SIZE && inputFile >> names[count] >> averages[count])
       count++


	inputFile.close();
}



and the file i have
Duck, Daffy
77.3
Pluto
88.0
Duck, Donald
94.3
Mouse, Mickey
80.0
Mouse, Minnie
94.3
Last edited on
Thanks. Now I see the data file I understand the question. My previous reply is not relevant.

In your latest code, you could replace these two lines
47
48
	while (count < MAX_CLASS_SIZE && inputFile >> names[count] >> averages[count])
       count++

with something a bit like this:
47
48
49
50
51
52
53
    while (count < MAX_CLASS_SIZE 
        && getline(inputFile, names[count]) 
        && inputFile >> averages[count])
    {
        inputFile.ignore(100, '\n');
        count++;
    }

Notice the ignore() at line 51. Its purpose is to consume the trailing newline (and any possible whitespace) after reading the average.


Also, function void inputData should probably be int inputData and it should return the count, in order that the rest of the code knows how many records have been read into the arrays.


Last edited on
thank you!!

line 52
inputFile.ignore(100, '\n')
what the need for 100, '\n'??

another question
using array averages i need to fill out char array grades in another function
so ]the prototype
void grades(double[], char[])
is that good do i need anything else?
Last edited on
line 52
inputFile.ignore(100, '\n')
what the need for 100, '\n'??
If we consider the input file:
Duck, Daffy
77.3
Pluto
88.0

that could be represented like this:
 
"Duck, Daffy\n77.3\nPluto\n88.0\n"
or possibly like this:
 
"Duck, Daffy\n  77.3  \n  Pluto  \n       88.0          \n"

We know there is definitely at least one character after the average figure - the newline, hence a simple ignore() is the minimum. But if there happened to be a space or more before the newline, it might not be enough. A natural caution leads me to prefer to cover the possibility of there being some extraneous character in there. In that case, ignore(100, '\n') would deal with anything from 0 to 99 spaces preceding the newline. If you yourself are certain that there will never be a need for this, then just use a single ignore(). There are other ways of dealing with unwanted whitespace, using the std::ws manipulator. You might as an alternative try inputFile >> ws; at line 51. Try these ideas out, try using neither, try one, try the other, experiment and see what happens, that's the way to find out.
http://www.cplusplus.com/reference/istream/istream/ignore/
http://www.cplusplus.com/reference/istream/ws/


in another functionjavascript:editbox1.editPreview()
so ]the prototype
void grades(double[], char[])
is that good do i need anything else?

Well, the first function, inputData has a loop which repeats a particular number of times, depending on the file content. The function grades will need to loop that same number of times. But it won't read the file all over again to count the number of times to repeat. Instead, you need to take the value of count which should be returned by the first function and pass it as a parameter to the function grades.
pretty much all done but last thing
how to add * next to people who has highest average
i already did function to find highest but how to add the * to it?

Sounds like a need for an if-statement to compare current student average with the highest value already found, and a cout << "*".
I got this is there a better way to write this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

for (int i = 0; i < students; i++)
	{
		if (averages[i] == highest)
		{
			cout << "Name: " << names[i] << "*" << endl;
			cout << "Average " << averages[i] << "\n";
			cout << "Grade :  " << grades[i] << endl;
			cout << endl;
		}
		else
		{
			cout << "Name : " << names[i] << "\n";
			cout << "Average " << averages[i] << "\n";
			cout << "Grade :  " << grades[i] << endl;
			cout << endl;
		}
	}
Last edited on
There's so much shared between the two blocks of code that it makes sense to write it just once. I've not tested this, but it should give an idea.
1
2
3
4
5
6
7
8
9
for (int i = 0; i < students; i++)
{
    string star = (averages[i] == highest) ? "*" : " ";

    cout << "Name: " << names[i] << star << endl;
    cout << "Average " << averages[i] << "\n";
    cout << "Grade :  " << grades[i] << endl;
    cout << endl;
}


Line 3 using the conditional operator ? is a convenient way to assign a result depending on whether an expression is true or false.

You could use an ordinary if-else statement but it could take three or four lines that way:
1
2
3
4
5
    string star;
    if (averages[i] == highest)
        star = "*";
    else
        star = " ";

(Well, to be fair that could be shortened considerably too).

finally done. You helped me a lot a big thanks
Topic archived. No new replies allowed.