Click here to Skip to main content
13,151,514 members (46,137 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
okay I have the first line down
This is what I'm reading from a file

BECDCBAADEBACBAEDDBEDCBAAECDCB
1234 BECDXACDXAXDXEDBXCABCDEXXECDCB

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:

        infile>>r;
 
    while  (!infile.eof()){
        while (r != '\n' && d<30){
            right [d] = r;
            cout<<right [d];
            infile>>r;
            d++;
        }
        infile>>i;
        cout<<i;
    }
Posted 20-Mar-17 12:41pm
Updated 20-Mar-17 13:25pm
Comments
ppolymorphe 20-Mar-17 19:26pm
   
Is it a Repost ?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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;
			break;				
		}
		infile >> r;
		d++;
	}
	infile >> r;
	/*cout << r;*/
}
  Permalink  
Comments
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web01 | 2.8.170924.2 | Last Updated 20 Mar 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100