Messy way to read Data from File, help out

Jun 18, 2016 at 8:33pm
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".

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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace 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()
{

 unsigned short ID;
 string nameF;
 string nameL;
 string currentCourse;
 unsigned short currentPoints;
 unsigned short 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";
        }
    }
}

Last edited on Jun 19, 2016 at 6:57am
Jun 19, 2016 at 6:37am
Hi,

check the following code

1
2
3
4
5
6
7
8
9
10
11
12
13
// Example program
#include <iostream>
#include <string>
using namespace std;

int main()
{
  std::string name,name2;
  
  getline (cin, name);
  cin>>name2;
  cout<<name<<endl<<name2;
}


akki lucky
akku lucku
akki lucky
akku 


can you see why cin>> is not the best choice?

Hope it helps

PS: Please edit and use code tags
http://www.cplusplus.com/articles/jEywvCM9/

PPS: welcome to cplusplus.com :)
Last edited on Jun 19, 2016 at 7:45am
Jun 19, 2016 at 7:34am
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!



Topic archived. No new replies allowed.