Click here to Skip to main content
15,359,191 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
okay I have the first line down
This is what I'm reading from a file


But the problem is I can't read the second line properly, the inner while loop is perfectly fine, as it's reading the first line, the right answers, into an array.

The output I'm getting when trying to read the numbers are 234234 instead of 1234.

Thank you!

What I have tried:


while  (!infile.eof()){
    while (r != '\n' && d<30){
        right [d] = r;
        cout<<right [d];
Updated 20-Mar-17 13:25pm
Patrice T 20-Mar-17 19:26pm
Is it a Repost ?

For more help, try to improve your question with code that can be executed.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
Hi Member 13068214,

Please use Peek() to check newline. For some reason it is not recognizing newline at the loop condition.
while (!infile.eof()) {
	while (d < 30) {
		right[d] = r;
		cout << right[d];
		if (infile.peek() == '\n')
			d = 0;
		infile >> r;
	infile >> r;
	/*cout << r;*/
Member 13068214 21-Mar-17 7:52am
We haven't learned peek as yet. What is the equivalent doing of peek?
Mehedi Shams 21-Mar-17 18:19pm
peek() is used like the same as reading the next character, except that it doesn't proceed the file pointer. That is, it just peeps at the next character, but doesn't proceed with usual file read operation.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900