Help with functions and fstream

I am still rather new to C++ and am finding it hard to understand and utilize the following concepts:

Questions:

1. I’m trying to call the studentInformation( ) in the int main( ) and I don’t know how.

2. Once I’ve collected the information in the studentInformation( ) I want to store that information on a text file. I think I know how to write it to a text file using ostream. But does ostream accept strings, then integers, then a double, output like the example below?

(row and column wouldn't show of course, also the -- are only to help format the question better)

------ Column 1:-- Column 2:-- Column 3:-- Column 4:
row 1: First name , Last name , A-Number , Grade
row 2: First name , Last name , A-Number , Grade

So if I wanted to retrieve this stored information with my roster( ) using istream and probably two loops creating a 2D array. Would it interpret it like this?

line 1 column 1 = first name,
line 1 column 2 = last name,
line 1 column 3 = a number,
line 1 column 4 = grade,
then rinse and repeat.
line 2 column 1 = first name,


I have researched these topics and find what I believe to be the answers, a bit daunting, and have a hard time understanding them. So please don’t think me lazy.

Thanks for the time

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

class student
{
public:
	int roster()
	{
		cout << "--Roster--" << endl;
		// Pull the information for each student from text file
		// Run information from text file through studentInformation()
		// I want the information in that format.
		// If its really hard to try and run it through that function I
		// could make an easier function specifically for formatting.
		// Print out roster for each student (All the information in the
		// text file)

		return 0;
	}
	int studentInformation()
	{	
		string firstName;
		string lastName;
		int aNumber;
		double grade;

		cout << "--Student information--" << endl;
		cout << "First Name: " << endl;
		getline(cin,firstName);
		cout << "Last Name: " << endl;
		getline(cin,lastName);
		cout << "A-Number: A" << endl;
		cin >> aNumber;
		cout << "Grade: " << endl;
		cin >> grade;

		return 0;
	}	

};

int main()
{
	int choice = 0;
	cout << "---Student Report v1.0---" << endl;
	cout << "1. Roster" << endl;
	cout << "2. Add New Student" << endl;
	cout << "3. Input Grade" << endl;
	cout << "4. Student Search" << endl;
	cout << "5. Exit" << endl;
	cin >> choice;

	switch(choice)
	{
	case 1: //Call and use the roster()
		break;
	case 2: //Call and use the studentInformation()
		break;
	case 3: //Call and use the inputGrade()
		break;
	case 4: //Call and use the studentSearch()
		break;
	case 5: cout << "Thank you for using Student Report v1.0" << endl;
			return 0;
		break;
	default: cout << "Invalid Selection." << endl;
		cout << "Please enter a menu option between 1 and 5." << endl;
		break;
	}

	return 0;
}




hi,
i'm not familiar with ostream/istream, but i think i can help with your first problem.

to use a function that's in a class, you have to declare a variable of that class first - like you do with a structure. so, for example, you might want to put

1
2
3
student cExample;

cExample.studentInformation();


instead of

 
student.studentInformation()


which i know i do sometimes.

you can also use dynamic memory:

1
2
3
student cExample = new Student;

cExample -> studentInformation();


Topic archived. No new replies allowed.