Like Tree1Likes
  • 1 Post By anduril462

Help me! problem in reading and accepting Fullname

This is a discussion on Help me! problem in reading and accepting Fullname within the C Programming forums, part of the General Programming Boards category; So im having trouble, im just a beginner programmer and still learning. Im having trouble in reading my full name ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    4

    Question Help me! problem in reading and accepting Fullname

    So im having trouble, im just a beginner programmer and still learning. Im having trouble in reading my full name in just a single variable and some kind of authentication. this is my code by the way.

    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
    char NAME_MINE1[40];
    
    void clrscr();
    
    system("CLS");
    printf("Enter Full name: ");
    scanf("%39s",&NAME_MINE1);
    
    if(NAME_MINE1=="Qwerty You Tube")
    {
    system("CLS");
    printf("Nice!");
    getch();
    go to Phase2;
    }
    else
    {
    system("CLS");
    printf("WRONG!");
    
    
    getch();
    system("cls");
    exit(0);
    }

    What im trying to do is:

    if i enter my full name i will be able to go on to the next statement/command but when i put on some name (which is not mine) i will automatically get an error and exit the program.

    If i type "Qwerty You Tube" EXACTLY and not just the first string before the whitespace i will be able to proceed but when i enter "Qwerty Tube You" or "Qazse Qop Queen" i will get an error and the program will exit.

    am i doing something wrong or what ? Is what i want even possible ? im so confused right now im seaching for answers for hours! and to no avail ( Please help me ((((((

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    You should read the documentation for scanf (link). The %s modifier stops at any white space, thus if you type "John Doe", it stops after John. You should use fgets to read in the entire line. Note that fgets leaves the newline character (you press enter after you type your name -- that enter puts a '\n' in the input buffer), so you will have to remove it. There's a suggestion on how to here: FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    4
    Quote Originally Posted by anduril462 View Post
    You should read the documentation for scanf (link). The %s modifier stops at any white space, thus if you type "John Doe", it stops after John. You should use fgets to read in the entire line. Note that fgets leaves the newline character (you press enter after you type your name -- that enter puts a '\n' in the input buffer), so you will have to remove it. There's a suggestion on how to here: FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com.
    ohh so how about my 14th line ? i would like to make it somewhat like an authentication

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,690
    You can't compare strings in 'C' like that, with the equality operator (==).
    Look into the "strcmp()" function. Take special note of the return value.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Quote Originally Posted by viney17 View Post
    ohh so how about my 14th line ? i would like to make it somewhat like an authentication
    Duh! I totally missed that. You can't use an == to compare strings, you need to use the strcmp function. Google will have plenty of examples and tutorials.

    A few other notes

    • Please indent your code properly, it's hard to read otherwise, which makes mistakes more likely, and makes people not want to help you.
    • By convention, ALL_UPPERCASE is used for constants, i.e. values that never change. Variables are typically lower_case_with_underscores or camelCase. Pick whichever you like, and use it consistently. Again easy to read code helps everybody.
    • void clrscr(); does not call the clrscr function. All that line does is provide a prototype, which is usually not what you want to do. #include the right header, if you want to use the clrscr() function. But frankly, I find it visually awkward when the screen keeps clearing, so I would not use all those system("cls") calls. One at the beginning or end might be okay.
    • go to Phase2; I'm not sure what you mean by that, but if you're intending to use goto, I would say "STOP IT". Read this article: Goto Considered Harmful.
    • You appear to be missing a closing curly bracket for your else clause (perhaps around line 25 or 26).
    • getch is not standard or portable. I would suggest using getchar, which would have the same effect, but is standard.
    Salem likes this.

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    4
    Quote Originally Posted by anduril462 View Post
    Duh! I totally missed that. You can't use an == to compare strings, you need to use the strcmp function. Google will have plenty of examples and tutorials.

    A few other notes

    • Please indent your code properly, it's hard to read otherwise, which makes mistakes more likely, and makes people not want to help you.
    • By convention, ALL_UPPERCASE is used for constants, i.e. values that never change. Variables are typically lower_case_with_underscores or camelCase. Pick whichever you like, and use it consistently. Again easy to read code helps everybody.
    • void clrscr(); does not call the clrscr function. All that line does is provide a prototype, which is usually not what you want to do. #include the right header, if you want to use the clrscr() function. But frankly, I find it visually awkward when the screen keeps clearing, so I would not use all those system("cls") calls. One at the beginning or end might be okay.
    • go to Phase2; I'm not sure what you mean by that, but if you're intending to use goto, I would say "STOP IT". Read this article: Goto Considered Harmful.
    • You appear to be missing a closing curly bracket for your else clause (perhaps around line 25 or 26).
    • getch is not standard or portable. I would suggest using getchar, which would have the same effect, but is standard.
    so i tried this code

    Code:
    int main(void)
    {
          char szKey[] = "My Full Name";
          char szInput[80];
          
    void clrscr();
    
    char szKey[] = "My Full Name";
          char szInput[80];
    
            do
          {
           printf("Enter my name: ");
           gets(szInput);
           }
           
            while (strcmp (szKey,szInput) != 0);
           system("CLS");
           printf("Good");
           getch();
    
    
    }
    but the output look like this:

    Enter my name:

    Enter my name: --->(cursor here)<---

    how can i fix the double "Enter my name" code? im really confused -.-
    Last edited by viney17; 02-07-2013 at 07:05 AM.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    The first thing is to properly indent your code so it's readable, and so people will actually want to help you. Like so:
    Code:
    int main(void)
    {
        char szKey[] = "My Full Name";
        char szInput[80];
    
    
        void clrscr();
    
    
        char szKey[] = "My Full Name";
        char szInput[80];
    
    
        do
        {
            printf("Enter my name: ");
            gets(szInput);
        }
    
    
        while (strcmp (szKey,szInput) != 0);
        system("CLS");
        printf("Good");
        getch();
    
    
    
    
    }
    Second, make sure you are using the actual code that produces the problem. The code you posted does not produce a double prompt. It does not produce anything because it doesn't compile.

    Which brings me to my third point: make sure the code compiles with no errors or warnings at the highest warning level:
    Code:
    $ make foo
    gcc -Wall -Wunreachable-code -ggdb3 -std=c99 -pedantic  -lm -lpthread  foo.c   -o foo
    foo.c: In function ‘main’:
    foo.c:8: error: redefinition of ‘szKey’
    foo.c:3: note: previous definition of ‘szKey’ was here
    foo.c:9: error: redeclaration of ‘szInput’ with no linkage
    foo.c:4: note: previous declaration of ‘szInput’ was here
    foo.c:13: warning: implicit declaration of function ‘printf’
    foo.c:13: warning: incompatible implicit declaration of built-in function ‘printf’
    foo.c:14: warning: implicit declaration of function ‘gets’
    foo.c:17: warning: implicit declaration of function ‘strcmp’
    foo.c:18: warning: implicit declaration of function ‘system’
    foo.c:19: warning: incompatible implicit declaration of built-in function ‘printf’
    foo.c:20: warning: implicit declaration of function ‘getch’
    make: *** [foo] Error 1
    Lines 8,9: Why did you declare those variables a second time? Just once is enough.
    Line 13,14,19: Using these functions requires you to #include <stdio.h>
    Line 17: Using the strcmp() function requires you to #include <string.h>
    Line 18: Using the system() function requires you to #include <stdlib.h>
    Line 20: Don't use getch(). Use getchar() instead. It's nearly identical, and is a standard function, so everybody can use it.
    Not listed, but still needs fixing:
    Line 6: You don't use the clrscr() function anywhere, so remove this line. It does absolutely nothing.

    Fixing all of that, and trying again:
    Code:
    $ make foo
    gcc -Wall -Wunreachable-code -ggdb3 -std=c99 -pedantic  -lm -lpthread  foo.c   -o foo
    /tmp/ccRC1I9v.o: In function `main':
    /home/tyco/sandbox/cprogramming/foo.c:13: warning: the `gets' function is dangerous and should not be used.
    Ahh, yes, the gets() function. It is dangerous, I don't recommend using it. Use fgets instead (note, you will have to remove the new line). Read these links: FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com and FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me! problem in reading and accepting Fullname
    By viney17 in forum C++ Programming
    Replies: 1
    Last Post: 02-06-2013, 10:36 AM
  2. getchar() not accepting 'ans' value
    By Zaheer Attar in forum C Programming
    Replies: 4
    Last Post: 09-10-2011, 02:42 PM
  3. Problem accepting a character
    By babu in forum C Programming
    Replies: 7
    Last Post: 08-27-2007, 01:26 AM
  4. problem with accepting password
    By babu in forum C Programming
    Replies: 3
    Last Post: 07-23-2007, 07:42 AM
  5. accepting +123 help
    By peanut in forum C Programming
    Replies: 7
    Last Post: 11-01-2006, 11:48 PM

Tags for this Thread


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