Thread: getting an error ???

  1. #76
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I take it you didn't change the printf to use %f - that looks like a floating point number printed with %d.

    The reason the language has while and do-while and a few other constructs is exactly to avoid having goto's. They do the same thing in a more easy to read fashion.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  2. #77
    Registered User
    Join Date
    Aug 2007
    Posts
    59
    ...................
    Last edited by ki113r; 09-05-2007 at 10:02 AM.

  3. #78
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, if you enter f (or any other non-float-number stuff) as part of your input, then it will get stuck - like I said, scanf() can be very temperamental, and will quite easily get confused by "bad input".

    If it is a requirement to actually be able to tolerate user errors, then fgets() is the right way to go.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #79
    Registered User
    Join Date
    Aug 2007
    Posts
    59
    Ok , well ill get some sleep now, and ask my tutor about using fgets , thanks a lot for your help

  5. #80
    Registered User
    Join Date
    Aug 2007
    Posts
    59
    Turns out the user must enter the f value , and the program must process the whole thing . I think the way I will handle this is to use string compare. I think my tutor would prefer that to fgets. When I askd him this is the reply I rceeived :
    > Am I allowed to use a more advanced way if it simplifies the
    > procedure ?
    No. Look at style ( that he teaches in lectures and practicals ) NOT the code you use.

    What do you think is easier and more logical way for a beginner to use for my problem , to convert the input to a string use string compare to compare the apertures with valid values , or to use fgets ? We haven't been taught neither of the ways . Any suggestions ?

  6. #81
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    well, I'm thinking since the string you get for aperture is *always* in the form: f1.2 f1.4, etc, you have an f followed by a number. Thinking further, if you had a char array and the you wanted to convert that to a number via stroll(), you can't do it because the first char is not a number. So you could advance the array by one and then use stroll()...

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  7. #82
    Registered User
    Join Date
    Aug 2007
    Posts
    59
    Maybe there's a way to simply ignore the input of the first character , and store the rest in a string ?

  8. #83
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    There is a dead simple way; but why store the rest in a *string*, if you can ignore the first character and read the rest as a *float*???

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  9. #84
    Registered User
    Join Date
    Aug 2007
    Posts
    59
    Quote Originally Posted by QuantumPete View Post
    There is a dead simple way; but why store the rest in a *string*, if you can ignore the first character and read the rest as a *float*???

    QuantumPete
    If I can store it as a float it's even better . How would I do that ?

  10. #85
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    I'll give you an example and then you can work it out from there, I'm sure
    Code:
    float amount = 0.0f;
    printf  ("Enter the price like so: £100.23\n");
    scanf ("£%f", &amount);
    printf ("You entered £%f\n", amount);
    You could also use fgets to get the input as a string and then do something very similar with sscanf to get the float, like matsp said. I reccomend you go to your department library and get "The C Programming Language" by Kernighan and Ritchie (Second Edition). All this stuff is explained in there and will help you to fix simple problems by yourself

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  11. #86
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Mmm, 85 replies and we seem to be going round in circles. The question shouldn't be this hard.
    @ki113r, I would suggest you start a new thread, with your latest code and whatever specific questions you still have outstanding.
    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.

  12. #87
    Registered User
    Join Date
    Aug 2007
    Posts
    59
    Quote Originally Posted by QuantumPete View Post
    I'll give you an example and then you can work it out from there, I'm sure
    Code:
    float amount = 0.0f;
    printf  ("Enter the price like so: £100.23\n");
    scanf ("£%f", &amount);
    printf ("You entered £%f\n", amount);
    You could also use fgets to get the input as a string and then do something very similar with sscanf to get the float, like matsp said. I reccomend you go to your department library and get "The C Programming Language" by Kernighan and Ritchie (Second Edition). All this stuff is explained in there and will help you to fix simple problems by yourself

    QuantumPete
    Simple and effective I like it I am pretty sure there won't be more questions, the rest seems quite starightforward

  13. #88
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    You should also be seeing your tutor or whoever for help, while is place is great it shouldn't be your sole source of information.

  14. #89
    Registered User
    Join Date
    Aug 2007
    Posts
    59
    How would I limit the printf ouput to only 1 decimal when it prints out the float? I tried searching the FAQ to no avail

  15. #90
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by ki113r View Post
    How would I limit the printf ouput to only 1 decimal when it prints out the float? I tried searching the FAQ to no avail
    "The C Programming Language" by Kernighan and Ritchie (Second Edition) page 153/154

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM