Writing functions and using arrays.

I'm working on a program for my C++ class. The assignment uses a file that includes student names and three test scores.

For example:
Name
Test1
Test2
Test3
Name
Test1
Test2
Test3

My teacher wrote the main part of the program for us and we are to write the functions. We are to write a program that will read the student names and totals, sort the names and totals in decending order by totals and print the sorted list.

This is what he wrote for main (we are not supposed to change anything here):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>#include <fstream>#include <iomanip>#include <string>#include <conio.h>using namespace std; int main(){    string names[50];    int totals[50], count;    count = ReadArrays(names,totals);    SortArrays(names,totals,count);    PrintArrays(names,totals,count);    getch();    return 0;}#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <conio.h>
using namespace std;

int main()
{
    string names[50];
    int totals[50], count;
    count = ReadArrays(names,totals);
    SortArrays(names,totals,count);
    PrintArrays(names,totals,count);
    getch();
    return 0;
}


We are to write the ReadArrays, SortArrays and PrintArrays functions. This is where I'm having some trouble, as I'm really struggling with this class.

First, I'm trying to write the ReadArrays function, then move to the PrintArray function and lastly, write the SortArrays function (this is what he said to do.)

He gave us a loop to use in the ReadArrays function:

Here is the loop structure you will need to read the file STUDENT.TXT. Your program will be different, though. Instead of a single string called "name", you will use an array of strings.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
ifstream StudentFile;    string name; // This sample uses a single string.                 // You will want an array of strings instead!      int test1, test2, test3;        .        .        .     // process students until end of file    while(getline(StudentFile,name))  // you'll need an array instead of "name"!    {        StudentFile >> test1;  // assume that if we got a name, 3 test scores DO follow        StudentFile >> test2;            .            .            .        StudentFile.ignore(10,'\n');  // gets the carriage return after third number    }    ifstream StudentFile;
    string name; // This sample uses a single string.
                 // You will want an array of strings instead!


    int test1, test2, test3;
        .
        .
        .
    
    // process students until end of file
    while(getline(StudentFile,name))  // you'll need an array instead of "name"!
    {
        StudentFile >> test1;  // assume that if we got a name, 3 test scores DO follow
        StudentFile >> test2;
            .
            .
            .
        StudentFile.ignore(10,'\n');  // gets the carriage return after third number
    }


I've inserted this, but am not sure what to change to make it run....This is what I have for code so far

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
#include <iostream>#include <fstream>#include <iomanip>#include <string>#include <conio.h>using namespace std; int ReadArrays(string, names[], int totals[]);   int main(){    string names[50];    int totals[50], count;    count = ReadArrays(names,totals);    //SortArrays(names,totals,count);    //PrintArrays(names,totals,count);    getch();    return 0;} int ReadArrays(string, names[], int totals[]){	ifstream StudentFile;    names[]; 	int test1, test2, test3;     while(getline(StudentFile,names[]))    {        StudentFile >> test1;          StudentFile >> test2;		StudentFile >> test3;         StudentFile.ignore(10,'\n');  	}}#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <conio.h>
using namespace std;

int ReadArrays(string, names[], int totals[]);



int main()
{
    string names[50];
    int totals[50], count;
    count = ReadArrays(names,totals);
    //SortArrays(names,totals,count);
    //PrintArrays(names,totals,count);
    getch();
    return 0;
}

int ReadArrays(string, names[], int totals[])
{
	ifstream StudentFile;
    names[];

	int test1, test2, test3;
    
    while(getline(StudentFile,names[]))
    {
        StudentFile >> test1;  
        StudentFile >> test2;
		StudentFile >> test3;
            
        StudentFile.ignore(10,'\n');  
	}
}


I really would appreciate some help with writing these functions...I'm trying desperately to pass this class so I can finish my degree (I'm not majoring in Computer Science, but have to take this class as part of a science credit...)
You haven't told ifstream on line 25 what file you want to read.

