Thread: if function not working (its inside anot if function)

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    2

    if function not working (its inside anot if function)

    i am trying to do a program like a weight lost program
    so it will take in value such as the BMR and show how much you have eaten extra and according to that i need to add the excess into the current weight, my program work untill the switch part, i have problem in the (if ,if else part) only the first if work the rest doesn't. please help me!



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    
    float curweight;
    float *pcurweight = &curweight;
    float idealweight;
    float burn;
    float daily;
    float lostweight;
    float howmuchburn;
    float height;
    float age;
    float BMR;
    float excess;
    *pexcess=&excess;
    
    
    
    
    char gender;
    *pgender=&gender;
    int main()
    {
    FILE *createfile=fopen("D:\\Desktop\\project.txt","a");
    //Read the file to check if there is any number stored//
        {
        FILE *createfile=fopen("D:\\Desktop\\project.txt","r");
        fscanf(createfile,"%f %f %f",&BMR,&idealweight,&curweight);
        printf("%.2f",curweight);
        printf("%.2f",BMR);
        printf("%.2f",idealweight);  //just to check the current weight//
        }
    
    
    //if there is no current weight stored,it will promt to store//
    if(curweight==0)
        {
        FILE *createfile=fopen("D:\\Desktop\\project.txt","w");
        printf("Welcome!\n");
        printf("Please enter if male or female(M/F)");
        scanf("%c",&gender);
        *pgender=toupper(gender) ;
        printf("Please enter your current weight in kg");
        scanf("%f",&curweight);
        printf("Please enter your current height in cm");
        scanf("%f",&height);
        printf("Please enter your age");
        scanf("%f",&age);
        printf("Please enter your desired weight");
        scanf("%f",&idealweight);
        fprintf(createfile,"%f\t",idealweight);   //print ideal weight into file//
    
    
    switch( gender )
        {
            case('M'):
            printf("Calculating BMR for male:\n");
            BMR=((66.5 + (13.75*curweight) + (5.003*height) - (6.755*age))*1.2);
            break;
    
    
            case('F'):
            printf("Calculating BMR for female:\n");
            BMR=((55.1 + (9.563 *curweight) + (1.850*height) - (4.676*age))*1.2);
            break;
    
    
        }//switch//
    
    
        printf("\nIf you engage in no activity for the entire day is,your body will burn %.2f kcal(BMR).",BMR);
        fprintf(createfile,"%f\t",BMR); //print BMR into file//
        printf("\nPlease enter your how much calories you have consumed today");
        scanf("%f",&daily);
    
    
    
    
        excess=BMR-daily;//excess or lack
    
    
    
    
    if(excess<0)
    printf("You have consumed %f more then how much your body can burn.",excess);//to display positive number//
    
    
    *pexcess=((1/7700)*excess);//change into kg
    printf("You excess %f(kg).",excess);   //test//
    *pcurweight=curweight+excess;   //must plusointo current weight(kg)//
    printf("You current %f(kg.",curweight);//test//
    
    
    
    
    
    
        else if(excess=0)
        printf("Perfect!.",excess);
    
    
        else if(excess>0)
       {
        printf("Its not healty to consume lesser than your BMR");
        printf("You have consumed %f lesser than your BMR",excess);
       * pexcess=((1/7700)*excess);//change into kg
        printf("You excess%f(kg.",excess);//test//
        *pcurweight=curweight-excess;//must minus ointo current weight(kg)//
        printf("You current %f(kg.",curweight);
      }
    
    
    
    
        }//if there is not value in the file//
      
    
    
    else
    
    
    {
        printf("Your current weight is %.2f",curweight);
        printf("Please enter your how much calories you have consumed today");
        scanf("%f",&daily);
    
    
     *pcurweight=curweight + daily  ;
    
    
    printf("current %.2f",curweight);
    fprintf(createfile,"\ncurrent weight is%.2f",curweight);
    } 
    
    
    
    
    
    
    
    
    fclose(createfile);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You are looking at the last diagnostic (aka error message, warning, etc) issued by your compiler (which will be at the bottom of your screen or window).

    Scroll up and look at the first diagnostic issued by your compiler.

    Compilers tend to become a little confused when they encounter a problem. That affects how they respond to subsequent code. So the first few diagnostics are often easy to track back to the cause. Subsequent ones will be more garbled.


    A few things I can see at a quick glance.

    1) There are at least two cases of variables being used without being declared. Those will trigger compiler errors before the compiler even reaches any "if" statement. [That is how I know you are working through the diagnostics backward - you asked about problems later in the code without mentioning earlier problems that your compiler WILL complain about.]

    2) if you wish to have an if or else control multiple lines of code, you need to put that code into curly braces. Your failure to do that will be responsible for a number of error messages from your compiler.

    3) In the "else if (excess = 0)" on line 100, the = sign is an assignment. This will change excess to have a value of zero and, since the expression "excess = 0" has result zero, the test is false.

    4) Your indentation is terrible, which makes it hard to be sure what code is associated with what.

    5) The first 3 lines or in main() call fopen() with the same file name, and different modes. This will make the second call pointless - as it will fail at run time.

    6) You don't check if any I/O statements (calls to fopen(), fscanf(), printf(), etc) succeed. This won't trigger compiler diagnostics, but does increase chances that your code - if you get it to compile - will not do what you expect.


    Fix the problems above - on your own - and you will learn something useful.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Sounds more like they are taking pot-shots at what their problem is.

    I'm guessing they weren't even compiling with warnings enabled at all, let alone looking at the messages it spews out on that code.

    Compile with warnings enabled, fix all the warnings and errors you get. If they don't show you where the problem was, come back and have another go with your cleaned up code.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers and function calls inside a function
    By lexitron in forum C Programming
    Replies: 1
    Last Post: 12-03-2011, 05:43 PM
  2. Creating an scanf function inside an function
    By anserudd in forum C Programming
    Replies: 4
    Last Post: 03-25-2011, 09:19 AM
  3. Calling a function inside a function
    By kizyle502 in forum C Programming
    Replies: 1
    Last Post: 09-17-2009, 11:29 AM
  4. Replies: 7
    Last Post: 04-11-2009, 09:44 AM
  5. how to use a member function inside a function pointer?
    By mickey in forum C++ Programming
    Replies: 6
    Last Post: 10-17-2002, 04:27 AM

Tags for this Thread