Thread: for loop ignoring scanf inside loop

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    13

    for loop ignoring scanf inside loop

    Logicially I think it's possible to have a scanf in a for loop. Am I wrong?

    Here is the code that's ignoring the loop, I took out the parts that are not required to answer this question. When running the program choose 1 and then a number, between 3-10 should be sufficent:

    Code:
    int main()
    { // open main
        int selection, amount, count;
        int value;
        printf("Welcome to the Cacluator of Statics of Materials\n\n");
        printf("1.  2-D Resultant Force at a point.\n");
        printf("Please choose a selection: ");
        scanf("%d", &selection);
        
        if(selection == 1)
            {
            printf("\nYou choose to calcualte a Resultan Force at a point.\n\n");
            printf("Please insert the amount of Forces: ");
            scanf("%d", &amount);
            int forces[amount];
            count = 0;;
            for(count == 0; count < amount; count++)
            {
            printf("\nPlease insert the value of force #%d: ", count+1);
            scanf("d", &value); //  <---- This is being ignored why? 
            forces[count] = value;    
            }
            }
            else
            {
            printf("\nSelection Invalid!  Please re-run the program.\n\n");
            }  
     system("PAUSE");   
    } // close main
    Last edited by xIcyx; 04-17-2007 at 01:43 AM.

  2. #2
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    You have several mistakes in your code:

    Code:
    int main()
    { // open main
        int selection, amount, count;
        int value;
        printf("Welcome to the Cacluator of Statics of Materials\n\n");
        printf("1.  2-D Resultant Force at a point.\n");
        printf("Please choose a selection: ");
        scanf("&#37;d", &selection);
        
        if(selection == 1)
            {
            printf("\nYou choose to calcualte a Resultan Force at a point.\n\n");
            printf("Please insert the amount of Forces: ");
            scanf("%d", &amount);
            int forces[amount];
            count = 0;; //<--- two ; here
            for(count == 0; count < amount; count++)
            {
            printf("\nPlease insert the value of force #%d: ", count+1);
            scanf("d", &value);
            forces[count] = value;    
            }
            }
            else
            {
            printf("\nSelection Invalid!  Please re-run the program.\n\n");
            }  
     system("PAUSE");   
    } // close main
    First, modify "d" into "%d" inside the scanf(), change the equality check "==" into an assignment "=", then try again.

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    13
    That's a sign I need to go to sleep. Thank you... all I was missing was the &#37;.

    As for the "==" I declared count = 0; therefor i'm saying that when count is equal to 0 run the loop. May not be the best way but it works.

    the ;; got there by deleting code to post on here. Wasn't in the main program, thank you for notifying me though.
    Last edited by xIcyx; 04-17-2007 at 01:49 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-23-2009, 03:51 AM
  2. Caught in a Loop
    By aprilbiz in forum C Programming
    Replies: 16
    Last Post: 07-22-2002, 06:06 PM
  3. for loop or while loop
    By slamit93 in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2002, 04:13 AM
  4. Replies: 1
    Last Post: 11-19-2001, 04:45 PM
  5. Scanf for char doesn't work in loop
    By Unregistered in forum C Programming
    Replies: 10
    Last Post: 10-18-2001, 04:42 AM