entering numbers to array in a row whithout length input

This is a discussion on entering numbers to array in a row whithout length input within the C Programming forums, part of the General Programming Boards category; does it clean the input stack?? or i need to do i=getchar(); i cant use fflush(); i need to press ...

  1. #16
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    does it clean the input stack??
    or i need to do
    i=getchar();

    i cant use fflush();

    i need to press twice enter with i=getchar();

    do i have to?

  2. #17
    Registered User
    Join Date
    Dec 2008
    Posts
    15
    I'm not sure what you mean by in a 'row'. Since you said string could you solve it like this?

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int index;
        char input[40];
        int length=5;
    
        printf("enter string\n");
        scanf("%s",input);
        printf("\n");
    	
        input[length]='\0';
    	
        printf("%s",input);
    }

  3. #18
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Hmm! so you can only use gets()
    Quote Originally Posted by transgalactic2 View Post
    does it clean the input stack??
    or i need to do
    i=getchar();
    what does the above mean

  4. #19
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    > does it clean the input stack??
    It's not a stack. More of a queue of characters (ie a stream).

    Quote Originally Posted by lvl99 View Post
    I'm not sure what you mean by in a 'row'. Since you said string could you solve it like this?

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int index;
        char input[40];
        int length=5;
    
        printf("enter string\n");
        scanf("%s",input);
        printf("\n");
    	
        input[length]='\0';
    	
        printf("%s",input);
    }
    No... very bad. http://apps.sourceforge.net/mediawik...tle=Scanf_woes

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    fgets() is the "file" version of gets(). It is also a safer version of gets(), so if you can use gets(), you should be allowed to use fgets() - after all, both functions do the same thing [except for the well-known flaw that gets() makes absolutely no checks against the user's input being longer than the receiving buffer - and thus causing the program to crash].

    But if your teacher IS AWARE of this [or at least you are], and you will be told off for using fgets() when gets() will do the job, I would say "go ahead and use gets()" - but be aware that you are lighting your barbecue with petrol, and you do have a good, safe barbeque lighting fluid standing right next to the petrol can.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #21
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    how to clean the input queue and end the input of the string without ffulsh and with 1 enter
    only?

  7. #22
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    can i use only
    Code:
    printf("enter string\n");
     gets (input);
    after i press enter does it cleans the input queue?

  8. #23
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    Quote Originally Posted by transgalactic2
    can i use "gets"
    for this sort of input??
    No, if you are not allowed to use fgets(), then even more so you are not allowed to use gets(). If you are allowed to use gets() and yet not allowed to use fgets(), then your teacher is an incompetent C programmer with no understanding of security issues, and you may want to consider quitting the class or end up an incompetent C programmer yourself.

    Quote Originally Posted by lvl99
    I'm not sure what you mean by in a 'row'. Since you said string could you solve it like this?
    That is just as bad as gets(). With scanf() for a string the length must be given with the format specifier.

    Quote Originally Posted by transgalactic2
    how to clean the input queue and end the input of the string without ffulsh and with 1 enter
    only?
    Use a loop to getchar() until you reach EOF. In fact, it would be wrong to use fflush(), even if you were allowed to, since the behaviour of fflush() is undefined for input streams.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #24
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    I tried to do that getchar loop.
    its not working
    did you mean this?
    Code:
     printf("enter string\n");
     gets (input);
    
    for(i=0;input[i]!=(int)"\0";i++){
     k=getchar();
    }

  10. #25
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    According to the FAQ, it should be:
    Code:
    int ch;
    while ((ch = getchar()) != '\n' && ch != EOF);
    Are you sure you must use gets()? Can you use scanf()?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #26
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    i can use scanf

    but i cant have the length being inputted by the user
    he can enter one coordinates and he can enter 5

    i know that the maximum string allowed is 40 chars long
    Last edited by transgalactic2; 12-30-2008 at 01:22 AM.

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    Quote Originally Posted by transgalactic2
    i can use scanf

    but i cant have the length being inputted by the user
    he can enter one coordinates and he can enter 5

    i know that the maximum string allowed is 40
    In that case use:
    Code:
    scanf("%39s", input);
    If you need to "flush" the input buffer you can still use the code from the FAQ answer after the scanf(). The purpose of the 39 is to ensure that a maximum of 39 characters can be read (the last character is reserved for the null character, if you recall).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #28
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    with that code
    i have to press twice enter

    what to do so i will need to input only one enter?
    Code:
    int main()
    {
           int index;
        char input[40];
        char input2[40];
        int i,k;
        int length=5;
    
    
     printf("enter string\n");
     gets (input);
    
    int ch;
    while ((ch = getchar()) != '\n' && ch != EOF);
    
    }

  14. #29
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    Quote Originally Posted by transgalactic2
    with that code
    i have to press twice enter
    That should not be the case, and the code that you gave is not the one that I gave you.

    There is a catch though: %s matches a whitespace delimited string, but I believe you want to match whitespace as well. I think that the simplest and safest solution, barring the use of fgets(), would be:
    Code:
    #include <stdio.h>
    
    int main()
    {
        char input[40];
        char ch;
        int i;
        printf("Enter a string: ");
    
        for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch != EOF; ++i)
        {
            input[i] = ch;
        }
        input[i] = '\0';
    
        printf("You entered: '%s'\n", input);
    
        return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #30
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    so in this code.
    i use scanf or gets?
    Code:
    #include <stdio.h>
    
    int main()
    {
        char input[40];
        char ch;
        int i;
        printf("Enter a string: ");
    
        for (i = 0; i < 39 && (ch = getchar()) != '\n' && ch != EOF; ++i)
        {
            input[i] = ch;
        }
        input[i] = '\0';
    
        printf("You entered: '%s'\n", input);
    
        return 0;
    }

Page 2 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  2. Problem getting the input from a temp variable into the Array
    By hello_moto in forum C++ Programming
    Replies: 3
    Last Post: 03-16-2006, 12:50 AM
  3. array length
    By Wick in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2003, 04:53 PM
  4. Is there a bug in this part of my algorithm for connect 4?
    By Nutshell in forum Game Programming
    Replies: 8
    Last Post: 04-28-2002, 01:58 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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