nooby question C

This is a discussion on nooby question C within the C Programming forums, part of the General Programming Boards category; Hi, This question might have been covered before but im gonna ask again. Say you ask from the user the ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    54

    nooby question C

    Hi, This question might have been covered before but im gonna ask again.

    Say you ask from the user the type in a value such as f1.2 or f5 etc. now you want to get the value and put it into a formula but u dont need the f u only need the number, is there a way to get the number out of it and use it?

    ta

  2. #2
    Registered User
    Join Date
    Aug 2007
    Location
    MD, USA
    Posts
    71
    >> is there a way to get number out of users input of f1.5<<
    Yes, several, but to keep it simple for you I think you could use
    scanf("&#37;f", &doubleblah);
    ...and it will skip the 'f' and begin reading at the 1 and stop reading at a non-digit, whitespace or \n and place that value in the variable.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    > This question might have been covered before but im gonna ask again.
    Well at least twice before, and by YOU
    C error please help
    C problem (simple)

    Not to mention the 3 or 4 of your cohorts which seem to be in the same class as you, and all having the same problem of how to parse "f1.2" into two separate fields.

    This really is getting tiresome to explain over and over.
    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.

  4. #4
    Registered User
    Join Date
    Sep 2007
    Posts
    54
    Thank you howardL for that info ill give it a try.

    TO Salem: you dont have to answer if you dont want to no ones forcing you. you can just ignore it. there is no rule saying im not allowed to ask the same question over and over again.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by HowardL View Post
    >> is there a way to get number out of users input of f1.5<<
    Yes, several, but to keep it simple for you I think you could use
    scanf("%f", &doubleblah);
    ...and it will skip the 'f' and begin reading at the 1 and stop reading at a non-digit, whitespace or \n and place that value in the variable.
    Except that won't work. scanf() doesn't skip over alpha characters when looking for floating point numbers.

    --
    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.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by juststartedC View Post
    TO Salem: you dont have to answer if you dont want to no ones forcing you. you can just ignore it. there is no rule saying im not allowed to ask the same question over and over again.
    No, but the forum rules state
    Post once: don't keep pressing post. Duplicates will be deleted. Do not cross post; almost everyone reads most of the important boards.
    Whilst you technically haven't broken either of those rules by the letter, you haven't exactly been following the spirit of the rule either.

    --
    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.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    54
    so what will work? i could always ask the user to type 1.2 instead of f1.2 etc but that would be kind of cheating i guess and i will probably lose some marks.

    Only if there was a simple way...

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, putting my dislike for scanf() aside, the solution has been given a few times in the past to other of your classmates doing the same excercise:
    Code:
    #include <stdio.h>
    
    int main() {
    	for(;;) {
    		float f;
    		int x = scanf("f%f", &f);
    		getchar();
    		printf("x = %d, f = %f\n", x, f);
    	}
    	return 0;
    }
    Note that you should check the return value from scanf() and make sure that you got the right number of "results" (in this case 1). Try this app with some different inputs, e.g. "f8.0", "F8.0", "8.0" and such.

    --
    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.

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    There is,

    Code:
        char letter;
        float number;
    
        if(scanf("&#37;c%f", &letter, &number) != 2)
        {
            /* invalid */
        }else{
            /* valid, validate letter see if it's 'f' or something */
        }
    Stop asking already.

    if you want to allow, uppercase and lowercase f's:
    Code:
    /* ... */
    scanf("%*[fF]%f", &number);
    Or something, or use the above and check letter = 'F' or letter = 'f'

    Edit: See matsp's post, it's probably better
    Last edited by zacs7; 09-20-2007 at 04:13 AM.

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    54
    okidoki thanks a lot guys sorry to keep asking the same question. you may blame the lecturer

    I just cant afford to fail this unit, ill be kicked out of uni

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  2. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  3. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  4. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 12:47 AM

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