Thread: Beginner to C need help

  1. #1
    Registered User
    Join Date
    Aug 2013
    Posts
    4

    Beginner to C need help

    Hey guys just saw this forum today and joined.
    I am new to C(and programming actually) soo please bear with me
    I have to make a gas mileage calculator which should display the average miles per gallon after taking inputs from the user using ONLY the for loop(no break statements or anything). If the user presses -1 on the input where it says enter gallons then the program should terminate. Heres the code(I havent completed it ofc.. Im having trouble with the looping):
    Code:
    #include<stdio.h>
    #include<conio.h>
    int main(void){
    float gallon=0, mile=0, tgmile=0;
    int lop=0, num=1;
    printf("Enter gallons used(-1 to exit)");
    scanf("%d", &gallon);
    if(gallon==-1){
    
    
    lop=-1;
    }
    
    
    for(lop;lop==-1;num++){
    
    
    printf("\nEnter miles driven");
    scanf("%d",&mile);
    if(lop!=-1){
    printf("\nEnter gallons used(-1 to exit)");
    scanf("%d", &gallon);
    gallon=lop;
    }
    }
    return 0;
    }

    The way Ive tried to do is to ask the input about gallons first before starting the loop and if the input is -1 then i should assign -1 to lop variable so the for loop doesnt execute. But if I give some number other than 1 to the first input it still doesnt execute the for loop. Any help? I have to use only the for loop no break statements or goto or anything like that
    the num variable will be used for calculating the average and its assigned 1 since it started before the for loop and tgmile is for storing the complete average gallon per mile of EVERY input.
    Basically it should continue asking for input of gallon and miles till the user presses -1 on the gallon input
    Last edited by spiderslayerx; 08-31-2013 at 12:07 AM.

  2. #2
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Code:
    for(lop;lop==-1;num++)
    Your loop is saying while lop is -1 keep getting input. Perhaps you want to terminate the loop when lop does == -1; i.e. keep looping while lop != -1

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    A for loop is probably the wrong kind of loop. Especially since I don't see why num is useful. I would use a while loop. The only other thing I would do is make sure that the code that calculates is under an if statement, so if the user decides to quit, most of the loop won't do anything:

    Code:
    while gallons not equal to -1:
      prompt and get miles
      prompt and get gallons 
      if gallons not equal to -1:
        do calculation
      end if
    end while
    Try making those steps into code...

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You need to use the specifier %f (for floats) in your scanf, not %d (which is for ints).
    And get rid of conio.h since you're not using it.
    Last edited by oogabooga; 08-31-2013 at 12:28 AM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Aug 2013
    Posts
    4
    well I think I fixed the problem by eliminating the lop variable completely to stop using redundant ifs and just using the gallon variable in the for loop and replacing the == in the for by >-1 heres the new code:
    Code:
    #include<stdio.h>
    #include<conio.h>
    int main(void){
    float gallon=0, mile=0, tgmile=0;
    int num=1;
    printf("Enter gallons used(-ve number to exit)");
    scanf("%f", &gallon);
    for(gallon;gallon>-1;num++){
    
    
        printf("\nEnter miles driven");
        scanf("%f",&mile);
        printf("\nEnter gallons used(-ve number to exit)");
        scanf("%f", &gallon);
    
    
    }
    return 0;
    }
    its working now(and its not finshed ofc) and terminating for all numbers less than 0(but I want it to end somehow when I only press -1)

    Quote Originally Posted by SirPrattlepod View Post
    Code:
    for(lop;lop==-1;num++)
    Your loop is saying while lop is -1 keep getting input. Perhaps you want to terminate the loop when lop does == -1; i.e. keep looping while lop != -1
    Oh yea I really have trouble with this sort of thing. I tried your code it does run for numbers greater than -1 but when i press -1 it still runs.. any reason why this is happening?

    Im not supposed to use the while loop. Just for. Ive managed to make it work like this and it works but I just want it to terminate when I press -1 not terminate when its less than -1
    Last edited by spiderslayerx; 08-31-2013 at 12:36 AM.

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by spiderslayerx View Post
    but when i press -1 it still runs.. any reason why this is happening?
    Yes. Read my previous post.

    And get rid of the code that asks for gallons outside of the loop.
    That's pointless.
    Look again at whiteflags pseudocode and try to implement it.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Code:
    float gallon=0, mile=0, tgmile=0;
    int lop=0, num=1;
    printf("Enter gallons used(-1 to exit)");
    scanf("%d", &gallon);
    %d is used to read integers. gallon is a float. They are represented totally differently, and what you've done here is write an integer value into an address occupied by a float. Because the representations are different, the value of gallon interpreted as a float won't resemble the number you entered. Use %f for float: printf - C++ Reference

  8. #8
    Registered User
    Join Date
    Aug 2013
    Posts
    4
    Quote Originally Posted by oogabooga View Post
    Yes. Read my previous post.

    And get rid of the code that asks for gallons outside of the loop.
    That's pointless.
    Look again at whiteflags pseudocode and try to implement it.
    Ive put the gallons input outside of the loop because if the user presses -1 it should end immediately. If I put it insde the loop then press -1 then it would ask for miles and then end the program. I have to end the program as soon as -1 is pressed on gallons wihtout taking the miles input wihtout using break statements or goto

    Quote Originally Posted by smokeyangel View Post
    Code:
    float gallon=0, mile=0, tgmile=0;
    int lop=0, num=1;
    printf("Enter gallons used(-1 to exit)");
    scanf("%d", &gallon);
    %d is used to read integers. gallon is a float. They are represented totally differently, and what you've done here is write an integer value into an address occupied by a float. Because the representations are different, the value of gallon interpreted as a float won't resemble the number you entered. Use %f for float: printf - C++ Reference
    yup I think this is my problem right here.

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    So now you've got the %f thing sorted, doesn't it work if you just do this?

    Code:
    for(gallon;gallon != -1;num++){

  10. #10
    Registered User
    Join Date
    Aug 2013
    Posts
    4
    Quote Originally Posted by smokeyangel View Post
    So now you've got the %f thing sorted, doesn't it work if you just do this?

    Code:
    for(gallon;gallon != -1;num++){
    yes I finally got it working like I want thanks all

    Now I got something to ask.. if you enter a decimal value in a float variable a(like 1.5 or something) and store it in an int b, will only 1 be stored?(assuming a is 1.5)

    Also if I input like a 1(not 1.0000 or something similar) in a float variable.. will it be stored as 1.00000.... or something like 0.99999?

  11. #11
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by spiderslayerx View Post
    Ive put the gallons input outside of the loop because if the user presses -1 it should end immediately. If I put it insde the loop then press -1 then it would ask for miles and then end the program. I have to end the program as soon as -1 is pressed on gallons wihtout taking the miles input wihtout using break statements or goto
    Blah blah blah.
    Did you even read whiteflags post?
    What an idiot....
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  12. #12
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Hmmmmmm

  13. #13
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by spiderslayerx View Post
    Now I got something to ask.. if you enter a decimal value in a float variable a(like 1.5 or something) and store it in an int b, will only 1 be stored?(assuming a is 1.5)
    Yes - if you have a float and assign it to an int, the fractional part is thrown away. Even if it was 1.9999, it'd still be 1.

    Quote Originally Posted by spiderslayerx View Post
    Also if I input like a 1(not 1.0000 or something similar) in a float variable.. will it be stored as 1.00000.... or something like 0.99999?
    It'll be stored precisely as 1.0000. Integers can be stored accurately in floats, up to a certain number (around 24 bits I think).
    The problem comes as soon as any fractional parts are introduced. Just like one third can't be expressed precisely in decimals, some numbers can't be expressed precisely in binary floating point. For instance 0.11 will be stored as 0.10999999

  14. #14
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Whoa, can we not call people idiots? That's a little harsh and doesn't seem to be constructive in a place that is intended for beginners.

    Honestly, if you're good at programming then what are you doing here? This forum has no use to someone who is advanced so calling anyone an 'idiot' to me is a grievous offense.

    As the King of the Beasts and the inheritor of the Yawarakai-Te I have spoken and I dismiss you of all credibility, ooga. As far as I'm concerned, you're of no use and are hindering the sentient evolution of the human race so it's either go with the flow or drown.

    Also, please read things carefully, spider, and re-read them over and over again. Bashing your head on the wall is indeed exactly how it's supposed to feel.

  15. #15
    Registered User
    Join Date
    Jan 2011
    Posts
    144
    Quote Originally Posted by spiderslayerx View Post
    yes I finally got it working like I want thanks all

    Now I got something to ask.. if you enter a decimal value in a float variable a(like 1.5 or something) and store it in an int b, will only 1 be stored?(assuming a is 1.5)

    Also if I input like a 1(not 1.0000 or something similar) in a float variable.. will it be stored as 1.00000.... or something like 0.99999?
    Hi,
    please post the working code so we can help you further if there are any problems.

    Also, I see you have tried to indent your code. Here is an article on how this can be done better
    Indent style - Wikipedia, the free encyclopedia

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Help
    By maniac20 in forum C Programming
    Replies: 22
    Last Post: 05-01-2008, 09:46 PM
  2. Help for a beginner!
    By Crazy Glue in forum C++ Programming
    Replies: 20
    Last Post: 10-07-2005, 08:05 AM
  3. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 11:21 AM
  4. please help a beginner
    By karen66 in forum C Programming
    Replies: 5
    Last Post: 12-16-2002, 07:02 AM
  5. beginner
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 07-01-2002, 10:15 AM