For loop problems, input please.

This is a discussion on For loop problems, input please. within the C Programming forums, part of the General Programming Boards category; I'm trying to run this for loop. I've included the entire code to try to solve the problem. I'm really ...

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

    For loop problems, input please.

    I'm trying to run this for loop. I've included the entire code to try to solve the problem. I'm really not understanding why this is not looping. When I enter an invalid selection it should make the user input it again, but it's not looping. To re-create my problem when running choose a number greater then 5. The area's i'm questioning about are in red. Any input is apprechiated.

    Code:
    #include "stdio.h"
    #include "math.h"
    #include "stdlib.h"
    
    int main()
    {
        int selection, amount, count, prog;
        double angle, angle2, angle3, radians, radians2, radians3, forcex, forcey, force, force2, totalx, totaly, value, resultant, direction, degree;
        #define PI 3.141592654 
        
        printf("Welcome to the Cacluator of Statics of Materials.\n\n");
        printf("1.  2-D resultant force at a point.\n");
        printf("2.  2-D uknown force vectors with angles (Max 2) with known resultant force. \n");
        printf("3.  I'm not sure yet. \n");
        printf("4.  Directions. \n");
        printf("5.  Exit the program.\n\n");
        prog = 0;
        for(prog == 0;prog > 0; prog++);
        {
        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 input the amount of Forces: ");
            scanf("%d", &amount);
            int forces[amount];
            count = 0;
            totalx = 0;
            totaly = 0;
            for(count = 0; count < amount; count++)
            {
            printf("\nPlease input the unit value of force #%d: ", count+1);
            scanf("%lf", &value);
            forces[count] = value;
            printf("\nPlease input the positive angle from the x axis: ");
            scanf("%lf", &angle);
            radians = angle * PI / 180;
            forcex = value * cos(radians);
            forcey = value * sin(radians);        
            totalx = totalx + forcex;
            totaly = totaly + forcey;
            }
            resultant = sqrt(totalx * totalx + totaly * totaly);
            direction = atan(totaly/totalx);
            degree = direction * 180 / PI;
            printf("\nThe unit value of the forces in the x direction is: %8.4lf \n", totalx);
            printf("The unit value of the forces in the y direction is: %8.4lf \n", totaly);
            printf("The resultant force is: %8.4lf units at %8.4lf degrees.\n\n", resultant, degree);
            }
            else if(selection == 2)
            {
            printf("\nYou choose to caclulate two unkown vectors.\n\n");
            printf("Please input the magnitude of the resultant force: ");
            scanf("%lf", &resultant);
            printf("Please input the angle of the first unknown from the resultant force: ");
            scanf("%lf", &angle);
            if(angle < 0)
            {
            angle = angle * -1;
            }
            else
            {
            printf("Please input the angle of the second unkown form resultant force: ");
            scanf("%lf", &angle2);    
            }
            if(angle2 < 0)
            {
            angle2 = angle2 * -1;
            }
            else
            {
            radians = angle * PI / 180;
            radians2 = angle2 * PI / 180;
            angle3 = 180 - (angle + angle2);
            radians3 = angle3 * PI / 180;
            force = resultant/sin(radians3)*sin(radians2);
            force2 = resultant/sin(radians3)*sin(radians);
            printf("\nThe first unknown is: %8.4lf units.", force);
            printf("\nThe second unknown is: %8.4lf units.\n\n", force2);
            }
            }
            else if(selection == 3)
            {
                
            }
            else if(selection == 4)
            {
            printf("\nPlease read the following instructions for proper operations:\n\n");   
            }
            else if(selection == 5)
            {
            printf("\nExiting the program.\n\n");
            prog = -2;  
            }
            else
            {
            printf("\nSelection Invalid!  Please re-enter your selection.\n\n");
            prog = 0;
            } 
        } 
     system("PAUSE");   
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for(prog == 0;prog > 0; prog++);
        {
    Well for starters, you're not actually initializing prog here.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,439
    The ; at the end of the for loop as well means you do nothing a number of times.
    Then all the stuff which follows it just happens once.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need some help with C program writing
    By The_PC_Gamer in forum C Programming
    Replies: 9
    Last Post: 02-12-2008, 08:12 PM
  2. Problems with input and fflush
    By edugarcia in forum Linux Programming
    Replies: 1
    Last Post: 11-24-2004, 12:52 PM
  3. Custom Made Safe Input Function
    By Beast() in forum C Programming
    Replies: 6
    Last Post: 08-21-2004, 10:19 PM
  4. Input Problems and C# (CSharp) Tutorials
    By Grayson_Peddie in forum C# Programming
    Replies: 4
    Last Post: 02-27-2003, 09:45 PM
  5. Problems with commas as input
    By Yojimbo III in forum C++ Programming
    Replies: 3
    Last Post: 02-05-2003, 08:18 PM

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