Hello guys, i am working on this program to Add and later on Read data for Students/Teachers/Guest Teachers. Currently only Students are available for adding.
The problem is that all lines are in total mess whereever i try to Read(Get data) file for Students (1. Get data for student with ID..."). Not sure how to handle that. I'm still very new to C++ and new to programming in general and i guess i skip something little, or maybe not. I suspect my last "void displayAllS() func".
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
usingnamespace std;
void inputS();
void inputT();
void inputGT();
void displayAllS();
int main()
{
int option;
cout <<":::MENU:::\n\n";
cout<<"\n 1. Get data for student with ID..."
<<"\n 2. Get data for teacher with ID..."
<<"\n 3. Get data for guest teacher with ID..."
<<"\n 4. Add data for new student..."
<<"\n 5. Add data for new teacher..."
<<"\n 6. Add data for new guest teacher..."
<<"\n 7. Exit the program."
<<"\n *** Make your choice *** ";cin>>option;
system("cls");
switch(option)
{
case 1: cout<< "Students data: ";
displayAllS();
break;
case 2:cout<< "The current data for teachers is: ";
break;
case 3:cout<< "The current data for guest teachers is: ";
break;
case 4:cout<< "Here you can add data for new student: ";
inputS();
break;
case 5:cout<< "Here you can add data for new teacher: ";
inputT();
break;
case 6:cout<< "Here you can add data for new (Guest) teacher: ";
inputGT();
case 7:cout<< "Goodbye.";
}
}
void inputS()
{
unsignedshort ID;
string nameF;
string nameL;
string currentCourse;
unsignedshort currentPoints;
unsignedshort averageEvaluationMark;
ofstream myfile("newStudent.txt", ios::app);
system("cls");
cout << "Enter student's ID: " << endl;
cin >> ID;
cout << "Enter new student's first name: " << endl;
cin >> nameF;
cout << "Enter new student's last name: " << endl;
cin >> nameL;
cin.sync();
system("cls");
cout << "Enter new student's current course: " << endl;
cin >> currentCourse;
cout<< "Enter new student's current points: "<<endl;
cin>> currentPoints;
cout<< "Enter new student current evaluation mark: "<<endl;
cin>>averageEvaluationMark;
system("cls");
myfile << ID << ' ' << nameF << ' ' << nameL << ' ' << currentCourse << ' ' << currentPoints << ' ' << averageEvaluationMark << endl;
myfile.close();
main();;
}
void displayAllS()
{
ifstream myfile("newStudent.txt");
string line;
if (myfile.is_open()){
while (getline (myfile, line)){
cout << line << "\n";
}
}
}
I was such a dummy to forget "endl;" after "line", dear me. Now order works perfectly, each Student has his own line, the next one goes below and so on.
It's my first program that involves creating files to store data and get data back of them so my attention was all blurry.
I will edit each code i post from now on. Appreciate the help, mate, Cheers!