Like Tree3Likes
  • 1 Post By anduril462
  • 1 Post By rcgldr
  • 1 Post By rcgldr

How to use fgets

This is a discussion on How to use fgets within the C Programming forums, part of the General Programming Boards category; Currently I am doing the first exercises from Illustrating C. The exercise that I am trying first is the one ...

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    5

    How to use fgets

    Currently I am doing the first exercises from Illustrating C. The exercise that I am trying first is the one where someone can input degrees and the program will be able to put those in to sin or cos. Im trying to use fgets to take input from the user. the answer can only be sin or cos. Im having trouble with how to get it to work.

    My goal is to have the output of the choice sin or cos. Store that choice. proceed to ask what the degrees are from the user. then i would have the degree input multiplied by pi/180 converting it to radians and having the program compute it that way

    Any help is greatly appreciated.

    this is what i have so far
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
     int a;
    int main()
    {
        char text;
        int x = 3.14159265358979323846264338327;
        printf ("\n Would you like to compute a sin or cos function? \n");
            fgets( text, 3, stdin);
        printf("\n %s \n",text );
    
    
    return 0;
    
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,489
    man page fgets section 3

    The first thing you need is an array of characters.
    Eg
    char buff[100];
    fgets(buff,sizeof(buff),stdin);
    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.

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Don't use global variables if it can be avoided. I.e., get rid of that "int a" (or put it in main).


    ints can only hold, well, integers. You're trying to stuff a "real" number into x, which is just an int. You need to declare x as a floating point number. The usual choice is double.


    fgets reads a line of input into a string. A string in C is an array of char. You've only defined text to be a single char. It will need at least 5 chars to hold a 3 character input. This is due to 2 extra characters, the newline and the nul character that indicates the end of the string. Since the newline is left in the string, you'll have to either remove it or simply compare it to strings that end in a newline, like "sin\n".
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,459
    First, you should be more descriptive than "having trouble with how to get it to work." Does it compile? If not, copy-paste the exact error messages with line numbers. Does it not appear to run? Does it crash or give incorrect output? What is the exact input you gave it, what output did you actually get and what output did you expect to get. Since you didn't specify, I'll just point out problems I see.

    • Your indentation/formatting is inconsistent. Keeping your code neat and orderly improves readability. If your code is difficult to read, it's easy to make mistakes, but hard to find and fix them. For such a small program, it's not a big deal, but get in a good habit early. Once your programs grow beyond a few dozen lines, and start including conditionals, loops and functions, good formatting will become extremely important.
    • Why do you have a global variable named a? Globals are rarely the right solution (they certainly aren't here). Read this link, then remove the declaration.
    • Why do you declare x as an int, then try to store a non-integer number in it? All those digits to the right of the . are truncated (i.e. thrown away), so x is just 3. You should use a double type for real numbers. And why is the variable named x? Why not call it pi, since that's what it is? Better yet, define a constant, PI (note the convention of using all uppercase for constants), since pi will always have the same value.
    • Read the documentation for fgets (here). It reads in a string (a bunch of sequential characters -- see my next point). Pay particular attention to the part about how many characters it reads in: one less than size characters. Where size is the second parameter to fgets. That means, if you pass 3, it only stores 2 characters of input, and the third character stored is the null terminator.
    • You try to read multiple characters into text, but you only declared text to be a single character. Perhaps you should declare it as an array of char, to hold the whole string the user enters.
    • Remember that fgets stores the newline (the user presses enter after they input 'sin' or 'cos'), so you need to remove it. You can see an example of how to do that here (it's the strchr part).


    Get all that sorted out. Then, figure out how you would do this yourself with paper and pencil. If you can't write down the steps with pencil and paper, then you wont be able to program a computer to do it. Once you have those steps, it should be much easier to write the code. One heads up: the sin and cos functions (read those links for documentation on those functions) take as arguments the angles in radians, so you'll need to convert the user's input from degrees first.
    stahta01 likes this.

  5. #5
    Registered User
    Join Date
    Sep 2013
    Posts
    5
    This is my new code. I am getting errors on lines 16, 17 and 20. error: expected expression before 'char'

    Is my math allowed on lines 17 or 20?

    Also, when i run the code, the scanf function does not recieve input on a new line. It is on the same line as the second question. How can i make the input flashing _ appear under the second question
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    
    int main()
    {
        double degrees;
        double answer;
        char text[4];
        double PI = 3.14159265358979323846264338327;
       
    
        printf ("\n Would you like to compute a sin or cos function? \n");
            fgets( text, 4, stdin);
        
        printf("\n How many degrees does the angle have?" \n);
             scanf (" %f ", degrees );
    
        if (char text = sin){
            answer = sin(char text*180/(double PI));
    }
    else {
            answer = cos(char text*180/(double PI))
    }
        printf("\n %.2f \n", answer );
    
    
    return 0;
    Last edited by Nik Brkic; 09-06-2013 at 04:33 PM.

  6. #6
    Registered User
    Join Date
    Sep 2013
    Posts
    5
    Code:
    #include <stdio.h>
    #include <math.h>
    #include <string.h>
    
    int main()
    {
        double degrees;
        double answer;
        char text[4];
        double PI = 3.14159265358979323846264338327;
    
    
        printf ("\n Would you like to compute a sin or cos function? \n");
            fgets( text, 4, stdin);
    
        printf("\n How many degrees does the angle have? \n");
             scanf ("\n %f ", degrees );
    
        if (char text = sin){
            answer = sin(double degrees*180/(double PI));
    }
    else {
            answer = cos(double degrees180/(double PI));
    }
        printf("\n %.2f \n", answer );
    
    
    return 0;
    
    }

  7. #7
    Registered User
    Join Date
    Jun 2005
    Posts
    6,255
    1) scanf() requires addresses of all arguments specified in the format string, not values.

    2) The statement "if (char text = sin)" is meaningless. An if statement is not allowed to declare a variable. Even if you could define a variable of type char named text (given that one already exists), sin is a function. A variable of type char cannot be initialised with the address of a function.

    2a) fgets() has read a string. If you want to compare the string with (say) "sin", then do a string comparison (like strcmp() which is declared in <string.h>).

    3) The statement "answer = sin(double degrees*180/(double PI))" is also meaningless. Remove the keyword double from everywhere in it. It is not necessary to tell the compiler that degrees is of type double, since the compiler already knows (as degrees was declared previously). And, in fact, it is illegal to do so.



    Your recurring problem is that you've skimmed your introductory material (textbook, course notes, lecturer comments) and are now making half-arsed guesses about how to proceed. Try taking the time to understand the material BEFORE trying to write code.
    Right 98% of the time, and don't care about the other 3%.

  8. #8
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Wow. There's more errors than working code.
    You need to read your C book more carefully.
    Go through the examples and exercises.


    Here's some points about this program:
    * Once you've declared a variable you don't need to keep mentioning it's type all over the place. In fact, it's an error to do so.


    * I believe I said last time that you need at least 5 characters in your text variable to properly hold the input. I'd declare it as at least 80.


    * Your scanf format needs to be %lf to read a double. (That's the letter ell, not a one.) And scanf requires the address of the variable, not its value, so you have to use the address operator, the ampersand &, in front of degrees.


    * You used a single = when you meant ==. However, even == is wrong here, since you're comparing to a string. You must use the strcmp function. And a string constant must be enclosed in double quotes.

    EDIT: I've pretty much just repeated what grumpy said, even about not reading your book closely enough!
    Last edited by oogabooga; 09-06-2013 at 06:31 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    1,277
    Although you can implement workarounds, it's probably not a good idea to use both fgets() and scanf() in the same program. Instead always use fgets() to read a line of data into a buffer, then use sscanf() to extract a string or to read a value from the line of data in the buffer.
    stahta01 likes this.

  10. #10
    Registered User
    Join Date
    Sep 2013
    Posts
    5
    Quote Originally Posted by grumpy View Post
    Your recurring problem is that you've skimmed your introductory material (textbook, course notes, lecturer comments) and are now making half-arsed guesses about how to proceed. Try taking the time to understand the material BEFORE trying to write code.
    i agree i it may look half-assed but I am not currently in a CS class. Im using Illustrating C to learn C and this is the second chapter: Concepts. There are 4 exercises at the end that challenge that asks the reader to make a program to meet the needs of the question. Do you feel I should read the whole book before trying these end of chapter questions?

    http://i278.photobucket.com/albums/k...ps9d052c29.png

    Question
    Is the math legal on line 20 and 23

    rcgldr, are you saying that I should replace my scanf with fgets to read in my degree value? What kind of trouble can I get in by having both fgets and scanf?

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    1,277
    Quote Originally Posted by Nik Brkic View Post
    rcgldr, are you saying that I should replace my scanf with fgets to read in my degree value? What kind of trouble can I get in by having both fgets and scanf?
    Your program won't get into trouble if you use fgets() and scanf(). It's just that if you use fgets() to read a line at a time into a buffer, then sscanf() on that buffer, you don't have to worry about keeping track of when scanf() should be skipping past newlines.
    vart likes this.

  12. #12
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,459
    Quote Originally Posted by Nik Brkic View Post
    i agree i it may look half-assed but I am not currently in a CS class. Im using Illustrating C to learn C and this is the second chapter: Concepts. There are 4 exercises at the end that challenge that asks the reader to make a program to meet the needs of the question. Do you feel I should read the whole book before trying these end of chapter questions?

    http://i278.photobucket.com/albums/k...ps9d052c29.png
    This is a totally superficial judgement, but based on the fonts used in your screen shot, and the fact that EVERY SECTION TITLE IS SCREAMING AT YOU IN ALL CAPS!!!!!!111!!1!!1!!!, it seems like not the best book/e-book out there. Honestly, I can't personally recommend a book (I took C 15 years ago, and frankly learned most of it on the job, on this forum, and from the standard), but we do have a sticky thread with book recommendations.

    Definitely don't read the whole book (10-11 chapters) before doing the exercises from chapter 2. Find a better book, and take your time with it. Really study the examples, and when you write code, compare it to the examples, and see how it's different. I would be, even in you (seemingly poor) book, they don't mention the type of the variable every single time they reference it.
    Quote Originally Posted by Nik Brkic View Post
    Question
    Is the math legal on line 20 and 23
    Well, considering line 23 is missing the multiplication operator, then it's definitely wrong. But also, your conversion is "upside down". You should take a look into dimensional analysis. Basically, you start out with degrees "on top", i.e. you have a certain number of degrees. You want to convert that to radians, i.e. you want to end up with radians "on top". So to cancel out the degrees, you need to multiply by some conversion with degrees on the bottom (to cancel out your original degrees), and radians on top. So you should convert the user input like so
    Code:
    rad = deg * PI / 180;
    Quote Originally Posted by Nik Brkic View Post
    rcgldr, are you saying that I should replace my scanf with fgets to read in my degree value? What kind of trouble can I get in by having both fgets and scanf?
    To elaborate on what rcgldr said: Using fgets everywhere is one way. You use fgets to read a whole line of user input, then you use sscanf (note the extra 's' for scanning a string (read from the keyboard by fgets) instead of scanning the keyboard directly) to scan the input for integers, doubles, etc. The reason is that scanf (only one 's', scanning directly from the keyboard) has some particular behavior, especially with the %c operator or when it comes to handling bad user input (people are great at making typos). Otherwise, it can get a little difficult to keep track of who is responsible for handling the new line (i.e. when the user presses enter), since fgets, scanf and getchar are all a little different in this regard.

    The summary is: use one method to read from the keyboard (preferably not scanf). Then use whatever functions to parse that input.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets help
    By automagp68 in forum C Programming
    Replies: 4
    Last Post: 04-23-2012, 01:22 AM
  2. Help using fgets()
    By ICool in forum C Programming
    Replies: 7
    Last Post: 09-10-2007, 06:24 AM
  3. fgets() vs gets()
    By alexnb185 in forum C Programming
    Replies: 10
    Last Post: 08-24-2007, 06:07 PM
  4. fgets help
    By mapunk in forum C Programming
    Replies: 4
    Last Post: 11-30-2005, 06:41 AM
  5. fgets
    By G'n'R in forum C Programming
    Replies: 8
    Last Post: 09-18-2003, 12:24 AM

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