getline is not displaying in one line

I used getline to import EMPLOYEE's First and Last Name from a txt file. After calculating the weights ans stuff. Now when i try to write to output txt file am having the below issue in which its not putting it in one line.

***CODE***
fout << "Note: This report for " << employee << " was prepared according to the fair practice of the University." << endl ;

***THIS IS WHAT ITS PRINTING***
Note: This report for FIRSTNAME LASTNAME
was prepared according to the fair practice of the University.



i also tried like this;

***CODE***
fout << "Note: This report for " << getline(fin, employee) <<" was prepared according to the fair practice of the University." << endl ;

***THIS IS WHAT ITS PRINTING***
Note: This report for 0 was prepared according to the fair practice of the University.


this second code puts everything in one line but its showing 0 (zero) instead of the employee's first and last name

Can anyone let me what am doing wrong or do i need to provide more info ?
thanks
std::getline returns not the string read but rather a reference to the istream object.

What is most likely happening is somewhere, somehow, your employee string is being appended with a newline character. Can you show your code that uses employee?
Last edited on
Here is the complete 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
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main()
{
	string heading1, heading2, employee, supervisor, id, telephone, address ;
	double fall, spring, summer, final, per4 ;
	const double FALL_PER = 0.39, SPRING_PER = 0.37, 	SUMMER_PER = 0.24 ;
	char per0 = '0', per1 = '1', per2 = '3', per3 = '5';


	ifstream fin ;  // To read from the input file
	ofstream fout ; // To write result in output file

	fin.open("Project2_ID_Input.txt") ;   // Opening input file
	fout.open("Project2_ID_Output.txt") ; // Opening output file

	// Importing data from the input file
	getline(fin, heading1) ;
	getline(fin, heading2) ;
	getline(fin, employee) ;
	getline(fin, supervisor) ;
	getline(fin, id) ;
	getline(fin, telephone) ;
	getline(fin, address) ;
	fin >> fall ;
	fin >> spring ;
	fin >> summer ;

	// Calculate the total weight
	final = (fall * FALL_PER) + (spring * SPRING_PER) + (summer * SUMMER_PER) ;


	// Writing date to the output file
	fout << '\t' << '\t' << '\t' << '\t' <<'\t'<< " " << heading1 ;
	fout << '\t' << heading2 << endl;
	fout << endl ;
	fout <<  '\t' << '\t' << '\t' <<"       Name of the Employee: " << setw(17) << employee ;
	fout <<  '\t' << '\t' << '\t' <<"     Name of the Supervisor: " << setw(16) << supervisor ;
	fout <<  '\t' << '\t' << '\t' <<"              Employee ID #: " << setw(15) << id ;
	fout <<  '\t' << '\t' << '\t' <<"           Telephone Number: " << setw(20) << telephone ;
	fout <<  '\t' << '\t' << '\t' <<"                    Address: " << setw(46) << address ;
	fout <<  '\t' << '\t' << '\t' <<"   Fall Semester Evaluation: " << setw(10) << setprecision(2) << fixed << fall   << endl;
	fout <<  '\t' << '\t' << '\t' <<" Spring Semester Evaluation: " << setw(10) << setprecision(2) << fixed << spring << endl;
	fout <<  '\t' << '\t' << '\t' <<" Summer Semester Evaluation: " << setw(10) << setprecision(2) << fixed << summer << endl;
	fout <<  '\t' << '\t' << '\t' <<"  Final Weighted Evaluation: " << setw(10) << setprecision(2) << fixed << final  << endl;

	if (final < 70)
	fout <<  '\t' << '\t' << '\t' <<"               Salary Raise: " << setw(6) << per0 << "%\n" << "\nWarning: You'r not getting a penny from us" ;
	else if (final < 75 && final > 70)
	fout <<  '\t' << '\t' << '\t' <<"               Salary Raise: " << setw(6) << per0 << "%" << endl;
	else if (final > 75 && final < 80)
	fout <<  '\t' << '\t' << '\t' <<"               Salary Raise: " << setw(6) << per1 << "%" << endl;
	else if (final > 80 && final < 90)
	fout <<  '\t' << '\t' << '\t' <<"               Salary Raise: " << setw(6) << per2 << "%" << endl;
	else if (final > 90 && final < 100)
	fout <<  '\t' << '\t' << '\t' <<"               Salary Raise: " << setw(6) << per3 << "%\n" << "\nWah Wah Wee Wah!: You are the boss !" << endl;
	else if (final > 100)
	fout <<  '\t' << '\t' << '\t' <<"               Salary Raise: " << setw(6) << per4 << "%\n" << "\nWah Wah Wee Wah!: You are the boss !" << endl;
	else
	fout << "Invalid Entry" ;
	fout << endl ;

	fout << "Note: This report for " << getline(fin, employee) <<" was prepared according to the fair practice of the University." << endl ;
	fout << "Any discrepancies must be reported by " << employee <<  " to the supervisor, " << supervisor << "." << endl ;


	fin.close  () ; // Closing input file
	fout.close () ; // Closing output file


	return 0;
}
Testing your code as is (except for replacing the call to getline on line 67 with employee), I do not get the problem you are describing...

The only issue I see from the output is the questionable format (Did you forget to output endl on lines 41 to 45?) and the misspelling of "You're".
really ? it works you ? :s then why is it not working for me ??? maybe a compiler issue ... am using eclips

and lines 41 to 45 display correctly for me even without endl ... thank you for the missing endl and misspelling tho :)
Topic archived. No new replies allowed.