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;
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
usingnamespace 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;
}
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.
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.
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.
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
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.
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).
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.