student management system

I am trying to create a student management system as my first software; i want to keep the data in a file;

work on getting and storing the names to file has been successful BUT READING THE NAMES FROM THE FILE AND PRINT TO THE CONSOLE SCREEN HAS NOT BEEN SUCCESSFUL;

I HAVE READ AND WATCH MANY TUTORIALS on this and i believe i am doing things exactly as i have seen in the tutorials.

my code has NO ERROR OR WARNING but i don't get desired result;

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
  #include<iostream>
#include<fstream>
#include<string>
#include<sstream>
using namespace std;

int main(){
	int num,n;
	string input;
	cout<<"ENTER NUMBER OF STUDENT IN CLASS ";

	getline(cin,input);
	stringstream(input)>>num;

	string student[num];
	string names[num];
		ofstream myfile;
		myfile.open("database.txt",ios::app);
	for (int i= 0; i<num; i++)
	{
	cout<<"ENTER STUDENT "<<i+1<<" NAME"<<endl;
	getline(cin,input);
	student[i]=input;
	ofstream myfile;
	myfile<<student[i]<<"\n";
	}
	myfile.close();
	//getting and storing the name is succesful



/* my problem begins from here
there no errors or warning but i dont get desired result

eventually i am going to wrap up the codes separately in a function
 and use switch to call them. so the user get to call the which function he wants,
whether to input student data or view student data 
but for now i want this part to work out first */


//reading from the file into an array;

	ifstream myfiles("database.txt");
	
	 for (n=0; n<num;n++){
             myfiles>>names[n];	//read file line by line into array
	            }
system("cls");
cout<<"NAMES OF STUDENT\t\t\t"<<"INDEX NUMBER\t\t\t"<<"GRADE";
		cout<<"\n--------------------------------------------------------------------\n";
cout<<names[n]<<endl;// display array content



	
cin.get();
cin.ignore();
return 0;
}
Last edited on
string names[num]; is not legal in standard c++, though most compilers allow it. array sizes technically need to be know at compile time. You could use a vector...
vector<string> names(num); //ok and nothing else needs to change there.

you can directly read. no need to read and copy.
getline(cin, names[i]);

cout<<names[n]<<endl; this is the big one. first, its outside the loop, so N is almost certainly out of bounds. Second, its not in the loop, so it only writes 1 record. write the header before the loop, and move the writer into the loop:

system("cls");
cout<<"NAMES OF STUDENT\t\t\t"<<"INDEX NUMBER\t\t\t"<<"GRADE";
cout<<"\n--------------------------------------------------------------------\n";

for (n=0; n<num;n++)
{
myfiles>>names[n];
cout<<names[n]<<endl;
}

if you keep your {} braces aligned and on lines by themselves it is much more obvious when you have this kind of problem. Putting them misaligned makes it harder to see, very very hard with nested loops/conditions/etc.
Last edited on
you're awesome.
but problem not entirely solved;
the names appears blank;

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
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#include<vector>
using namespace std;

int main(){
	int num;
	cout<<"ENTER NUMBER OF STUDENT IN CLASS ";
	cin>>num;
	string student[num];
	vector<string> names(num);
string input;
	
		ofstream myfile;
		myfile.open("database.txt",ios::app);
	for (int i= 0; i<num; i++)
	{
	cout<<"ENTER STUDENT "<<i+1<<" NAME"<<endl;
	cin>>input;
student[i]=input;
	ofstream myfile;
	myfile<<student[i]<<"\n";
	}
	myfile.close();
	
	ifstream myfiles("database.txt");
	
system("cls");
cout<<"NAME OF STUDENTS\t\t\t"<<"INDEX NUMBER\t\t\t"<<"GRADE";
cout<<"\n-------------------------------------------------------------------------------------\n";

for (int n=0; n<num;n++){
myfiles>>names[n];
cout<<names[n]<<endl;
}
	cin.get();
	cin.ignore();
		return 0;
}
Last edited on
The same problem that jonnin mentioned still exists for string student[num]. Change it to a vector.

