Thread: scanf but not complete

  1. #1
    Unregistered
    Guest

    scanf but not complete

    I want to use scanf to get up to 10 chars. Each char will go into a different variable. Here is my code.

    scanf("%c%c%c%c%c%c%c%c%c%c", &c1, &c2, &c3, &c4, &c5, &c6, &c7, &c8, &c9, &c10);

    The problem: I do not care if the user does not enter 10 chars. I want to allow the user to enter any number of chars up to a maximum of 10. But if the user enters say 5 chars and press enter, the program will wait for another 5 chars. How do I stop it from waiting? Thanks

  2. #2
    Unregistered
    Guest
    someone please help!
    I can explain it better if you need.... I have been trying it and I think maybe I have to use getchar() instead of scanf but I do not know what I would do if I used getchar() and I wanted each char in a different variable. Can someone please help me with this? Thanks

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    I take it your not up on char arrays (strings) yet.
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  4. #4
    Unregistered
    Guest
    You are right, is it possible to do what I want with what we have learnt so far (very very basic stuff)? If so, even if it is not the prefered way to do it, can you please show me how. I have tried and tried with scanf and getchar() but I am unable to do it. Thanks

  5. #5
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Thumbs up

    One possible solution to your problem is to use a while loop and use an array instead of 10 different variables...like...


    char ch[10];
    int i;

    i=0;
    while(i<10)
    {
    ch[i]=getche();
    if(ch[i]=='\n')
    break;
    else
    i++;
    }

    Try it Out...

    Regards.

  6. #6
    Unregistered
    Guest
    Thanks vsriharsha but I have a small problem, we have not learned arrays yet I was wondering if it is possible to do it without arrays? Or am I being asked to do something that we have not learned the background for yet? I know arrays is probably the prefered method, but can it be done without them? Thanks for your help

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Code:
    char ch, c1, c2, c3, c4 ect ect;
    int i = 1;
    
    while(i < 11)
    {
       ch = getchar();
       if(ch = '\r') break;
    
       switch(i)
       {
              case 1: c1 = ch;break;
              case 2: c2 = ch;break;
              case 3: c3 = ch; break;
              ect. ect
              case 10:c10 = ch;break;
        }
        i++;
    }
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  8. #8
    Unregistered
    Guest
    Thanks bigtamscot!!! it works very good

  9. #9
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    scanf("%c%c%c%c%c%c%c%c%c%c", &c1, &c2, &c3, &c4, &c5, &c6, &c7, &c8, &c9, &c10);

    Yeah, you would really want to avoide something repetitive like this when you program. For one, it's very prone to error and makes debugging that much harder, and it's hard to read. The idea of the loop was good (if you really don't want to use strings).
    1978 Silver Anniversary Corvette

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf() consideres useless
    By Snafuist in forum C Programming
    Replies: 15
    Last Post: 02-18-2009, 08:35 AM
  2. Replies: 2
    Last Post: 02-20-2005, 01:48 PM
  3. scanf issue
    By fkheng in forum C Programming
    Replies: 6
    Last Post: 06-20-2003, 07:28 AM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM