0

what's the problem in input(std[i].name)? i'm trying to take input string with whitespace but can't. Someone please help me.

#include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    int main() {
    struct student 
    int roll;
    char name[30];
    int age;
    } std[3];
    for(int i=0;i<3;i++){
    int j=i+1;
    printf("\nStudent%d\n\tEnter Roll:",j);
    scanf("%d",&std[i].roll);
    //-----------***********-----------------------------------
    printf("\n\tEnter name:"); 
    scanf("%[^\n]",std[i].name);/* prblem */
    //------------****************----------------------------------
    printf("\n\tEnter age:");
    scanf("%d",&std[i].age);
    }
    for(int i=0;i<3;i++){
    int j=i+1;
    printf("\n student%d\n\tRoll:%d",j,std[i].roll);
    printf("\n\t name:%s",std[i].name);
    printf("\n\t age:%d",std[i].age);
    }
    return 0;
    }

output: Program output

  • Please add a space so `scanf(" %[^\n]",std[i].name);` and see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). Most format specifiers automatically filter leading whitespace characters from the input, but `%c` and `%[]` and `%n` do not. You can instruct `scanf` to do so by adding a space just before the `%`. – Weather Vane Oct 15 '22 at 17:22
  • Start by properly indenting your code. It is unreadable as shown. – n. m. could be an AI Oct 15 '22 at 17:24
  • Thanks a lot!That's my first ask . – Mustafejur Rahman Oct 15 '22 at 17:43
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Oct 16 '22 at 00:54

1 Answers1

0

First off, there was some issues with the curly braces so the code would not initially compile, so did a bit of cleanup there. Second, scanf really does not support regular expressions. As a reference, you might want to check out this previous issue scanf regular expressions

With that in mind, following is a revised version of your code snippet.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main()
{
    struct student
    {
        int roll;
        char name[30];
        int age;
    } std[3];

    for(int i=0; i<3; i++)
    {
        int j=i+1;
        printf("Student%d\nEnter Roll: ",j);
        scanf("%d",&std[i].roll);
        //-----------***********-----------------------------------
        printf("Enter name: ");
        scanf("%s",std[i].name);/* prblem */    /* Used simple string input */
        //------------****************----------------------------------
        printf("Enter age:");
        scanf("%d",&std[i].age);
    }

    printf("\n");   /* Added for cosmetic output */

    for(int i=0; i<3; i++)
    {
        int j=i+1;
        printf("Student%d\n\tRoll:%d\n",j,std[i].roll);
        printf("\tname: %s\n",std[i].name);
        printf("\tage: %d\n",std[i].age);
    }
    return 0;
}

The main take-away is that the scanf function call for the name was simplified for this test to just utilize the string formatting character "%s". With that here was a sample test on my terminal.

@Una:~/C_Programs/Console/StringInput/bin/Release$ ./StringInput 
Student1
Enter Roll: 1
Enter name: Craig
Enter age:44
Student2
Enter Roll: 2
Enter name: Albert
Enter age:45
Student3
Enter Roll: 3
Enter name: John
Enter age:43

Student1
    Roll:1
    name: Craig
    age: 44
Student2
    Roll:2
    name: Albert
    age: 45
Student3
    Roll:3
    name: John
    age: 43

If you need to utilize some type of regular expression functionality for the name, you might review the above reference issue for ideas.

Give this a try.

NoDakker
  • 3,390
  • 1
  • 10
  • 11
  • Your code is good for Single word input without whitespace! My problem was take input with white space. problem is solved. Thanks for your concern. – Mustafejur Rahman Oct 15 '22 at 17:53