First program..

This is a discussion on First program.. within the C Programming forums, part of the General Programming Boards category; Yep, I'm you're new C newbie I just started learning C (actually I already knew a lot of it, like ...

  1. #1
    uninteresting
    Join Date
    Jun 2002
    Posts
    66

    Unhappy First program..

    Yep, I'm you're new C newbie
    I just started learning C (actually I already knew a lot of it, like the structure), and I've learned pretty quickly. But no one I've asked has found the bug yet. I asked the question over at Flashdaddee, but their C forum isn't very busy. Anyway, here it is. For some reason it doesn't realize it when I type 'Tman' as my username. In case it matters, I'm using djgpp as my compiler.

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    char nick[40];
    char pass[40];
    int age;
    
    //Gather user's info here:
    printf("Welcome to a program made by Tman \n");
    printf("What is your (nick)name? ");
    gets(nick);
    printf("\nHow old are you? ");
    scanf("%d", &age);
    
    //Now process it:
    if(strcmp(nick, "Tman") == 0){
            printf("You're not Tman! Prove it! What's the password, then? ");
            gets(pass);
            goto check_pass;
    }
    
    process:
    printf("\n\nHello, %s.\n", nick);
    printf("So you're %d, eh? \n", age);
    printf("\nThanks for using my program!");
    return 0;
    
    check_pass:
    if(strcmp(pass, "xYz123") == 0) {
            printf("\nOkay, so I was wrong.  Welcome, Tman! ");
    } else {
            printf("\nHa! Caught ya! Nope, %s is not the right password.", pass);
    }
    goto process;
    }
    Can anybody find my problem?
    Last edited by Tman; 06-28-2002 at 11:54 AM.
    *** TITANIC has quit (Excess Flood)

  2. #2
    uninteresting
    Join Date
    Jun 2002
    Posts
    66
    c'mon! I you can't find anything just tell me so at least I know you thought about me..
    *** TITANIC has quit (Excess Flood)

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Your call to scanf leaves a newline in the input buffer, the next request for input takes the newline and ends. So the program muxes up. This should work better:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
      char nick[40];
      char pass[40];
      int age;
    
      //Gather user's info here:
      printf("Welcome to a program made by Tman \n");
      printf("What is your (nick)name? ");
      gets(nick);
      printf("\nHow old are you? ");
      scanf("%d", &age);
      while ( getchar() != '\n' );
    
      //Now process it:
      if(strcmp(nick, "Tman") == 0){
        printf("You're not Tman! Prove it! What's the password, then? ");
        gets(pass);
        goto check_pass;
      }
    
    process:
      printf("\n\nHello, %s.\n", nick);
      printf("So you're %d, eh? \n", age);
      printf("\nThanks for using my program!\n");
      return 0;
    
    check_pass:
      if(strcmp(pass, "xYz123") == 0) {
        printf("\nOkay, so I was wrong.  Welcome, Tman! ");
      } else {
        printf("\nHa! Caught ya! Nope, %s is not the right password.", pass);
      }
      goto process;
    }
    And if you must use goto, only jump forward in the program, not backward. This minimizes confusion.

    -Prelude
    My best code is written with the delete key.

  4. #4
    uninteresting
    Join Date
    Jun 2002
    Posts
    66
    Thanks! It works! I know, I should use procedures/functions/whatever-you-call-them , but I'm just focused on making a working program. Ok, my next input call would be the one for the password (as long as 'Tman' is the nick), which comes after the printf that says 'prove it!'. But I don't see any output to the screen like that! I just don't understand..
    *** TITANIC has quit (Excess Flood)

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >But I don't see any output to the screen like that! I just don't understand..
    Remember how I said that the call to scanf left a newline character in the input stream? The gets function uses the newline character as it's stopping point for reads, so it will read a newline as the first character and immediately end. The array that you were assigning to will still hold uninitialized values and when you compare it with the password, the comparison fails. Note that gets doesn't wait for the user to enter anything if there is already data in the stream, so you don't get a chance to enter anything.

    -Prelude
    My best code is written with the delete key.

  6. #6
    uninteresting
    Join Date
    Jun 2002
    Posts
    66
    Yes, but the program asks for the nick after I use scanf!
    *** TITANIC has quit (Excess Flood)

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Asks after scanf? Prelude is confused:
    Code:
    printf("Welcome to a program made by Tman \n");
    /* Ask for the name */
    printf("What is your (nick)name? ");
    /* Get the name with a nasty buggy function */
    gets(nick);
    printf("\nHow old are you? ");
    /* scanf, definitely after asking for the nick and recieving it */
    scanf("%d", &age);
    while ( getchar() != '\n' );
    
    //Now process it:
    if(strcmp(nick, "Tman") == 0){
      /* Perhaps you mean it asks for the password? */
      printf("You're not Tman! Prove it! What's the password, then? ");
      gets(pass);
      goto check_pass;
    }
    For me, the program asks for the name, then the age, then if the name is Tman it asks for the password. If the name isn't Tman it skips the entire password process. Are you getting something different?

    -Prelude
    My best code is written with the delete key.

  8. #8
    Unregistered
    Guest
    printf( "\nHow old are you?" ), fflush( stdout );

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: First program..

    Originally posted by Tman
    Yep, I'm you're new C newbie
    I just started learning C (actually I already knew a lot of it, like the structure), and I've learned pretty quickly.
    I don't want to start another goto argument in this thread (there's been plenty in the past
    ), so I'll keep it short and sweeet.

    Don't use goto unless you absolutely have to. Apparently there are places in code where goto is good to have, but personally I've never seen one in any code I've looked at. Control the flow with the other things, like loops and if statements. Sorry, but having 3 labels in a program the size you have written is not good

    Also, I know you've tried gets() and fgets(), but now you've toyed, forget about gets(), and stick with fgets(). If you are worried about the newline character fgets() puts in the array, just use this simple snippet to remove it:
    Code:
    len = strlen(myarray);
    if (myarray[len-1] == '\n') myarray[len-1] = '\0';
    Finally, welcome to cprogramming.com
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  10. #10
    uninteresting
    Join Date
    Jun 2002
    Posts
    66
    d'oh: change that to before My point is what does that newline character have to do with anything? Nothing ever got input after i used fgets, the if function wasn't working, but take that character away and the if function magically works. Why?

    Hammer, I know, I know, I'll fix it, but this is my first C program so chill out
    *** TITANIC has quit (Excess Flood)

  11. #11
    uninteresting
    Join Date
    Jun 2002
    Posts
    66
    ok, hammer told me to make sure that scanf() recieve a string input from the user.. how? Would it be easiest to make my own scanf() with getchar() and checking?
    *** TITANIC has quit (Excess Flood)

  12. #12
    Registered User billholm's Avatar
    Join Date
    Apr 2002
    Posts
    225
    >>My point is what does that newline character have to do with anything? Nothing ever got input after i used fgets.

    I have a similar question in mind. Why does a newline (which I suppose was truncated from gets()) affect the program flow? It's supposed to be discarded from the buffer stream. I had a similar program flow to that of yours Tman: first gets() then scanf() then strcmp(), but it worked fine when I tested it. I use Borland C++ 3.1. Could it be a bug?

    PS.
    A logic bug exists in Turbo C 2.0. It has something to do with the compiler's tokenizing of the switch() statement. It's a rare one, but I was fortunate enough to have bumped into that.
    All men are created equal. But some are more equal than others.

    Visit me at http://www.angelfire.com/my/billholm

  13. #13
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    - Newline in string problem.

    Consider the following:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
    	char buf[BUFSIZ];
    	
    	if (fgets(buf, BUFSIZ, stdin))
    	{
    		printf ("You entered >%s<\n", buf);
    		if (strcmp(buf, "mypassword") == 0)
    			printf ("Correct password\n");
    		else printf ("Invalid password\n");
    	}
    	
    	return(0);	
    }
    The strcmp() will never return 0. Why? Because the fgets() function puts the newline character into the buf array. This means that when the user enters
    >mypassword<enter>
    the newline is considered to be part of the string, and is used in strcmp(). The hardcoded password does not end with a newline. Therefore the two strings are different.

    This is why you must remove the trailing newline character from the array before testing it against the hardcoded password.

    Also, run this program (my code given here). You will notice the output is:
    Code:
    mypassword
    You entered >mypassword
    <
    Invalid password
    Note here that the < character is on the next line. This shows that the newline is part of the buf string.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  14. #14
    uninteresting
    Join Date
    Jun 2002
    Posts
    66

    AHH

    Ok, when i said fgets(), i meant scanf() (I was tired last night and you know..) Just replace all the fgets() with scanf() on my second to last question.. *sigh*
    *** TITANIC has quit (Excess Flood)

  15. #15
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: AHH

    Originally posted by Tman
    Ok, when i said fgets(), i meant scanf() (I was tired last night and you know..) Just replace all the fgets() with scanf() on my second to last question.. *sigh*
    I kinda lost with this Can you repeat your question please.... What is it you want to know exactly?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

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

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 12:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 09:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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