Your declaration of ReadArrays has an extra comma in it between strings and names.

Line 26 does nothing (except generate a compile error).

Also, line 30 you have to read into a specific element of the names array... you didn't specify which one
(put a number -- the index -- between the brackets).

I'm guessing you don't want line 36.
Thanks for the help. Line 36 was given by my teacher, so we're supposed to use it.
He also told us what the declaration line was and that was what it was :\
So, I made a few changes...would it be something like this?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int ReadArrays(string, names[], int totals[])
{
	ifstream ("Desktop://student.txt");

	int test1, test2, test3;
    
    while(getline(StudentFile,names[0]))
    {
        StudentFile >> test1;  
        StudentFile >> test2;
		StudentFile >> test3;
            
        StudentFile.ignore(10,'\n');  
	}
}
You probably need to use a counter variable to indicate which part of "names" you want to use, since as of now you will simple be overwriting the first part (0) over and over. You'll also need to eventually add up the test scores as well and put them in the other array.

Also...
(I'm not majoring in Computer Science, but have to take this class as part of a science credit...)


Tell me about it...sometimes the requirements just seem...stupid.
Hahaha yes, very stupid :\ I feel like I can't even comprehend this and now I have about a month to raise my grade 5% so I can pass and the credit will transfer to my university *sigh*

That being said, I'm sorry if some of my questions seem stupid...I've been trying to get help all semester from everyone, including my instructor, but still haven't been able to pull my grade up...ughhh!!

so for a counter variable, would that be something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int ReadArrays(string, names[], int totals[])
{
	ifstream ("Desktop://student.txt");

	int test1, test2, test3, count;
    
    while(getline(StudentFile,names[0]))
    {
        StudentFile >> test1;  
        StudentFile >> test2;
		StudentFile >> test3;
		count++;
            
        StudentFile.ignore(10,'\n');  
	}
}


also, I'm getting an error with getline and names array... :(
And studentfile in the while loop is giving me an error, too
You need to put more effort into this. Line 1 has an extra comma between string and names which makes no sense. You never initialized count and also the idea is to use the count as the offset into the names array. It does you know good to increment an uninitialized variable. You never use it anyway. Hopefully your file has less than 51 names because you have done nothing to prevent the while loop from ending other than to read the entire file until there is no more data. if you are going to use a names array then you need to make sure that there are no out of bounds accesses.
I will try to change some things around, but the first function is to simply read the file, the second is to print the file and the third is to put them into decending order...
Okay, so I've done some work and the first function works :) (According to my teacher, I just need to put something else in the array, but I'm not sure what...) He said: Your function is only using array spot [0] in each array. That means each student you read replaces the previous name that you stored in that position. The first student’s name should go in names[0]. The second student should go in names[1], etc.

I understand what he is saying, but I'm not sure how to do this.

I'm now trying to work on the next function, PrintArrays. My teacher also said: you want to return the number of students that you successfully read in that function. That becomes “count” in main.

This is where I'm absolutely stuck. Since the file is read in the ReadArrays function, do I need to open it again the PrintArrays function? I'm not sure how to set the counter up. I will try some things and see what I come up with, but here is what I have for code (the fixed ReadArrays function)

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
53
54
55
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <conio.h>
using namespace std;

int ReadArrays(string names[], int totals[]);

int PrintArrays(string names[], int totals[], int count);



int main()
{
    string names[50];
    int totals[50], count;
    count = ReadArrays(names,totals);
    //SortArrays(names,totals,count);
    PrintArrays(names,totals,count);
    getch();
    return 0;
}

int ReadArrays(string names[], int totals[])
{
	ifstream StudentFile;
	StudentFile.open("Desktop://student.txt");

	int test1, test2, test3;
    
    while(getline(StudentFile, names[0]))
    {
        StudentFile >> test1;  
        StudentFile >> test2;
		StudentFile >> test3;

		totals[0] = test1 + test2 + test3;
            
        StudentFile.ignore(10,'\n');  
	}

	return 0;
}