1
2
	ofstream myfile;
	myfile<<student[i]<<"\n";

This doesn't make sense. You already defined an ofstream called myfile on line 15.
I assume you want to delete line 21.

But also, it's good to check that a file is successfully open whenever you make a file stream.
Line 26+:
1
2
3
4
5
6
ifstream myfiles("database.txt");
// add this:
if (!myfiles)
{
    cout << "Error: Cannot open database.txt" << endl;
}

Last edited on
thanks its working now!
you guys are awesome
Last edited on
hello guys i am stacked again in my own quest.
i want to delete the content of the existing file when the program run newly.
i have read the article from http://www.cplusplus.com/doc/tutorial/files/. i know how to do that. but its not working;
you guys are awesome from beginning.
please check out the code and tell me what i am doing wrong and how to correct it.
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
#include<sstream>
#include<vector>
using namespace std;

int main(){
	char grade;
	int num;
	string input;
	double examscore;
	cout<<"ENTER NUMBER OF STUDENT IN CLASS ";
	cin>>num;
	vector<string> name(num);
	vector<string> names(num);
	vector<double> mark(num);
	vector<double> marks(num);
	
		ofstream namebase;
//this is how l learnt from http://www.cplusplus.com/doc/tutorial/files/
//but it doesn't truncate.
//i know i didn't need to specify ios::out, but i thought that might be the problem

		namebase.open("names database.txt",ios::out | ios::app | ios::trunc);//this the line of problem
	for (int i= 0; i<num; i++)
	{
		int a=i+1;
	cout<<"ENTER STUDENT "<<a<<" NAME: ";
	
	 cin>>name[i];
	
	cout<<"ENTER STUDENT "<<a<<" EXAM SCORE: ";
	cin>>mark[i];
	
	namebase<<name[i]<<"\n";
	}
	namebase.close();
	
	ofstream markbase;
	markbase.open("marks database.txt",ios::app);
		for (int i= 0; i<num; i++)
	{
	markbase<<mark[i]<<"\n";
	}
	markbase.close();
	
	ifstream readnames("names database.txt");

if (!readnames)
{
    cout << "Error: Cannot open names database.txt" << endl;
}

	for (int n=0; n<num;n++)
	{
		readnames>>names[n];
	}

	ifstream readmarks("marks database.txt");

if (!readmarks)
{
    cout << "Error: Cannot open mark database.txt" << endl;

}
	for (int k=0; k<num;k++)
	{
	readmarks>>marks[k];
	}
	system("cls");
	cout<<setw(15)<<"ID"<<setw(50)<<"NAME"<<setw(25)<<"EXAM SCORE"<<setw(15)<<" GRADE\n";
	cout<<setw(85)<<"---------------------------------------------------------------------------------------------------------------\n";
	
	
	for(int j=0 ;j<num;j++)
	{
		if(marks[j]>=0&&marks[j]<=30)
			{
		grade='F';
			}
		else if(marks[j]>30&&marks[j]<=40)
			{
			grade='E';
			}
		else if(marks[j]>40&&marks[j]<=50)
			{
			grade='D';
			}
		else if(marks[j]>50&&mark[j]<=70)
			{
			grade='C';
			}
		else if(marks[j]>70&&marks[j]<=85)
			{
			grade='B';
			}
		else if (marks[j]>85&&marks[j]<=100)
			{
			grade='A';
			}
		else { cout<<"Mark is out of range of grading system";grade=' ';}
	
		cout<<setw(15)<<"ID NOT YET SET"<<setw(50)<<names[j]<<setw(23)<<marks[j]<<setw(15)<<grade<<endl;
	}

	cin.get();
	cin.ignore();
		return 0;
}
Last edited on
i want to delete the content of the existing file when the program run newly.

Just open it like this:
ofstream namebase("names database.txt");
Delete line 26, and replace line 21 with the above.

