Thread: How to scan string from user

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    99

    How to scan string from user

    I wrote c programming. I am trying to scan the name of student if student name match with given name then show the status of student other show invalid name of student
    Code:
    #include<stdio.h>
    int main (void)
    {
       unsigned int name;
       printf("Promote user to enter name of student : ");
       scanf("%s",&name);
       
       if(name == Ram)
       { 
       printf(" Student Name :  Ram. \n Exam status : failed.\n year : 2018");
       }  
       else if(name == Dev)
       { 
       printf(" Student Name :  Dev. \n Exam status : pass. \n year : 2018");
       } 
       
       else if(name == Devid)
       { 
       printf(" Student Name :  Devid. \n Exam status : pass. \n year : 2018");
       } 
       else   
       {
        printf ("Entered invalid name");
       }
     return 0;
    }
    error

    hello.c:10:15: error: 'Ram' undeclared (first use in this function)
    if(name == Ram)
    ^~~
    hello.c:10:15: note: each undeclared identifier is reported only once for each function it appears in
    hello.c:14:20: error: 'Dev' undeclared (first use in this function)
    else if(name == Dev)
    ^~~
    hello.c:19:20: error: 'Devid' undeclared (first use in this function)
    else if(name == Devid)
    ^~~~~
    What's wrong in program?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Refer to what DRK told you in your topic switch case error in program: "The compiler tells exactly what the problem is - you provided some identifiers (red, green, blue) which were not previously declared."

    This time, instead of red, green, blue, the identifiers are Ram, Dev, Devid. Again, an enum is a good choice, or you could use the other suggestions by Salem in post #4 of that topic.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Lookup strcmp function.

    C library function - strcmp()

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by laserlight View Post
    Refer to what DRK told you in your topic switch case error in program: "The compiler tells exactly what the problem is - you provided some identifiers (red, green, blue) which were not previously declared."
    Look at this program

    Code:
    #include<stdio.h>
    int main (void)
    {
       unsigned int grade;
       printf("Promot user to enter grade of student : ");
       scanf("%d",&grade);
       
       if(grade > 100)
       {
         printf ("Entered invalid grade");
       }  
        else if (grade > 35)
       { 
        printf ("Your grade is %d \n You are passed",grade);  
       }
       else   
       {
         printf ("Your grade is %d \n You are failed",grade);
       }
     return 0;
    }
    Promot user to enter grade of student : 101
    Entered invalid grade
    Your grade is 45
    You are passed
    Your grade is 34
    You are failed

    if you compare first and second, both are similar the difference is only in scanning type.

    In first example, I was trying to scan name while in second example I was trying to scan numbers.

    When I scan numbers I don't need to use enum or define but when I need to scan string or name why I need to use enum or something like define red
    Last edited by vead; 01-09-2018 at 11:32 AM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by vead
    When I scan numbers I don't need to use enum or define but when I need to scan string or name why I need to use enum or something like define red
    It is not about the "scanning", it is about the identifiers. Consider this program:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        char name[10];
        printf("Enter your name: ");
        if (scanf("%9s", name) == 1)
        {
            if (strcmp(name, "vead") == 0)
            {
                printf("%s! Good to meet you.\n", name);
            }
            else
            {
                printf("%s is not vead.\n", name);
            }
        }
        else
        {
            printf("Error: could not read input.\n");
        }
        return 0;
    }
    In this case, I used a string literal "vead" for comparison with the input stored in the variable named name. Now try to compile this program:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main(void)
    {
        char name[10];
        printf("Enter your name: ");
        if (scanf("%9s", name) == 1)
        {
            if (strcmp(name, vead) == 0)
            {
                printf("%s! Good to meet you.\n", name);
            }
            else
            {
                printf("%s is not vead.\n", name);
            }
        }
        else
        {
            printf("Error: could not read input.\n");
        }
        return 0;
    }
    The only thing that changed is that instead of using the string literal "vead", I now use the identifier vead. You should get an error similiar to what you encountered previously. Now compile and run:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define vead "vead"
    
    int main(void)
    {
        char name[10];
        printf("Enter your name: ");
        if (scanf("%9s", name) == 1)
        {
            if (strcmp(name, vead) == 0)
            {
                printf("%s! Good to meet you.\n", name);
            }
            else
            {
                printf("%s is not vead.\n", name);
            }
        }
        else
        {
            printf("Error: could not read input.\n");
        }
        return 0;
    }
    This is the same as the previous program, except that I added a #define to define the identifier named vead to be the string literal "vead". This is a macro, so during preprocessing the compiler (or preprocessor) would replace the identifier vead with the string literal "vead", thereby resulting in a program essentially identical to the first one.

    Note that I misspoke about the use of enum: in this case you are dealing with strings, so enum is not applicable, but #define could be used, or you could use say, a const string. Notice that I used stahta01's suggestion for strcmp instead of comparing with == since == would compare pointers rather than the string content.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2012
    Posts
    99
    Quote Originally Posted by laserlight View Post
    It is not about the "scanning", it is about the identifiers.
    Notice that I used stahta01's suggestion for strcmp instead of comparing with == since == would compare pointers rather than the string content.
    I prefer to write program without string.h library because every compiler has different library I am from electronics background

    How to write this program without strin.h library

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by vead
    I prefer to write program without string.h library because every compiler has different library I am from electronics background
    <string.h> is part of the C standard, so if it is not present, then it does imply that your environment significantly lacks standard library support, which is quite possible from your line of work. However, that also imply that you need to know your C well, so until you do, you should use the standard library.

    Quote Originally Posted by vead
    How to write this program without strin.h library
    Implement your own version of strcmp. This means you need to read up on strcmp, understand what it does and does not do. This also means you need to read up on C's concept of null terminated strings. If you expect to be spoonfed these, then you should discard your electronics background temporarily and just use the C standard library until you have more knowledge to be able to implement library functionality like this.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 09-04-2017, 09:56 PM
  2. How to prompt user to scan numbers into an array
    By Jeffery1 in forum C Programming
    Replies: 9
    Last Post: 05-20-2013, 08:12 PM
  3. Replies: 3
    Last Post: 11-01-2010, 08:22 PM
  4. C Function won't scan for user inputted value
    By kevin250 in forum C Programming
    Replies: 10
    Last Post: 10-21-2010, 10:41 PM
  5. C Function won't scan for user inputted values
    By kevin250 in forum C Programming
    Replies: 3
    Last Post: 10-20-2010, 08:47 PM

Tags for this Thread