Thread: Flexibility with reading one or two strings?

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    30

    Flexibility with reading one or two strings?

    While programming I bumped into a block that required me to scan in a name that can take a first name OR a full name. Before this I was using a scanf to read a string, and obviously it does not work for flexibility for one or two strings.

    Here's how my program is running.

    name:
    age:
    hbp:
    lbp:

    where name is a string, age is an int, hbp lbp a float. I tried using fgets but then messes things up like this:

    name: age:

    and doesn't take a name at all. Any help would be great.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You could read up on how scanf works, or you could read a whole block with fgets; the things your book or lesson should have or should be covering right about now.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    30
    Well, as I said above, I've already tried fgets() but it does not prompt for an input. I've also tried delimiters and other functions to get what I wanted to read either just one or two strings but is not working.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Using fgets followed by sscanf is a good idea, because then you can use #defines for your buffer sizes instead of magic numbers.

    C doesn't have native regular expression support, so the usual way to specifify at each point what you expect next. How should the program know where the end of the name is? You seem to want to allow having a line with nothing in the name field, instead the age field immediately follows, but how would your program know that the person's first or last name is not "age"?
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    30
    No, this is what I used to have in my program.

    Code:
        
            printf("name: ");
            scanf("%s", &patients[count]->name);    
            printf("age: ");
            scanf("%d", &patients[count]->age);
            printf("high bp: ");
            scanf("%f", &patients[count]->hbp);
            printf("low bp: ");
            scanf("%f", &patients[count]->lbp);
    However, for the name portion of it, I need it to have the option of reading one or two strings. First name or full name. I am trying what you suggested right now, though I am not having any luck doing so at the moment.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by bungkai View Post
    Well, as I said above, I've already tried fgets() but it does not prompt for an input.
    What are you even trying to say here? fgets just takes a block/line of input, you still have to put something like "printf( "Enter a line: ");", that's not built into fgets. Besides, you saying fgets doesn't work tells me nothing - and since I've used fgets successfully in the past, leads me to believe that you don't know what you are talking about. Which is why you should be posting your example of it not working.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Just replace your first scanf() with an fgets statement into a temp buffer. Then store the value into your structure. Take a look at how to get a line of text.


    EDIT: You don't need to use a temp buffer btw. Just threw it out there.
    Last edited by AndrewHunter; 07-09-2011 at 10:04 PM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    30
    What I'm trying to say is that with fgets(), my output becomes:

    name: age:

    instead of:

    name:
    age:

    It also completely ignores taking a line of text and skips to the next scanf.
    char buff[20];
    fgets(buff, sizeof(buff), stdin);

    I don't think there's anything wrong here, but I can't seem to get it working

    quzah: sorry for my lack of clarity, I meant take in an input. Not prompt.

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Then it is something that you are doing wrong in your code. Here is a short example code:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    typedef struct{
    	char name[20];
    }myStruct;
    
    int main(void){
    
    	myStruct example;
    
    	printf("Enter your name:");
    	fgets(example.name,sizeof(example.name),stdin);
    
    	printf("%s", example.name);
    	getchar();
    	return (0);
    }
    It is possible that you have a trailing newline char in your buffer from some previous input operation. Did you ask the user for anything else prior to this?
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by bungkai View Post
    char buff[20];
    fgets(buff, sizeof(buff), stdin);

    I don't think there's anything wrong here, but I can't seem to get it working

    quzah: sorry for my lack of clarity, I meant take in an input. Not prompt.
    I didn't mean just show me how fgets works, I already know how it works. I meant post your actual program that you are having problems with. The whole thing, and your sample file you are trying to read.


    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Ok, so you have this:
    Code:
    while((scanf("%s", &c)) != EOF)
    Although you declared c to be a character, not a string. This may lead to problems. The newline character is still in the buffer from your scanf call. You need to deal with that before you try to take anymore input using fgets because fgets will stop at the newline character.

    You should check the return of malloc, plus there is no need to cast it.

    Try to reorganize your code so you you do not need global variables, these will tend to lead to all sorts of bad behavior.
    Last edited by AndrewHunter; 07-10-2011 at 12:47 AM.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  12. #12
    Registered User
    Join Date
    May 2011
    Posts
    30
    AndrewHunter, I added this:

    Code:
            while((scanf("%s", &temp)) != EOF)
            {
                    if (temp != '\n')
                    {
                            c = temp;
                    }
    I used temp as a placeholder and it still doesn't work.

    As for the other problems, I will try adjusting to it if I can get this problem fixed.

  13. #13
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Ok, so the "%s" tells scanf you want a string input. You want the "%c" which means character. The extra newline is in there because the first scanf() call only takes the 'n' (for example) out of the buffer but leaves the '\n' (user hitting enter) in the buffer. So:
    Code:
    while((scanf("%c", &c)) != EOF)
    {
          getchar();
          if(c == .....
    Give that a shot.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  14. #14
    Registered User
    Join Date
    May 2011
    Posts
    30
    Well, it works for the first, third, fifth and so on times. However, the second, fourth, and 6th go a little loopy and I don't really know what's going on :S. Sometimes I'm getting the same problem as before on the even number tries and sometimes it doesn't do as it's told. (Press D and assumes pressed N)

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    %s in scanf stops on whitespace, but does not read the whitespace, leaving it waiting for whatever you read next. Therefore, when you call fgets next, it immediately sees the newline, and pulls it off of the input stream, because that's what fgets does. You should generally pick one method for taking input, and use it the whole way through. If you want to use scanf then do so everywhere, and if you want to use fgets then use that everywhere.

    The problem is that after you ask it for a letter or a word, they hit enter, and you ignore the fact that they have it enter, and then wonder why it's skipping the next prompt you have. Like I said the first time, you should read up on scanf and fgets, and pick one, and use that. Just randomly throwing different pieces in and hoping they magically fix it isn't usually a good idea. It's best to know why something does or doesn't work.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with reading strings from a file
    By ldcel in forum C Programming
    Replies: 3
    Last Post: 12-01-2007, 01:31 AM
  2. Reading Strings from a file.
    By RealLife in forum C++ Programming
    Replies: 16
    Last Post: 05-12-2007, 09:17 PM
  3. Reading Strings
    By dacbo in forum C Programming
    Replies: 13
    Last Post: 11-10-2005, 02:08 PM
  4. reading and storing strings
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 02-01-2002, 12:38 PM
  5. Reading Strings
    By blsst40 in forum C Programming
    Replies: 1
    Last Post: 11-20-2001, 12:29 PM