Strings in Scanf function

This is a discussion on Strings in Scanf function within the C Programming forums, part of the General Programming Boards category; Ummmm what in the name of all that is holy are you doing it like that for? I was going ...

  1. #16
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Ummmm what in the name of all that is holy are you doing it like that for? I was going to post code but now that I have read you can't use arrays... I refuse to post anything other than a suggestion.

    You can always use %*s within printf() to print x number of characters in a string. So for the dashes, you can make a string literal with ample dashes in it, then use printf() with %*s to specify how many dashes you need per line segment.

  2. #17
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Btw, the reason it skips the enter game data is because of your lack of usage of scanf() with %s. Since you are explicitly telling it to grab characters, it is causing some input buffer issues. Right? Just think of it logically.

  3. #18
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    It skips the date because you forget a '\n' (newline) character when you read the five char for the visitor. For example this works, because in the variable clean there is the '\n' character read:
    Code:
    #include<stdio.h>
    
    int main(void)
    {
      int month, day, clean;
      char h1, h2, h3, h4, h5;
      char v1, v2, v3, v4, v5;
    
      printf("\nEnter home team: ");
      scanf("&#37;c%c", &h1, &h2);
      scanf("%c", &clean);
    
      printf("Enter visitor: ");
      scanf("%c%c%c%c%c", &v1, &v2, &v3, &v4, &v5);
      scanf("%c", &clean);
      
      printf("Enter game date (mm/dd): ");
      scanf("%d/%d", &month, &day);
    
      printf("\nHome\t  \tVisitor\t  \tDate");
      printf("\n----\t  \t-------\t  \t----");
      printf("\n%c%c  \t vs\t%c%c%c%c%c\t   on\t%d/%d/2008\n\n", h1, h2, v1, v2, v3, v4, v5, month, day);
    
      return(0);
    }
    EDIT: To be more clear.
    Whatever you press on the keyboard is stored in a buffer INCLUDING the <enter>, which is the \n character. So when you type UF there is actually three characters. U, F and \n. You have to read also the \n. If you don't, the next scanf will do that. So in v1 the \n is stored. You get my point.
    That is why the way you use scanf is flawed. It is a common mistake though. So try again keeping in mind that a \n character is entered when you press <enter>.
    Without using arrays, your program is fine. Except that it doesn't handle wrong input, but I don't think you want to get into that. Just keep in mind that if a team for example has more than 5 letters there will be an error again.
    Last edited by C_ntua; 09-10-2008 at 07:18 PM.

  4. #19
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    If teachers didn't institude useless rules, I wouldn't have to write scarey code like this...

    Example:
    Code:
    #include<stdio.h>
    
    int main(void)
    {
      int month, day, clean;
      double home, visitor;
    
      printf("\nEnter home team: ");
      scanf("&#37;7s", (char *)&home); // The cast shuts up warnings
    
      printf("Enter visitor: ");
      scanf("%7s", (char *)&visitor);
      scanf("%c", &clean);
      
      printf("Enter game date (mm/dd): ");
      scanf("%d%*c%d", &month, &day);
    
      printf("\nHome\t  \tVisitor\t  \tDate");
      printf("\n----\t  \t-------\t  \t----");
      printf("\n%s  \t vs\t%s\t   on\t%d/%d/2008\n\n", (char *)&home, (char *)&visitor, month, day);
    
      return(0);
    }
    Now your teacher can be happy, I can get flamed for even suggesting anything of the sort to a beginner, and we all live happily ever after. In fact, I would give you extra points for creativity if I were the teacher.

  5. #20
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    yeah don't worry, I originally would never do something like this because it's not logically, that's why I sort of need help with fixing up my code...

    Arrays would definitely be better

  6. #21
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    so can you explain for me (in conclusion) what the (char*) represents and does, and why there isn't a "clean" at the end of the first scanf for home?

  7. #22
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    1) Well, when you use the &#37;s (which stands for strings) in scanf, it scans all the input until it finds a whitespace. A whitespace is considered a newline, a space or a tab (\n, , \t). So it would read everything including the \n so no problem there, since it wont leave the \n behind. I believe you DO NOT need the clean in that code (correct me if wrong). Probably just forgot to delete in copy/paste. Even though it reads the whitespace it doesn't save it. Instead it saves a \0 character, since all strings in C should end with a \0 character. So in any case you read a string with %s and print it with %s without any problem.
    2) Since you cannot use arrays, master5001 uses a double to store the information. Probably the doulbe will be 8bytes, so it can store 8 characters. This in NOT a good idea generally to do. And I believe you are better using only %c when not using arrays. The 7 is to read only 7 characters in case the user enters more. Now, the casting is needed because %s requires an array of char. More precicely a pointer to an array of char (a char *). So that's why it casts the address of the double. This might seem complicated. If you had an array then you would put the address of the first element of the address. Now you put the address of the double variable and tell the compiler to treat it as an array of char. Anyway, when you learn to use arrays you will understand better how storing things in memory works for arrays.

    *The advantage of this code is that it doesn't require a fixed number of letters. In your code you needed 2 letters for the first team and 5 for the other.
    **Note that even %s can cause bug. If for example the user enters "Miami <enter>" (without the ") then scanf will read "Miami " store "Miami\0" and a \n will be left. So the next scanf would read the \n.
    That is why people clean the remaining characters. Cleaning means reading everything until you read the \n. Many bugs are cause by these "details", but knowing the problem will save you time figuring in the future possible bugs.
    ***A better code would be to have a maximum number of letters for a team. You choose, lets say 7 letters. Then you should read one character. Check if it is a whitespace. Ok, to make it simpler just check for the \n (ignoring comment **). If not store it in one variable. Then you read the next character. Etc etc. So you are sure you read everything and stored what you wanted. When you find the \n character you stop reading and wait for the next input which you store in the other set of variables.
    Note that this is kind of how %s works, it is just done automatically (and probably faster). But you should do it the way I described since is the only "good" way to do it without string and you will learn better how to handle the input.

    EDIT: Some people ask how this can be done without arrays. The simplest solution is using a set of variables. Instead of an array that has a[0], a[1] etc etc you use a0,a1 etc etc. When it comes to using the %s in scanf or other functions that need a char *, you DONT use them. You just do it manually reading one one character.
    Last edited by C_ntua; 09-11-2008 at 04:53 AM.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  4. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 09:56 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM

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