That will clear its text by default.
I'm not sure. But Ganado does opening the file like that overwrite over the existing data or does it clear the text? I think it just overwrites..

In that case I think he must use std::remove and then what you said.
does opening the file like that overwrite over the existing data or does it clear the text? I think it just overwrites..

No it doesn't overwrite the file it truncates the file effectively erasing the file contents.

you can call open directly with more flags to append, or the like.
file.open(name, ios::app); //looks like this, if I got the flag name right

Ah okay, I checked again. It seems for ofstream namebase("names database.txt"); it does erase the file contents but on using fstream namebase("names database.txt"); the same cannot be said.

When using fstream it just overwrites so if there was 11111 in the file and I output a 0 then the file would have 01111 instead of just 0.

Also another thing about fstream, when you try to open a file that doesn't exist, ofstream creates that file, but fstream doesn't do that.

Why does fstream differ with ofstream in these two ways?

Why does fstream differ with ofstream in these two ways?

Because the open modes are different.

std::ofstream() always opens the file for output and by default it truncates the file contents and creates an empty file if the file doesn't exist. You can alter the behavior of the opening of the file using the app, ate, trunc, the file will always be an output file.

std::fstream() by default opens the file using std::ios::in | std::ios::out. And since the file is an "input" file the file must exist and the file contents are preserved. You can alter a fstream to be either in or out, or both in and out and you can also set the open mode (app, ate, trunc). But if you do alter the open mode you must also be sure to specify the desired in/out mode as well since unlike an ofstream this parameter is overwritten by any modifications of the modes.

Edit: And be careful when altering the open modes to insure you have a valid mode, there are incompatible modes possible when altering the modes.
Last edited on
Just open it like this:
 
ofstream namebase("names database.txt");

Delete line 26, and replace line 21 with the above.
That will clear its text by default.
thanks this works:

ANOTHER PROBLEM
line 32 when i replace with
getline(cin,name[i])
the program skips the part of taking the names and goes straight to taking in the scores

but i want to be able to take in full name at once;
please how do i go about that one? anyone who can, please help
Last edited on
Perhaps, my comments will not be entirely on the topic, but I want to speak out. I’m just starting my way in programming and learning HTML so far everything seems easy. But when I see your tasks, I simply cannot imagine how one can find or understand what the error is. Guys, how do you handle it? Can you have tips for a newbie? In order to understand programming better, I had to forget a little about basic study and, fortunately, a narrative essay writing service https://edubirdie.com/narrative-essay-writing-service it helps me to keep my level of learning.
Last edited on
HTML is not a programming langauge it's a scripting language. It's for people who want to design their websites, it's not used for programming.

It's not bad to know HTML though.

This thread may be overwhelming, and it should be when you see it for the first time. But once you've learnt a little about programming languages then it will make more sense. It's like.. *fails at producing an example*

If you want to learn about C++ (some say it's not a good programming language for beginners though) then you can look up Youtube (and maybe eventually buy a book or something, advising is not my thing).

Or here, there's a gist of C++ on this site itself: http://www.cplusplus.com/doc/tutorial/

By the way can I ask how you came across this page? Just curious. ;]
Last edited on
HTML is not a programming langauge it's a scripting language.
It's a markup language. That's what the ML stands for. HTML can contain scripts (javascript) but is not a scripting language in itself

Bra599, you have to start small. Just a program that is one or two lines of code. Practice the simplest form of what you're trying to do, and then build from there. If you ever run into an error you don't know how to solve, google the error and whittle down your program until you understand where the error is coming from.
spotting errors comes from making mistakes which comes from coding. After years of coding, you will have made mistakes yourself and can spot similar issues in code from your experiences. Same for doing things... you will do something, and it will work but be a big mess or really slow or whatever, and you will redo it and learn a better way, which leads to seeing how to approach problems better. Training, reading code from other people, researching your algorithm or problem, all that stuff helps, but experience from writing code yourself over time, with a lot of hard work, is critical to getting there.
Topic archived. No new replies allowed.