Thread: while loop problem

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    11

    while loop problem

    Could anybody check this out?
    it gives me a segmentation fault error when run

    Code:
    #include <stdio.h>
    
    main()
    
    {
    	int iBegNum = 0;
    	int iEndNum = 0;
    	int iIncre = 0;
    	
    	printf("\n\tCounting program");
    	
    	printf("\nEnter the beginning number to start counting from: ");
    	scanf("%d", iBegNum);
    	
    	printf("\nEnter the number to stop counting at: ");
    	scanf("%d", iEndNum);
    	
    	printf("\nEnter the increment number: ");
    	scanf("%d", iIncre);
    	
    	while ( iBegNum <= iEndNum)
    	{
    		printf("\nThe value is: %d", iBegNum);
    	  iBegNum = iBegNum + iIncre;
    	}
    
      printf("\n\nThe program will now exit");
      
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    	printf("\nEnter the beginning number to start counting from: ");
    	scanf("%d", &iBegNum);
    Look up scanf() in your docs...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Mainly, it's forgetting the & on your scanf calls.

    Perhaps use a compiler (gcc) that can tell you when you're abusing printf/scanf ?
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c
    foo.c:5: warning: return type defaults to ‘int’
    foo.c: In function ‘main’:
    foo.c:13: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
    foo.c:16: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
    foo.c:19: warning: format ‘%d’ expects type ‘int *’, but argument 2 has type ‘int’
    foo.c:13: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
    foo.c:16: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
    foo.c:19: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result
    foo.c:29: warning: control reaches end of non-void function
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    printf("\nEnter the beginning number to start counting from: ");
    scanf("%d", iBegNum);
    	
    printf("\nEnter the number to stop counting at: ");
    scanf("%d", iEndNum);
    	
    printf("\nEnter the increment number: ");
    scanf("%d", iIncre);
    scanf needs an address to store the converted data into, therefore you must pass the address of (a pointer to) those variables to scanf. To do this you must prefix the variable name with & (the address-of operator).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  5. #5
    Registered User
    Join Date
    Jan 2011
    Posts
    11
    damn that always happens to me.

    thanks

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    11
    well it seems i got another problem here
    there is a parse error before else but i cant find it

    Code:
    	while ( iNum <= iNumQue )
    	{
    		
    		iCurrTime = time(NULL);
    		
    		do
    			{
    				iElapTime = time(NULL);
    				x = (rand() % 100 ) + 1;
    				y = (rand() % 100 ) + 1;
    				iResult = x + y;
    		
    				printf("\nSum %d and %d", x, y);
    			} while ((iElapTime - iCurrTime) < 10); /*end do while*/
    			
    			system("clear");
    		
    		printf("\n\n%d. Enter your answer: ", iNum);
    		scanf("%d", &iAns);
    		
    		if (iAns == iResult)
    			{
    				printf("\nCongratulations, you answered correct!");
    				iNumCorr = iNumCorr + 1;
    		else /*error: parse error before "else"*/
    			printf("\nSorry, you answered wrong");
    			iNumWrg = iNumWrg + 1;
    		  } /*end if*/
    		iNum = iNum + 1;
    	} /*end while*/

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You need to add braces in the vicinity.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    11
    ohh i got it
    thank you

  9. #9
    Registered User
    Join Date
    Jan 2011
    Posts
    11
    ok, a small question for the end

    how to clear the screen as neither system("clear") nor system("cls") seem to work
    >sh: cls: command not found<

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple loop problem I cant seem to solve
    By LightYear in forum C Programming
    Replies: 8
    Last Post: 03-21-2010, 06:59 PM
  2. Problem with infinite loop in signal handler
    By semun in forum C Programming
    Replies: 6
    Last Post: 07-22-2009, 01:15 PM
  3. Addition problem in loop
    By murjax in forum C Programming
    Replies: 3
    Last Post: 07-01-2009, 06:29 PM
  4. validation problem in a loop (newbie question)
    By Aisthesis in forum C++ Programming
    Replies: 11
    Last Post: 05-10-2009, 10:47 PM
  5. For Loop Problem
    By xp5 in forum C Programming
    Replies: 10
    Last Post: 09-05-2007, 04:37 PM