fixing getline from returning nothing and crazy numbers?

I've been doing an input output exercise that I believe I've gotten pretty much done. But I have a significant problem. When I try to read the data it'll do three things.
At the beginning it'll sort of work.
But when I'm trying to get salary out it gives me a crazy number.
And the last getline comes off as a blank.

I believe the protocol to fix this is to use cin.ignore(). But I'm unsure of exactly how to go about using it. I would like to believe that the crazy number (Returning as either a huge negative number or simply nul) I get is from the file being read wrong because of the getline.


Would I be right in thinking that cin.ignore() would fix it? Whenever I tried using it the whole thing becomes just white space. I have no idea what's going on with the salary. Here I thought multiplying hours and payrate would be easy.

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

const string IN_NAME_EXT= ".dat.txt";
const string REPORT_NAME_EXT= ".report.txt";

int main () {

   //Declare variables
   string employeeName, jobDescription
        , inputFileName, baseFileName
        , reportFileName;

   float hoursWorked, payRate, payAmount
       , totalPayRoll, salary;

   totalPayRoll=0;

   ifstream masterFile;
   ifstream reportFileE1;

   ofstream memoFileE1;
   ofstream payRoll;

   masterFile.open("master.lst.txt");


   if (masterFile.is_open()){
      cout << "File succcesfully opened\n" << endl;
   } else {
      cout << "Unable to open the file master.lst" << endl;
   }

   //open info files
   payRoll.open("payroll.out.txt");


   //process 1st employee
   //////////////////////////////////////////////////
   //Get base name from master file
   masterFile >> baseFileName;

   inputFileName= baseFileName + IN_NAME_EXT;

   //input file name is the name of the entire file
   //base file name is the name of the employee
   //INPUT_NAME_EXT is the program extension
   //(ie .txt .doc etc)

   //Make file names from base name
   reportFileE1.open(inputFileName.c_str());
   if (reportFileE1.is_open()){
      cout << "File succcesfully opened\n" << endl;
   } else {
      cout << "Unable to open the file" << endl;
   }

   cout << fixed << setprecision(2);

   //Open files
   reportFileName= baseFileName + REPORT_NAME_EXT;
   memoFileE1.open(reportFileName.c_str());

   
   //I'm pretty sure I need cin.ignore here,
   //but where ever I put it just messes up 
   //program even more.
   getline(reportFileE1,employeeName);
   reportFileE1 >> hoursWorked >> payRate;
   getline(reportFileE1, jobDescription);

   salary = payAmount * payRate;


   cout << employeeName << endl;
   cout << hoursWorked << " " << payRate << endl;
   cout << jobDescription << endl;

   cout << endl << endl << endl;

   //Write employee report
   memoFileE1 << "Dear " << employeeName << endl
              << "Last week you worked a total of " << hoursWorked
              << " per hour." << endl << "The amount $"
              << salary  << " has been electronically deposited "
              << "your account.\n\nWe appreciate your service as "
              << jobDescription << "." << endl;

   //Cout employee report
   cout << "Dear " << employeeName << endl
        << "Last week you worked a total of " << hoursWorked
        << " per hour." << endl << "The amount $"
        << salary << " has been electronically deposited "
        << "your account.\n\nWe appreciate your service as "
        << jobDescription << "." << endl;


   //Close files
   reportFileE1.close();
   memoFileE1.close();
   //accumulate totals
   totalPayRoll= totalPayRoll+ salary;
   ///////////////////////////////////////////////////////////////


   //***Make sure to do 3 more times***

   //write totalPayRoll File
   payRoll << employeeName << " , " << hoursWorked
           << ", " << salary << endl << endl
           << "Total Payroll $" << totalPayRoll << endl;

   //****** Be sure to format to 2 decimal places *******

   //close files
   payRoll.close();
   masterFile.close();
   return (0);
   }
where do your values for payAmount and payRate come from? Are they supposed to be read in from files as well? As it stands, your program just multiplies random float values to get the salary...
Would I be right in thinking that cin.ignore() would fix it?

kinda, because you are working with file cin would be the wrong stream. you would want somefileStream.ignore();

if you enter a number for example 9 you also press enter, so the input stream looks like 9'\n'. If you us a statement such as cin >> someInt; to extract the 9, this will not extract the '\n'. Then if you use getline, witch stops inputting when it sees a '\n', getline removes the '\n', but does not extract any data. So you end up with an empty stirng.
Last edited on
Topic archived. No new replies allowed.