Home > front end >  printing white space in strings in C
printing white space in strings in C

Time:10-02

I have to make a program that prints out my age, name, country by typing an input. The name has white space, so I have to make it to ignore whitespace and continue to print out. I successed on the program to not stop on the white space. But after accepting information and printing out the name, program is stopped. So I can't print out the country part. Why is this happening?

#include <stdio.h>

int main()
{
    int age;
    float name, country;
    
    printf("Age:");
    scanf("%d", &age);
    printf("Name:");
    scanf("%[s^\n]", &name);
    printf("Country:");
    scanf("%s", &country);
    
    return 0;
}

CodePudding user response:

With char arrays:

#include <stdio.h>

int main(void)
{
    int age;
    char name[20], country[20];

    printf("Age:");
    scanf("%d", &age);
    printf("Name:");
    scanf(" [^\n]", name);
    printf("Country:");
    scanf(" s", country);

    printf("Your name is %s, you are %d years old and you live in %s.", name, age, country);

    return 0;
}

CodePudding user response:

Are your name and country a floating value? Well, I don't think so.

You have to declare these variables as an array of characters. For example:

char name[80];

Then you can input a string that includes whitespaces by using this:

scanf("%<79>[s^\n]", &name); // The last one is reserved for the null character.

CodePudding user response:

since you are not reading the \n character from the buffer then the \n will reside there in the buffer preventing the reading of the country variable.

so you should clear the buffer. to that, you can write fflush(stdin); but the problem with it is its Undefined behavior in many compilers for different platforms.

the other way to do it is to use the following code:

void clearBuffer()
{
    int s = 0;
    while (s != EOF && s != '\n')
    {
        s = getchar();
    }
}

where I keep reading all the characters there in the buffer until the buffer is cleared out or I found a valid char in the buffer.

also, there are some problems with your code:

  1. it's char name[50], country[50]; not float name, country; as you are trying to read string, not a floating number.

  2. it's scanf("P[^\n]", name); not scanf("%[s^\n]", &name);

and this is the edited code:

#include <stdio.h>

void clearBuffer()
{
    int s = 0;
    while (s != EOF && s != '\n')
    {
        s = getchar();
    }
}

int main()
{
    int age;
    char name[50], country[50];

    printf("Age:");
    scanf("%d", &age);
    clearBuffer();

    printf("Name:");
    scanf(" I[^\n]", name);
    clearBuffer();

    printf("Country:");
    scanf("%s", &country);
    clearBuffer();

    printf("your age is : %d\n", age);
    printf("your name is : %s\n", name);
    printf("your country is : %s\n", country);


    return 0;
}

and this is some example output:

Age:15
Name:natalie samuel
Country:Canada
your age is : 15
your name is : natalie samuel
your country is : Canada
  •  Tags:  
  • c
  • Related