int PrintArrays(string names[], int totals[], int count)
{
	





	return count;
}
Your function is only using array spot [0] in each array.
he means that: while(getline(StudentFile, names[0])) and that totals[0]
rewrite it to

for(int i =0; (i < max) && getline(StudentFile, names[i]); ++i) -> totals[i]


your 'ReadArrays' must return count which is i while 'PrintArrays' shouldn't

use count in your 'PrintArrays'

1
2
3
4
5
6
7
8
void PrintArrays(string names[], int totals[], int count)
{
  cout << "Count " << cout << endl;
  for(int i = 0; (i < count); ++i)
  {
     cout << "Student " << names[i] << " Total " << totals[i] << endl;
  }
}
It's not tested + rewrite it according to your needs
Thank you for your help. I now realize I need a for loop in ReadArrays.

He told me that PrintArrays returns the count.

I now have, this:

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

int ReadArrays(string names[], int totals[]);

int PrintArrays(string names[], int totals[], int count);



int main()
{
string names[50];
int totals[50], count;
count = ReadArrays(names,totals);
//SortArrays(names,totals,count);
PrintArrays(names,totals,count);
getch();
return 0;
}

int ReadArrays(string names[], int totals[])
{
ifstream StudentFile;
StudentFile.open("Desktop://student.txt");

int test1, test2, test3, max, i=0;

while(getline(StudentFile, names[0]))
{
for (int i = 0; i < max; i++)
StudentFile >> test1;
StudentFile >> test2;
StudentFile >> test3;

totals[i] = test1 + test2 + test3;

StudentFile.ignore(10,'\n');
}

return 0;
}

int PrintArrays(string names[], int totals[], int count)
{
cout << "Count " << count << endl;

for(int i = 0; (i < count); i++)
{
cout << "Student " << names[i] << " Total " << totals[i] << endl;
}

return count;
}

But, all that prints to the screen is:

Count 0
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <conio.h>
using namespace std;

int ReadArrays(string names[], int totals[]);
//The ReadArrays function receives the names and totals from the file student.txt. It does not return anything to main.

int PrintArrays(string names[], int totals[], int count);
//The PrintArrays function receives the names, totals and student count from the file students.txt. It returns the student count.


int main()
{
string names[50];
int totals[50], count;
count = ReadArrays(names,totals);
//SortArrays(names,totals,count);
PrintArrays(names,totals,count);
getch();
return 0;
}

int ReadArrays(string names[], int totals[])
//The ReadArrays function receives the names and totals from the file student.txt. It does not return anything to main.
{
ifstream StudentFile;
StudentFile.open("Desktop://student.txt"); //Opens the file student.txt.

int test1, test2, test3, max=100, i=0;
//test1, test2, test3 are variables for the three test scores of each student.
//max is a variable for the maximum number of student names from the file.
//i is a loop variable.

for (int i = 0; i < max; i++) //Initializes i to zero. As long as i is less than max, keep looping and adding the three test scores.
{
StudentFile >> test1;
StudentFile >> test2;
StudentFile >> test3;

totals[i] = test1 + test2 + test3;

StudentFile.ignore(10,'\n');
}

return 0;
}

int PrintArrays(string names[], int totals[], int count)
//The PrintArrays function receives the names, totals and student count from the file students.txt. It returns the student count.
{
cout << "Count " << count << endl;

for(int i = 0; (i < count); i++)
{
cout << "Student " << names[i] << " Total " << totals[i] << endl;
}

return count;
}

I have now changed it to this, but am still getting the same output
Please use the code tags

But, all that prints to the screen is:

Count 0
that is because your 'ReadArrays' still returns 0 while it should return i

max is not supposed to be 100! It should be the array size of names and totals in order to not go out ouf bounds.

your latest version of 'ReadArrays' does not read the names anymore.

if your file is organized so that any value is on it's on line then use getline for each value and convert the string to int
Topic archived. No new replies allowed.