while loop problem

This is a discussion on while loop problem within the C Programming forums, part of the General Programming Boards category; Could anybody check this out? it gives me a segmentation fault error when run Code: #include <stdio.h> main() { int ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,546
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,801
    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
    21,729
    You need to add braces in the vicinity.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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

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