Thread: having some trouble reassigning input

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    41

    having some trouble reassigning input

    hey i am having trouble reassigning input from the user. the conditions are the user inputs several float values one at a time and once the user inputs "zero" the program should determine the largest and smallest numbers.
    this program is the one that i got help on last night and i tweaked it a bit. i am sure i am doing something wrong but i am not sure if i am assigning the values right or i went in the wrong direction on this one.
    first program is the main program that asks for input and sends the input to the second which in return sends the largest and smallest values entered. the second program shouldnt be changed because the original first program i made uses the second program.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    float max(float input1, float input2);
    float min(float input1, float input2);
    
    main()
    {
    float input, input1, input2;
    int term;
        while (term != EOF && input != 0)
    {
    printf("Enter a float value:\n");
    term = scanf("%f", &input);
        {
            input != 0;
            input2 = input1;
            input1 = input;
        }
    }
        if (term == EOF)
    {    
    printf("control-D\n"); 
    exit(0);    
    }
    
        if (input == 0)
    {
    float nMax, nMin;
    nMax = max(input1, input2);
    nMin = min(input2, input1);
    printf("Maximum = %.2f and Minimum = %.2f\n", nMax, nMin);
    exit(0);
    }
    }
    Code:
    #include "maxmin.h"
    
    float max(float input1, float input2)
      { 
        if (input1 > input2)
           return input1;
        else
        return input2;
       }
    
    
    float min(float input1, float input2)
      { 
        if (input2 < input1)
           return input2;
        else
        return input1;
     }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Just use two sanf() calls one for each input... or do it like you did last night with both on the same line...

    Line 16 has no efffect
    Line 17 uses input1 in an uninitialized state

    In both these cases your compiler should be warning you.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    You should use scanf for both floats and check the return-value from scanf:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    float max(float input1, float input2);
    float min(float input1, float input2);
    
    int main()
    {
    float input1, input2;
    printf("Enter two float values: ");
    if( 2==scanf("%f%f", &input1,&input2) )
      printf("Maximum = %.2f and Minimum = %.2f\n", max(input1, input2), min(input1, input2));
    else
      fputs("error on input",stderr);
    return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    41
    im suppose to get it to prompt for a float value after float value instead of a pair. it should keep getting a value from the user until 0 is entered and then it should show the largest and smallest of the inputs by the user. if i could do it like last night it would be easy for me to figure out but i havent made programs where i do something similar

    Quote Originally Posted by CommonTater View Post
    Just use two sanf() calls one for each input... or do it like you did last night with both on the same line...

    Line 16 has no efffect
    Line 17 uses input1 in an uninitialized state

    In both these cases your compiler should be warning you.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    41
    corrected:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    float max(float input1, float input2);
    float min(float input1, float input2);
    
    main()
    {
    float input, input1, input2;
    int term;
        while (term != EOF && input != 0)
    {
    printf("Enter a float value:\n");
    term = scanf("%f", &input);
    input1 = input;
    
    printf("Enter a float value:\n");
    term = scanf("%f", &input);
    input2 = input;
    
    }
    
        if (input == 0)
    {
    float nMax, nMin;
    nMax = max(input1, input2);
    nMin = min(input2, input1);
    printf("Maximum = %.2f and Minimum = %.2f\n", nMax, nMin);
    exit(0);
    }
        if (term == EOF)
    {    
    printf("control-D\n"); 
    exit(0);    
    }
    }
    2nd program is still the same

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    41
    hey guys just an update the first program has been remade:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #include "maxmin.h"
    
    float max(float input, float input2);
    float min(float input, float input2);
    
    main()
    {
    float input;
    float nMax, nMin;
    int firstt = 1;
    
    while ( 1 )
    {
    printf("Enter a float value: \n");
    scanf("%f", &input);
    if( input == 0 ) break;
    if ( firstt == 1)
    {
    nMax = nMin = input;
    }
    else
    {
    nMax = max(input, nMax);
    nMin = min(input, nMin);
    }
    }
    printf("Max = %.2f and Min = %.2f\n", nMax, nMin);
    }
    it still needs the 2nd program to run and compile. the problem is that it cant perform the functions correctly like it will display the max and min from the previous input before 0.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    And what happens if someone sits there and types ...

    12
    12
    15
    32
    48
    12134
    111
    012167

    and just keeps on going?

    Try this...
    Code:
    float inputs[2] = {0}
    int i;
    
    for(i = 0; i < 2; i++)
      scanf("%f", inputs[i];
    
    if (inputs[0] == 0 || inputs[1] == 0)
      { printf("Bye Bye\n\n);
         exit(1); }
    
    printf ("Min = %f", min(inputs[0],inputs[1]);
    printf ("Max = %f", max(inputs[0],inputs[1]);
    The thing you need to do the most is to stop guessing... and spend a bit of time thinking about how best to do this... note the 4 steps in my signature... no, they're not just there for show....
    Last edited by CommonTater; 10-09-2011 at 02:06 AM.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    SourceForge.net: Indentation - cpwiki
    Your indentation gets incrementally worse each time you post.
    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.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    SourceForge.net: Indentation - cpwiki
    Your indentation gets incrementally worse each time you post.
    Salem... we've been over this before. Everyone here knows I have my own weird style...

    Oh, wait, you meant the OP....

    Never mind.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reassigning readonly fields
    By Mario F. in forum C# Programming
    Replies: 12
    Last Post: 05-12-2011, 05:43 AM
  2. Having trouble with getting input from files.
    By Matsuya in forum C++ Programming
    Replies: 4
    Last Post: 05-26-2009, 10:56 AM
  3. Trouble with input validation
    By MSF1981 in forum C Programming
    Replies: 3
    Last Post: 02-22-2009, 11:59 AM
  4. Trouble with file input
    By w274v in forum C Programming
    Replies: 6
    Last Post: 12-18-2005, 04:40 AM
  5. reassigning FILE pointers & fprintf
    By MPC_Engr in forum C Programming
    Replies: 3
    Last Post: 10-18-2002, 05:24 PM