Thread: Strings in Scanf function

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    116

    Strings in Scanf function

    I'm trying to write a program where the user inputs a word, here is an example:

    Enter home team : UF
    Enter visitor: Miami
    Enter game date (mm/dd/yyyy): 09/06/2008

    Home Visitor Date
    ------- ------- -----
    UF vs Miami on 09/06/2008




    Here is the code I have:

    Code:
    #include<stdio.h>
    
    int main(void)
    {
    
       char h_team, v_team;
       int date;
    
       printf("\nEnter home team: ");
       scanf("%c", &h_team);
    
       printf("Enter visitor: ");
       scanf("%c", &v_team);
    
       printf("\nEnter game date (mm/dd/yyyy): ");
       scanf("%d/%d/%d", &date);
    
      
       printf("\nHome\t  \tVisitor\t  \tDate");
       printf("\n----\t  \t-------\t  \t----");
       printf("\n%c\tvs\t%c\ton\t%d\n", h_team, v_team, date);
    
    
       return(0);
    }

    I know that %c only grabs a single character, so how do I grab several characters without having to use arrays...

    Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you don't want to use arrays, where do you propose to store all these letters?

  3. #3
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    is there another way?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by dcwang3 View Post
    is there another way?
    Not really, no.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    No, nothing that would make for an understandable and serviceable program.
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Had Dino not stated his two cents above, I would say you could use a double or something...

    Code:
    scanf("&#37;d/%d/%d", &date);
    That is a dubious looking line of code right there.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Dubious is not the word. (You sell yourself way too short.)

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Dino says not dubious.
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    One thing I do not understand is when someone manages to actually compile something that every compiler that I commonly use (even some junky ones for some embedded machines) does issue warnings upon compilation. Though, I suppose that means even my nagging compilers will do the build too... But sometimes those warnings translate into massively destructive run-time errors.

  10. #10
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    1) For some reason ppl are happy when the program runs and don't pay attention on the errors
    2) Or they don't enable all the warnings (like with -Wall in gcc)

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Just a suggestion: instead of using slashes in a scanf format string like "&#37;d/%d/%d", you can use %*c, which ignores any character. That is, with a format string like "%d%*c%d%*c%d", the user can enter "1/2/3" or "1-2-3" or whatever.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by dwks View Post
    Just a suggestion: instead of using slashes in a scanf format string like "%d/%d/%d", you can use %*c, which ignores any character. That is, with a format string like "%d%*c%d%*c%d", the user can enter "1/2/3" or "1-2-3" or whatever.
    You learn something new every day!! Thanks dwks.
    Mainframe assembler programmer by trade. C coder when I can.

  13. #13
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Quote Originally Posted by dwks View Post
    Just a suggestion: instead of using slashes in a scanf format string like "%d/%d/%d", you can use %*c, which ignores any character. That is, with a format string like "%d%*c%d%*c%d", the user can enter "1/2/3" or "1-2-3" or whatever.
    I think learning how to correctly use scanf() would be the most logical first step for dcwang3.

  14. #14
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    here is my updated code:

    Code:
    #include<stdio.h>
    
    int main(void)
    {
      int month, day;
      char h1, h2, h3, h4, h5;
      char v1, v2, v3, v4, v5;
    
      printf("\nEnter home team: ");
      scanf("%c%c", &h1, &h2);
    
      printf("Enter visitor: ");
      scanf("%c%c%c%c%c", &v1, &v2, &v3, &v4, &v5);
    
      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);
    }

    here is my output:

    Enter home team: UF
    Enter visitor: Miami
    Enter game date (mm/dd):
    Home Visitor Date
    ---- ------- ----
    UF vs
    Miam on 0/0/2008


    (well that's sort of the output, the dashed lines are lined us with visitor and date), it's just the way copy and pasted worked through my mac terminal window.


    When I complete Miami, it skips the enter game date, and formats it. Even though it seems to me that I have the formatting uniform, it outputs like a mess. Can someone help me straighten it out???

    Thanks

  15. #15
    Registered User
    Join Date
    Feb 2008
    Posts
    116
    the professor doesn't want us to use strings and arrays, just because we haven't covered that. If that wasn't the case, I would use it, but that's not so that's why I am here asking questions about using single characters and printing out strings....

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, 02: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, 08:56 AM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM