Extremely confused - quick answer would be awesome

So I have an assignment for class, dont want to explain the whole thing but im trying to write some info to a file, then input from that file to another output file. The problem im running into is that on the 2nd output file, whenever I type in someones full name, it messes up the whole file, but if I use only the first name, works fine. Im assuming it has to do with getline but Ive been searching for the past 4 hours and cant find anything and I think im just confusing myself more. This assignment is due tomorrow so a quick help would be awesome. Heres the code.


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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//Header
//
//
//
//
#include<iostream>
#include<fstream>
#include<iomanip>
#include<string>
//include any other libraries 
using namespace std;

int main()
{
    const int SIZE = 21;
    float const Fweigh=0.21;
    float const Spweigh=0.22;
    float const Suweigh=0.23;
    float const Yweigh=0.34;
    
    int count = 1;
    int num = 1;
    char name[SIZE];
    char id[SIZE];
    float feval, speval, sueval, ceval, final;
    
    ofstream ofile;
    ifstream ifile;
    ofile.open ("EricInputData.txt");
   //Start of the DO Loop 
    do
    {
      cout << "Enter Employee's name: \n";
      cin.getline(name, SIZE);
      ofile << name << endl;
      
      cout << "Enter Employee ID#: \n";
      cin.getline(id, SIZE);
      ofile << id << endl;
      
      cout << "Enter Fall Evaluation: \n";
      cin >> feval;
      ofile << feval << endl;
      
      cout << "Enter Spring Evaluation: \n";
      cin >> speval;
      ofile << speval << endl;
      
      cout << "Enter Summer Evaluation: \n";
      cin >> sueval;
      ofile << sueval << endl;
      
      cout << "Enter Comprehensive Evaluation: \n";
      cin >> ceval;
      ofile << ceval << endl;
      cin.ignore();
      
      num++;
      
    }
    //This closes the DO Loop. 
      while (num < 10);
    //This closes the output file.
    ofile.close();
    
    ifile.open ("EricInputData.txt");
    ofile.open ("EricOutputFile.txt");
    
   
    
    
    while (count < 10)
    {
          ifile >> name;
          ofile << right << setw(40) << "Name of the Employee: "<< left << setw(15) << name << endl;
          
          ifile >> id;
          ofile << right << setw(40) << "Employee ID #: "<< left << setw(15) << id << endl;
          
          ifile >> feval;
          ofile << right << setw(40) << "Fall Semester Evaluation: "<< left << setw(15) << fixed << setprecision(2) << feval << endl;
          
          ifile >> speval;
          ofile << right << setw(40) << "Spring Semester Evaluation: "<< left << setw(15) << fixed << setprecision(2) << speval << endl;
          
          ifile >> sueval;
          ofile << right << setw(40) << "Summer Semester Evaluation: "<< left << setw(15) << fixed << setprecision(2) << sueval << endl;
          
          ifile >> ceval;
          ofile << right << setw(40) << "Comprehensive Annual Evaluation: "<< left << setw(15) << fixed << setprecision(2) << ceval << endl;
          
          final = (feval*Fweigh)+(speval*Spweigh)+(sueval*Suweigh)+(ceval*Yweigh);
          
          ofile << right << setw(40) << "Final Weighted Evaluation: "<< left << setw(15) << fixed << setprecision(2) << final << endl;
          
            if (final >= 90)
            {ofile << right << setw(40) << "Quality Grade: " << left << setw(10) << "Excellent" << endl;
            ofile << right << setw(40) << "Recommendation: " << left << setw(10) << "High Raise" << endl;}
  
            else if (final >= 80)
            {ofile << right << setw(40) << "Quality Grade: " << left << setw(10) << "Good"<<endl;
             ofile << right << setw(40) << "Recommendation: " << left << setw(10) << "Good Raise"<<endl;}
  
             else if (final >= 70)
             {ofile << right << setw(40) << "Quality Grade: " << left << setw(10) << "Satisfactory"<<endl;
             ofile << right << setw(40) << "Recommendation: "<< left << setw(10) << "No Raise"<<endl;}
  
             else if (final < 70)
             {ofile << right << setw(40) << "Quality Grade: " << left << setw(10) << "Unsatisfactory"<<endl;
             ofile << right << setw(40) << "Recommendation: "<< left << setw(10) << "Fire"<<endl;}
             ofile << endl;
          
          count++;
    }
    
    
    
    ifile.close();
    ofile.close();
    
    
    
    system("pause>nul");
    return 0;
}
Hello vanvan329,

what does it mean
it messes up the whole file
?

This line cin.getline(name, SIZE); takes a whole line (incl. space)
This line ifile >> name; takes a word only without space
So whenever you enter space you'll see different things.

Means:
cin.getline(name, SIZE); // <---- name = "abc def"
leads to
1
2
ifile >> name; // <---- name = "abc"
ifile >> id; // <---- id = "def" 



I'd suggest that you use std::getline() instead of cin.getline(). With std::getline() you can pass a std::string without limitation.
Last edited on
ok thanks for the response. im going to try this right now. What I meant by it messes up the whole file was that putting in a name with a space in it (first and last name) it would mess up the output file by putting the first half in name and the second half in the id row.

so why doesnt ifile >> name; produce the whole thing? just wondering.
So thats not really working. its inside of a loop so now its looping the problem throughout the entire output file
vanvan329 wrote:
so why doesnt ifile >> name; produce the whole thing? just wondering.
because the delimiter to recognize the end of a string/number is space by default while getline() has '\n'
getline() is not working...
Coming back to the original post - Only two lines are required to make it work:
1
2
3
4
5
6
7
8
9
10
11
12
    while (count < 10)
    {
          //ifile >> name;//get rid of this line 
          ifile.getline(name,21);//and use this one insted
          
            //..........All other lines the same as before
    

             ifile.getline(name,21);   //** Add this line   as the last line in the loop before count++        
          
          count++;
    }



//we need the extra line ifile.getline(name,21); //** Add this line as the last line in the loop before count++ at the bootom of the loop, because each set of employee
details is followed terminated by a carriage return and we need to get rid of it before getting
the next emplyee's name
Topic archived. No new replies allowed.