Like Tree1Likes
  • 1 Post By Salem

error problems

This is a discussion on error problems within the C Programming forums, part of the General Programming Boards category; hey guys so this file is a separate file from the main program i am writing and i keep getting ...

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

    error problems

    hey guys so this file is a separate file from the main program i am writing and i keep getting a "lvalue required as left operand of assignment"
    i still cant figure out what to do after fiddling around with the error.
    this function is suppose to take 2 input values from my main program and see which of the pair of numbers is bigger(max) and smaller(min)
    also could i ask how do i make a file.h and how do i write functions in a ".h" file
    appreciate the help i can get

    Code:
    float max(float input)
    {
    float input1, input2;
    if (input1 > input2)
    {
    max = input1;
    }
    else
    {
    max = input2;
    }
    }
    
    
    float min(float input)
    {
    float input1, input2;
    if (input1 < input2)
    {
    min = input1;
    }
    else 
    {
    max = input2;
    } 
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    Well you're using the name of the function as the return result.

    Say something like
    result = max;

    Then at the end of the function
    return result;
    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.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    41
    omg i cant believe i missed that thanks i will try it now

    Quote Originally Posted by Salem View Post
    Well you're using the name of the function as the return result.

    Say something like
    result = max;

    Then at the end of the function
    return result;

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    This is your one and only freebie... Study how this works until you understand it...

    Code:
    float max(float input1, float input2)
      {
        return (input1 > input2) ? input1 : input2; 
      }
    
    
    float min(float input1, float input2)
      {
        return (input1 < input2) ? input1 : input2;
      }
    See how easy it is?

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    41
    so i made the corrections:
    Code:
    float max(float input)
    {
    float input1, input2, ma1,mi2;
    if (input1 > input2)
    {
    ma1 = input1;
    }
    else
    {
    mi2 = input2;
    }
    }
    
    
    float min(float input)
    {
    float input1, input2, ma1, mi2;
    if (input1 < input2)
    {
    mi2 = input1;
    }
    else 
    {
    ma1 = input2;
    } 
    }
    i have another program which the user inputs 2 variables. it seems that the variables arent being passed to the max and min functions am i declaring the functions right in this main function?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    float max(float input);
    float min(float input);
    
    main()
    {
    float input1, input2;
    int term;
        while (term != EOF)
    {
    float i1, i2;
    printf("Enter a pair of float values:\n");
    term = scanf("%f" "%f", &input1, &input2);
    printf("You have entered %f and %f\n", input1, input2);
    max(i1);
    min(i2);
    printf("Max = %f and Min = %f\n", i1, i2);
    }
        if (term == EOF)
    {    
    printf("control-D"); 
    exit(0);    
    }
    }

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    1. You have two new variables (I had only one)
    2. You don't have a return statement at the end.
    3. In main, you also need something like answer = min(input1, input2);, and then print answer.
    ecsx00 likes this.
    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.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by ecsx00 View Post
    so i made the corrections:
    Code:
    float max(float input)
    {
    float input1, input2, ma1,mi2;
    if (input1 > input2)
    {
    ma1 = input1;
    }
    else
    {
    mi2 = input2;
    }
    }
    
    
    float min(float input)
    {
    float input1, input2, ma1, mi2;
    if (input1 < input2)
    {
    mi2 = input1;
    }
    else 
    {
    ma1 = input2;
    } 
    }
    i have another program which the user inputs 2 variables. it seems that the variables arent being passed to the max and min functions am i declaring the functions right in this main function?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    float max(float input);
    float min(float input);
    
    main()
    {
    float input1, input2;
    int term;
        while (term != EOF)
    {
    float i1, i2;
    printf("Enter a pair of float values:\n");
    term = scanf("%f" "%f", &input1, &input2);
    printf("You have entered %f and %f\n", input1, input2);
    max(i1);
    min(i2);
    printf("Max = %f and Min = %f\n", i1, i2);
    }
        if (term == EOF)
    {    
    printf("control-D"); 
    exit(0);    
    }
    }
    As salem points out...
    You are only passing in a single value.
    Your internal compare is on essentially random numbers, using uninitialized variables.
    Your functions aren't returning anything.
    You call the function from main but ignore it's return value...

    I'm amazed it even compiles.

    Try this...
    Code:
    float max(float input1, float input2)
      { 
        if (input1 > input2)
           return input1;
        else
           return input2;
       }
    See how it works... two values in the top, a decision based on those values... one value returned at the bottom.

    Now in your main code you need...
    Code:
    nMax = max(input1,input2);
    nMax is assigned the value returned by the function.

    The answer comes out the bottom... not the top.
    Last edited by CommonTater; 10-08-2011 at 02:58 AM.

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    41
    Salem while I am still trying to figure out how you only got one variable i fixed my functions with return which i forgot since my computer battery died. in main i added that answer = function and i have no compiling errors. i also appreciate the answers you are giving me im learning a bit more now.

    edit: new corrections are posted in a reply
    Last edited by ecsx00; 10-08-2011 at 03:08 AM.

  9. #9
    Registered User
    Join Date
    Sep 2011
    Posts
    41
    hey CommonTater so i did the changes in the program and ran it without problems. The functions are either not doing their intended tasks or the stuff in main isnt working either one i keep getting the same results.

    Quote Originally Posted by CommonTater View Post
    As salem points out...
    You are only passing in a single value.
    Your internal compare is on essentially random numbers, using uninitialized variables.
    Your functions aren't returning anything.
    You call the function from main but ignore it's return value...

    I'm amazed it even compiles.

    Try this...
    Code:
    float max(float input1, float input2)
      { 
        if (input1 > input2)
           return input1;
        else
           return input2;
       }
    See how it works... two values in the top, a decision based on those values... one value returned at the bottom.

    Now in your main code you need...
    Code:
    nMax = max(input1,input2);
    nMax is assigned the value returned by the function.

    The answer comes out the bottom... not the top.

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    41
    corrections:
    1st program main function:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    float max(float input, float inputt);
    float min(float input, float inputt);
    
    main()
    {
    float input1, input2;
    int term;
        while (term != EOF)
    {
    float nMax, nMin;
    printf("Enter a pair of float values:\n");
    term = scanf("%f" "%f", &input1, &input2);
    printf("You have entered %f and %f\n", input1, input2);
    nMax = max(input1,input2);
    nMin = min(input1,input2);
    printf("Max = %f and Min = %f\n", nMax, nMin);
    }
        if (term == EOF)
    {    
    printf("control-D"); 
    exit(0);    
    }
    }
    2nd program being called by 1st:
    Code:
    float max(float input1, float input2)
      { 
        if (input1 > input2)
           return input1;
        else
           return input2;
       }
    
    
    float min(float input1, float input2)
      { 
        if (input1 < input2)
           return input1;
        else
           return input2;
       }

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    It can't be compiling... Line 15 should cause an error about incorrect types...
    Code:
    term = scanf("%f" "%f", &input1, &input2);
    Should be...
    Code:
    term = scanf("%f %f",&input1, &input2);
    Most likely the compile is failing but it's running a previous version of the program... so it SEEMS to be giving you the same results. Set your compiler warnings to maximum and treat each message as a problem to be corrected.

  12. #12
    Registered User
    Join Date
    Sep 2011
    Posts
    41
    its working right now, only thing is when i use EOF the last input and output is displayed and exits. I dont want that there. btw thanks for all of the help! appreciate your commitment to helping me!

    edit: after several pair inputs, negative inputs make the entire program useless because previous values are used in the next input/out

    Quote Originally Posted by CommonTater View Post
    It can't be compiling... Line 15 should cause an error about incorrect types...
    Code:
    term = scanf("%f" "%f", &input1, &input2);
    Should be...
    Code:
    term = scanf("%f %f",&input1, &input2);
    Most likely the compile is failing but it's running a previous version of the program... so it SEEMS to be giving you the same results. Set your compiler warnings to maximum and treat each message as a problem to be corrected.
    Last edited by ecsx00; 10-08-2011 at 03:40 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please Help....error problems
    By princess_by_day in forum C Programming
    Replies: 6
    Last Post: 02-27-2011, 01:33 PM
  2. Problems with runtime error
    By azrael in forum C Programming
    Replies: 13
    Last Post: 03-20-2009, 04:53 PM
  3. Error Problems
    By Windu102 in forum C Programming
    Replies: 2
    Last Post: 08-24-2008, 08:42 AM
  4. error problems
    By meeloff in forum C++ Programming
    Replies: 3
    Last Post: 12-09-2004, 04:46 PM
  5. Stack Error Problems
    By Ice in forum C Programming
    Replies: 1
    Last Post: 04-24-2002, 04:28 PM

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