While loop trouble

This is a discussion on While loop trouble within the C Programming forums, part of the General Programming Boards category; I am writing a simple C program using a while loop to enter numbers to a file. I'm very new ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    21

    While loop trouble

    I am writing a simple C program using a while loop to enter numbers to a file. I'm very new to this so please don't say it's obvious.

    Code:
    #include <conio.h>
    #include <stdio.h>
    
    int main()
    {
    	int neilsdata[5];
    	int val, pos;
    	FILE *neilfile;
    	int cont = 1;
    	char contch;
    
    	// open file
    	if( ( neilfile = fopen( "vals", "wb" ) ) == NULL )
    	{
    		printf("Couldn't create file\n");
    		exit( -1 );
    	}
    
    	// write data to file
    	while ( cont==1 )
    	{
    		printf("Enter a value\n");
    		scanf("%d", &val);
    
    		fwrite( &val, sizeof val, 10, neilfile );
    
    		printf("Do you want another value\n");
    		scanf("%d", &contch);
    		if( contch == "y" )
    			cont = 1;
    		else
    			cont = 0;
    	}
    
    	fclose( neilfile );
    	
    
    	// read in same file
    	// open file
    	if( ( neilfile = fopen( "vals", "rb" ) ) == NULL )
    	{
    		printf("Couldn't open file\n");
    		exit( -1 );
    	}
    
    	fread( &val, sizeof val, 1, neilfile );
    	while( !feof( neilfile ) )
    	{
    		printf (&val, "\n");
    		fread( &val, sizeof val, 1, neilfile );
    	}
    
    	fclose( neilfile );
    
    	getch();
    	getch();
    
    }
    After taking one value and entering y/n it just throws a whole line of unhelpful characters and ends there. Something with the loop, or the scan command?

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,646
    Use the "%c" format specifier to read individual characters, not "%d". Since an int is larger than a char, by using %d your code is merrily tromping the memory adjacent to contch - which is a recipe for trouble. Also when scanf() sees a letter (which is not a valid part of a number) it leaves it in the stream, and loops forever - tromping memory repeatedly.

    I'm surprised your code even compiled, given that you are evaluating (contch == "y"). Use single quotes (to represent a single char value) not double quotes (which delimit strings of multiple char).

    Unrelated to the above, later on in your code, you loop on !feof(neilfile). Have a poke through FAQs here to learn why that is a bad idea.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Sunshine, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    19
    Look at the %d scanf format and the type of the variable contch.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. For Loop Trouble
    By Jackbenimble in forum C Programming
    Replies: 15
    Last Post: 02-11-2011, 03:04 PM
  2. Loop trouble
    By peter94 in forum C++ Programming
    Replies: 4
    Last Post: 09-24-2008, 07:51 AM
  3. While Loop trouble.
    By Aterxerxes in forum C++ Programming
    Replies: 6
    Last Post: 10-04-2005, 04:59 AM
  4. while loop trouble
    By bazzano in forum C Programming
    Replies: 1
    Last Post: 09-19-2005, 03:59 AM
  5. Trouble with a while loop
    By Dr^ZigMan in forum C++ Programming
    Replies: 8
    Last Post: 05-07-2005, 11:01 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21