Thread: String input problem, gets

  1. #1
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378

    String input problem, gets

    hey, i'm having a problem with my input function. i've had the problem before.. but i dont remember what the solution is. for some reason, when my program hits the gets input it goes straight through it.. so how do i make this work? here's my code for this function:
    Code:
    void encrypt_text(int random_number, char *key)
    {
         char text[255];
         char *pt;
         int stringlength, counter;
         
         if (random_number == 70) { --random_number; }
         
         /* user input of string to be encrypted */
         printf("\n\nEnter your text: ");
         if ((pt = gets(text)) == NULL)
         {
              printf("\nError on input.\n");
         }
         
         /* find length of string entered */
         stringlength = strlen(text);
    
         /* find string length for loop */
         counter = 0;
         do {
               text[counter] = text[counter]^key[random_number];
               printf("%c", text[counter]);
               ++counter;
         } while (counter <= stringlength);
    }
    Last edited by willc0de4food; 03-04-2005 at 06:29 AM.
    Registered Linux User #380033. Be counted: http://counter.li.org

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    18
    hi man.
    this problem is equal my problem, this link answer your question in relation with the function gets.
    http://www.eskimo.com/~scs/C-faq/q7.1.html

    I'will wait to have help you!

  3. #3
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    hmm.. i made the changes that are on the site you provided, and it's still running through the input section.. but its inputting the character referenced by the random number. so if the random number was 33, then the character in the key which is 33 is g and it will put g in for me so i dont get the chance to input anything. here's my new code:
    Code:
    void encrypt_text(int random_number, char *key)
    {
         char text[255] = "";
         char *pt;
         int stringlength, counter;
         
         system("cls");
         if (random_number == 70) { --random_number; }
         
         /* user input of string to be encrypted */
         printf("\n\nEnter your text: ");
         fgets(text, sizeof text, stdin);
         if ((pt = strchr(text, '\n')) != NULL)
         {
              *pt = '\0';
         }
         
         /* find the length of the entered string */
         stringlength = strlen(text);
         
         /* find string length for loop */
         counter = 0;
         do {
               text[counter] = text[counter]^key[random_number];
               printf("%c", text[counter]);
               ++counter;
         } while (counter <= stringlength);
         printf("\n\n");
    }
    Registered Linux User #380033. Be counted: http://counter.li.org

  4. #4
    Registered User computerfreaks's Avatar
    Join Date
    Jan 2005
    Posts
    30
    When you say that you don't get chance to input anything... what do you mean??

    Do you mean that it is ignoring the text to be encrypted and just putting the char found in the key, as referenced by the random number?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You'll code for food, but you use gets? I hope you like going hungry.

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

  6. #6
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    quzah, please excuse my ignorance while i go jump off a bridge b/c i look like ghandi however, i was aware of the buffer overflow errors that could be produced.. i just chose to ignore it because i'm not distributing the program to anyone that would input more characters then the allowable number, and its for use with my friends and me.
    in either case, the chance to input data is never given, the program does it for you for some reason.

    but kind of computerfreaks.. it doesn't ignore the text to be encrypted, it puts the char found in the key as referenced by the random number FOR YOU. so its like in a batch file where you can have it echo y when doing an action that requires a yes/no answer. and then it just never encrypts anything.. if i use:
    Code:
    scanf("%s", &text);
    then it will take input and it will encrypt it, but i can't use spaces with that.. ?



    my entire program source, if it helps, can be found here: http://67.162.200.72/myprograms/RandomNumberGenerator.c
    Last edited by willc0de4food; 03-04-2005 at 05:47 PM.
    Registered Linux User #380033. Be counted: http://counter.li.org

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Well then I would guess that you're calling scanf() somewhere before calling encrypt_text(). scanf() leaves the \n on the input buffer and gets() picks that up as a blank entry. And just so you know, you'll end up getting reprimanded for using gets() every time you post it on this forum. There really is no excuse for using it.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    hmm..i call scanf() in main for a menu ( see source: http://67.162.200.72/myprograms/RandomNumberGenerator.c ) and then in my switch() statement i call encrypt_text() but why would that create a problem? and how can i get the \n out of the input buffer?
    and like i said, i was just using gets for the sake of using it. lol
    Registered Linux User #380033. Be counted: http://counter.li.org

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It's in this site's FAQ. Something like:
    Code:
    while(getchar() != '\n')
      ;
    If you understand what you're doing, you're not learning anything.

  10. #10
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    thanks, it works. the problem was with the input buffer.. and the working code for my function now looks like this:
    Code:
    void encrypt_text(int random_number, char *key)
    {
         char text[255];
         char *pt;
         int stringlength, counter, ch;
         
         system("cls");
         if (random_number == 70) { --random_number; }
         
         /* flush the input buffer */
         while ((ch = getchar()) != '\n' && ch != EOF);
         
         /* user input of string to be encrypted */
         printf("\n\nEnter your text: ");
         fgets(text, sizeof(text), stdin);
         if ((pt = strchr(text, '\n')) != NULL)
         {  
                 *pt = '\0';
         }
         
         /* find the length of the entered string */
         stringlength = strlen(text);
         
         /* find string length for loop */
         counter = 0;
         do {
               text[counter] = text[counter]^key[random_number];
               printf("%c", text[counter]);
               ++counter;
         } while (counter <= stringlength);
         printf("\n\n");
    }
    Registered Linux User #380033. Be counted: http://counter.li.org

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > the problem was with the input buffer
    Actually, the problem is your code, and the lousy way in which it uses the input buffer.

    > char choice;
    > scanf("%d", &choice);
    1. using scanf() is a bad idea anyway, it simply messes up the input stream in it's own private way and any other input method - eg fgets - usually ends up barfing as a result.
    2. If you don't have a compiler which can check printf/scanf usage, then I suggest you get one. Storing an int (%d) where there is only room for a char is a bad move.

    > text[counter] = text[counter]^key[random_number];
    What will this print when you encrypt 'a' with the letter 'a' ?

    > while (counter <= stringlength);
    Why do you need to encrypt the trailing \0 ?

    > i just chose to ignore it because i'm not distributing the program
    Programming is way too difficult to simply be able to turn on and off "being about right" based solely on what you think the audience is. If you make being right a habit rather than a choice, then it won't matter what the audience is (or what the audience might become).

    Surely you're not suggesting that should one of your programs become more popular all of a sudden that you would go and rewrite it to be "better" just because more people are using it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #12
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    should i get like, a degree in programming with the major being C Programming before posting a question on this board? because it seems that anything that i produce from what i've been taught is being criticised as if i'm like the scum of the earth or something.

    1. sorry for my ignorance, i didn't know that scanf() was bad to use ?
    2. I use the Dev-C++ IDE which I BELIEVE, uses the gcc compiler.
    if you encrypt a^a then it displays nothing. but this is just an example for myself so i can see how things work. in another program that i want to use this in, i'm going to be storing the string in a text file that i dont want to be readable. but if i decrypt the product of a^a, will it return a?
    i dont need to encrypt the trailing \0.
    but how will one of my programs become more popular when the general public (or the "public" of any sort) will not have access to a program that i write? when i write a program for class or that i'm planning on having people use, i do do things better. but i figure if its just me messing around with the program, then i dont have to worry about a program becoming more popular.

    however, once again - i apologize for my ignorance.
    I've only been programming in C since January 5th.
    Last edited by willc0de4food; 03-05-2005 at 01:56 AM.
    Registered Linux User #380033. Be counted: http://counter.li.org

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > should i get like, a degree in programming with the major being C Programming before posting a question on this board?
    No, it's just your "I'll do it my way" attitude which is the problem. You seem uninterested in actually learning anything better or new ways of doing stuff.

    > because it seems that anything that i produce from what i've been taught is being criticised
    gets() and scanf() are the first things you probably learnt, and through the path of least resistance you're reluctant to let go of them and use things which are safer.

    Also, pretty much all the regulars here know that most "teachers" actually suck as both programmers and teachers, and many of the books which they recite from also suck. Don't take this as a reflection on you, just the general suckyness of the entire education system for programmers. Teachers just give you a start, the real learning is from practical experience and other programmers.

    When programming professionally, code is typically reviewed by a group of your peers, so you shouldn't feel bad about other people looking at your code and making suggestions. If you ever join an open source development, then many more programmers will get to look at your code (and change it). So you really should strive to be the best you can be rather than just being adequate for the immediate problem.

    > i didn't know that scanf() was bad to use ?
    Not so much as bad, but far too tricky to use properly. As such, when you start doing things a little more advanced, newbies usually show up with some kind of "it's ignoring my input" type question.

    > I use the Dev-C++ IDE which I BELIEVE, uses the gcc compiler.
    gcc -W -Wall -ansi -pedantic -O2
    Go into the compiler settings, and find where you can add additional compiler options. Set the flags above and see what happens.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  14. #14
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    ok, i went to tools > compiler options & checked the box for 'Add the following commands when calling the compiler.' is that correct?

    yea.. my attitude does get me in trouble at times :-[ lol, sry. but the part about me being uninterested in learning new & better is false. i live for finding better..it just prly doesn't seem like it because usually when i've been doing stuff, its at 3 or 4 in the morning lol. so then:
    what can i use to replace scanf()? or how do you use it properly? i've tried replacing it with fgets() but that didn't go over so well..
    and gets() was never taught by my C prof. i got that off the about.com c programming tutorial (here: http://cplus.about.com/od/beginnerct...1/l/blctut.htm ) because i'm only in class 2 days a week and thats not often enough for me to be satisfied.
    and i wouldn't really consider my prof to suck at programming or teaching, but my opinion could be somewhat biased as he's a friend of my dad and our families are friends and all that blah but he does seem to know what he's talking about as i know he knows C, C++, Java, ASM, and Cobol (he likes to pick on Cobol a lot, lol).. possibly others, but those are just the ones he's told us a little about in class. but yea.. i dont feel bad abt people reviewing my code, i think it was just my attitude made them have an attitude which made me take offense and basically i'm an idiot haha..
    Registered Linux User #380033. Be counted: http://counter.li.org

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  3. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  4. string problem
    By INeedSleep in forum C++ Programming
    Replies: 24
    Last Post: 11-08-2007, 11:52 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM