Thread: Hi guys, I'm new and I got problems :)

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

    Hi guys, I'm new and I got problems :)

    C problems that is ^.^
    My name is Allen and it's my first time posting a thread here and I figure I'll introduce myself along with a problem I'm having. I'm new to C programming so I'm doing an assignment and the logic seems ok to me but for some reason when I enter lets say 50.99, the while loop won't work right. Here's my code
    Code:
    printf("What is the cost of small package(s) (in dollars, 0< cost < 500): ");
    scanf("%e", &s_pack);  //Storing the user's input
    //Using while loop to make sure the input is valid
    while (s_pack > 500 || s_pack < 0) {
    printf("The amount you have entered is not within the parameter.\n");
    printf("Please enter the cost of small package(s) in dollars between 0 and 500: ");
    scanf("%e", &s_pack);
    }
    I entered 50.99 which is obviously false in the while loop but the loop ran anyway. I'm pretty sure I did something stupid but I dont know what. I will appreciate it if anyone can help ^.^

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Add a printf() line of code as the very first line of your while loop, and see what value s_pack has.

    That would be basic debugging, and show you the problem.

    Code:
    while (s_pack > 500 || s_pack < 0) {
    printf("The value of s_pack is: %*\n", s_pack);
    
    printf("The amount you have entered is not within the parameter.\n");
    printf("Please enter the cost of small package(s) in dollars between 0 and 500: ");
    scanf("%e", &s_pack);
    }
    *Replace with either f for float or lf for double data type - or use the proper format for s_pack.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    6
    Oh wow! All I did was changed %e to %lf and it worked! Thanks so much! ^.^ I wonder why though o.o I read over c programming and %e should also work.

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    That's because %e is for floats and %lf is for doubles. They are different data types.
    Code:
    while(!asleep) {
       sheep++;
    }

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by linx2001 View Post
    C problems that is ^.^
    My name is Allen and it's my first time posting a thread here and I figure I'll introduce myself along with a problem I'm having. I'm new to C programming so I'm doing an assignment and the logic seems ok to me but for some reason when I enter lets say 50.99, the while loop won't work right. Here's my code
    Code:
    printf("What is the cost of small package(s) (in dollars, 0< cost < 500): ");
    scanf("%e", &s_pack);  //Storing the user's input
    //Using while loop to make sure the input is valid
    while (s_pack > 500 || s_pack < 0) {
    printf("The amount you have entered is not within the parameter.\n");
    printf("Please enter the cost of small package(s) in dollars between 0 and 500: ");
    scanf("%e", &s_pack);
    }
    I entered 50.99 which is obviously false in the while loop but the loop ran anyway. I'm pretty sure I did something stupid but I dont know what. I will appreciate it if anyone can help ^.^
    A little hint for next time... when posting code snippets you shoule also show us the declarations of any variables you are referenceing...

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Adak View Post
    That would be basic debugging, and show you the problem.
    That would be slightly condescending. You get that here from time to time.
    But luckily such things are overlooked when the associated advice does the trick anyway.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It's suggestive of a basic debugging method that I would use. It's described as a basic technique, because adding a print statement, is a basic debugging technique.

    At no time does it suggest the OP is somehow less intelligent than anyone else, and is certainly *not* condescending. I do get a bit sharp with the OP's that are showing very little effort, but this wasn't the case here.

  8. #8
    Registered User
    Join Date
    Sep 2011
    Posts
    6
    Thank you all for your suggestions and tips, probably somewhere in my head, I read the data type wrong and thought %e for double. I learned my mistakes and will improve to become better

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hey guys can i get some help please?
    By Damienmc++keown in forum C++ Programming
    Replies: 3
    Last Post: 04-20-2011, 06:53 PM
  2. Hey guys, I need a little help here~
    By readytogo in forum C Programming
    Replies: 7
    Last Post: 10-29-2010, 12:41 AM
  3. Replies: 5
    Last Post: 12-05-2008, 10:04 PM
  4. Hey Guys
    By valar_king in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-13-2004, 06:54 PM