i need help with my code.

basically i have a file that contains a bunch of random romans and integers. the file will follow one of 2 formats. it will either look like this:
I
X
4
5

or it will look like this:
I 1
X 10
4 IV

the second format has one space between the roman numeral and their respective integer value. not when the program runs i need to check the file to see if they have been converted and follow the second format, if they havent been converted and follows the first format i need to convert them. my teacher said to just check first line to see what format it follows and to assume rest of file is like that. i made a test file to test it out but it acts funky. my idea was that if i run into a space between the roman and its integer value that means it has been converted but if i run into a \n that means it went to a newline and hasn't been converted. please tell me what i am doing wrong.

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
int main()
{
	int checker = 0;
	string check;
	ifstream tester("numbers.txt", ios::out);




	while (getline(tester, check, ' '))//put the first line into check stopping at a whitespace
	{
		if (tester.peek() != '\n')//not positive how peek() actually work so please explain. i thought that it looks at the character after my getline,
			// so if that character is a space it doesn't == a newline so it is converted. 
		{

			
			cout << " " << "converted" << endl;
		}

		else
		{
			cout << check << " " << "not converted " << endl;
			checker = 1;
			
			
		}

		break;
	}
line 10 essentially keeps appending chars to 'check' until a delimiter is found.
It will go multiple lines until it find the delimiter.
In this txt file for example:
aba
Xaa 10
check will be abaXaa

In your case, peek() will only return '\n' if there's a whitespace just before the escape char and that whitespace is the first of the line.

line 28 breaks the loop too early ?
Last edited on
oh yes break breaks it early imma get rid of it. i know this is going to sound stupid but you can't learn if you never ask. i've been learning sporadically c++, what is the difference between \n and the space between the roman numeral and its integer value in second format i post up there? i thought that when the stream read the file it is read something like this(using the second example in my first post):

I 1\n
X 10\n
4 IV\n
?
I may be wrong but that's what I think too.

Combining getline() and peek() for ur purpose is probably not going to work out too well because getline() isn't quite behaving as u expected.

The usual way I would do this with getline is something like:
1
2
3
4
5
6
while (getline(tester, check))
{
  int found=check.find(' ');
  if (found != std::string::npos) //did we reach the end of the string?
       //whitespace found !
}
Last edited on
Topic archived. No new replies allowed.