Thread: Criticize this please... infinite loop idk why

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    30

    Criticize this please... infinite loop idk why

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    double addfunc(double a, double b);       //Add function 
    double subfunc(double a, double b);       //Subtract function
    double divfunc(double a, double b);       //Divide function
    double multifunc(double a, double b);     //Multiply function
    void inputfunc(double *a, double *b);   //Input from console function
    void calculate(double *a, double *b);   //Choice of operation and calling function
    
    
    
    
    int main()
    {
    
    
    double answer1 = 0;                     //Holds first value inputted
    double answer2 = 0;                     //Holds second value inputted
    
    
    inputfunc(&answer1, &answer2);          //inputfunc function call passing back answer1/2 
    
    
    calculate(&answer1, &answer2);          //calculate function call passing answer1/2
    
    
    
    
    }
    
    
    
    
    double addfunc(double a, double b)           //Adds two numbers
    {
        return a+b;
    }
    
    
    double subfunc(double a, double b)          //Subtracts two numbers
    {
        return a-b;
    }
    
    
    double divfunc(double a, double b)          //Divides two numbers
    {
        return a/b;
    }
    
    
    double multifunc(double a, double b)         //Multiplies two numbers
    {
        return a*b;
    }
    
    
    void inputfunc(double *a, double *b)         //function to provide input
    {
        
        double temp1 = .12351;                        //temp1 default value 
        double temp2 = .12351;                      //temp1 default value
        
        do                                            //do loop continues to repeat if default value are not changed
        {
            
            
            printf("Enter your first number: ");    //Ask for input
            scanf("%l",&temp1);                        //Collect console input assign to temp1
            
            printf("Enter your second number: ");   //Ask for input2
            scanf("%l",&temp2);                      //Collect console input assign to temp2
            
            
        }
        while(temp1 == .12351 || temp2 == .12351);  
    
    
        
        *a = temp1;                                    //pass values collected back
        *b = temp2;                                    //pass values collected back
        
        return;
    }
    
    
    void calculate(double *a, double *b)            //calculate function that asks for input on operation and calculate/outputs result
    {
            
            char operation = 'Z';                    //preset value also used in while loop
            double temp1 = *a;                      //temp var to hold passed value
            double temp2 = *b;                        //temp var to hold passed value
            
            while (operation == 'Z')                //while loop to keep entry going if value entered doesnt meet if statement condition
            {
                printf("\nWhich operation would you like to use?(A/S/M/D/(Q)uit): "); //prompt for user to enter operation
                operation = getchar();                                                  //operation collecting input
            
                if (operation != 'A' || operation != 'S' || operation != 'M' || operation != 'D' || operation != 'Q')  //checking for bad input
                {
                    continue;                                                                                           //if bad input continues loop
                }
                else
                {
                    switch (operation)                                                                                //switch statement that takes choice and calc/output result
                    {
                        case 'A':
                            printf("Result: %lf",addfunc(temp1,temp2));
                            break;
                        case 'S':
                            printf("Result: %lf",subfunc(temp1,temp2));
                            break;
                        case 'M':
                            printf("Result: %lf",multifunc(temp1,temp2));
                            break;
                        case 'D':
                            printf("Result:  %lf",divfunc(temp1,temp2));
                            break;
                        case 'Q':
                            return;
                    }
                }
                operation = 'Z';                                                                                    //resets var to allow for looping on bad input
                    
            }
        return;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Look at the following snippet:
    Code:
    operation = getchar();                                                  //operation collecting input
     
    if (operation != 'A' || operation != 'S' || operation != 'M' || operation != 'D' || operation != 'Q')  //checking for bad input
    Let's assume that you entered 'A' in the first line. Since operation is equal to 'A' the not equal to 'A' is false, but 'A' is not equal to 'S'.

    Also you should re-read your documentation for scanf(). What is the proper format specifier for a double?
    Code:
        double temp1 = .12351;                        //temp1 default value
    ...
             
            printf("Enter your first number: ");    //Ask for input
            scanf("%l",&temp1);
    Jim
    Last edited by jimblumberg; 06-24-2013 at 09:23 AM.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    In addition to the above comments, I think you want "%lf" in your "scanf()" calls.

    Code:
    main.c||In function 'inputfunc':|
    main.c|73|warning: conversion lacks type at end of format|
    main.c|73|warning: too many arguments for format|
    main.c|76|warning: conversion lacks type at end of format|
    main.c|76|warning: too many arguments for format|
    main.c||In function 'main':|
    main.c|33|warning: control reaches end of non-void function|
    ||=== Build finished: 0 errors, 5 warnings ===|
    Code:
    printf("Enter your first number: ");    //Ask for input
    scanf("%lf",&temp1);                        //Collect console input assign to temp1
    
    printf("Enter your second number: ");   //Ask for input2
    scanf("%lf",&temp2);

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    30
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    
    double addfunc(double a, double b);       //Add function 
    double subfunc(double a, double b);       //Subtract function
    double divfunc(double a, double b);       //Divide function
    double multifunc(double a, double b);     //Multiply function
    void inputfunc(double *a, double *b);   //Input from console function
    void calculate(double *a, double *b);   //Choice of operation and calling function
    
    
    
    
    int main()
    {
    
    
    double answer1 = 0;                     //Holds first value inputted
    double answer2 = 0;                     //Holds second value inputted
    
    
    inputfunc(&answer1, &answer2);          //inputfunc function call passing back answer1/2 
    
    
    calculate(&answer1, &answer2);          //calculate function call passing answer1/2
    
    
    return 0;
    
    
    
    
    }
    
    
    
    
    double addfunc(double a, double b)           //Adds two numbers
    {
        return a+b;
    }
    
    
    double subfunc(double a, double b)          //Subtracts two numbers
    {
        return a-b;
    }
    
    
    double divfunc(double a, double b)          //Divides two numbers
    {
        return a/b;
    }
    
    
    double multifunc(double a, double b)         //Multiplies two numbers
    {
        return a*b;
    }
    
    
    void inputfunc(double *a, double *b)         //function to provide input
    {
        
        double temp1 = .12351;                        //temp1 default value 
        double temp2 = .12351;                      //temp1 default value
        
        do                                            //do loop continues to repeat if default value are not changed
        {
            
            
            printf("Enter your first number: ");    //Ask for input
            scanf("%lf",&temp1);                        //Collect console input assign to temp1
            
            printf("Enter your second number: ");   //Ask for input2
            scanf("%lf",&temp2);                      //Collect console input assign to temp2
            
            
        }
        while(temp1 == .12351 || temp2 == .12351);  
    
    
        
        *a = temp1;                                    //pass values collected back
        *b = temp2;                                    //pass values collected back
        
        return;
    }
    
    
    void calculate(double *a, double *b)            //calculate function that asks for input on operation and calculate/outputs result
    {
            
            char operation = 'Z';                    //preset value also used in while loop
            double temp1 = *a;                      //temp var to hold passed value
            double temp2 = *b;                        //temp var to hold passed value
            
            while (operation == 'Z')                //while loop to keep entry going if value entered doesnt meet if statement condition
            {
                printf("\nWhich operation would you like to use?(A/S/M/D/(Q)uit): "); //prompt for user to enter operation
                operation = getchar();                                                  //operation collecting input
            
                if (operation == 'A' || operation == 'S' || operation == 'M' || operation == 'D' == operation == 'Q')  //checking for bad input
                {
                    switch (operation)                                                                                //switch statement that takes choice and calc/output result
                    {
                        case 'A':
                            printf("Result: %lf",addfunc(temp1,temp2));
                            break;
                        case 'S':
                            printf("Result: %lf",subfunc(temp1,temp2));
                            break;
                        case 'M':
                            printf("Result: %lf",multifunc(temp1,temp2));
                            break;
                        case 'D':
                            printf("Result:  %lf",divfunc(temp1,temp2));
                            break;
                        case 'Q':
                            return;
                    }
                                                                                                               //if bad input continues loop
                }
                else
                {
                    operation = 'Z';
                    continue;
                }
                                                                                                    //resets var to allow for looping on bad input
                    
            }
        return;
    }
    Thank you for the help. These errors were driving me nuts.

    I am now trying to figure out why this line is doubling itself:

    Code:
    printf("\nWhich operation would you like to use?(A/S/M/D/(Q)uit): "); //prompt for user to enter operation
                operation = getchar();        
    








  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    in line 83 you better check return values of scanf to control your loop

    in line 104 - getchar returns int - so you can check the return value for EOF before treating it as char

    in line 106
    Code:
    operation == 'D' == operation == 'Q'
    Why do you need to pass pointers to your calculate function?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-14-2011, 11:33 PM
  2. infinite loop
    By leahknowles in forum C Programming
    Replies: 2
    Last Post: 03-31-2011, 03:40 PM
  3. help with infinite loop please
    By officedog in forum C Programming
    Replies: 4
    Last Post: 11-05-2008, 02:42 PM
  4. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM
  5. Infinite Loop
    By cdonlan in forum C Programming
    Replies: 25
    Last Post: 12-02-2004, 03:35 PM