Problem with getline function

When I run the main file below, the getline function copies into input_string variable a weird character that somehow overwrites a string output before it, such that "input" is overwritten by "@@@@@" as in below:

@@@@@_string : $$1+2^(2*(4-2))^2

The code that does this is below the only comment in the code. What is going on and how do I fix this?

Is getline recording a character that is not character on keyboard?


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
#include <iostream>
#include <string>
#include <fstream>
#include "test.h"


using namespace std;


int main()
{
	test calculator;
	string input_string;

	char cont;

	cout << "Make sure you have entered an infix expression into \"infixFile.txt\". " << endl
	     << "Enter 'c' to use the calculator: " << endl;

	cin >> cont;

	while( cont == 'c' || cont == 'C')
	{



		ifstream instream;
		ofstream outstream;

		instream.open("infixFile.txt");

		outstream.open("resultFile.txt");

		for(unsigned int line = 1; instream.good(); line++)
		{

 			getline(instream, input_string);

	               //'@' characters overwrite "input"!
			cout << "input_string : " << "$$" << input_string << "@@@@@" << endl;

			try
			{ 
				cout << "\nLine "<< line << " result is: " << calculator.CalculatePostfix(calculator.ConvertInfixToPostfix(input_string)) << endl;



			}



			catch(PreconditionViolatedException& PVE)
			{
				cout << PVE.what() << endl;

			}



		}

		cout << "\nMake sure you have entered your infix expression into \"infixFile.txt\" and save. " << endl
				 << "Enter 'c' to calculate your infix expression: ";
		cin >> cont;
		cout << endl;
	}



  return 0;
}  
Last edited on
Is infixFile.txt created on Windows machine and you are trying to run tis code on Linux one?

It looks like you string contains CR (carriage return) character which is possible in indicated case as Windows uses CRLF as endline and Linux uses just LF
infixFile.txt created on Windows machine and you are trying to run tis code on Linux one?

It looks like you string contains CR (carriage return) character which is possible in indicated case as Windows uses CRLF as endline and Linux uses just LF


Yea, I am using CYGWIN on windows, which allows me to use LINUX commands. But I created the text file in windows. I will try and create the text in a different software when I get home.
Last edited on
Topic archived. No new replies allowed.