Samjohns081998
Samjohns081998

Reputation: 141

scanf() is not working for getting a sentence

I am learning the language C by myself and with the help of internet. I came across an exercise, and I was able to read in everything with integers and double, but allowing the user to type in a full sentence and store it in a variable has given me hard time. Can someone explain how I can get a sentence from the user, and store it in a variable. I have tried many things, such as [%^\n] with scanf, and also fget but I am having some trouble. For some reason, it is not working.

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

int main() {
    int i = 4;
    double d = 4.0;
    char s[] = "Orange ";


    // Declare second integer, double, and String variables.
    int secondInt;
    double justDouble;
    char variable[500];

    // Read and save an integer, double, and String to your variables.
    scanf("%d", &secondInt);
    scanf("%lf", &justDouble);
    scanf("%[^ \n]", variable);


    // Print the sum of both integer variables on a new line.
    printf("%i\n ", i + secondInt);

    // Print the sum of the double variables on a new line.
    printf("%.1lf\n ", d + justDouble);

    // Concatenate and print the String variables on a new line
    printf("%s ", s);
    printf("%s ", variable);
    // The 's' variable above should be printed first.

    return 0;    
}

Upvotes: 1

Views: 461

Answers (2)

Aganju
Aganju

Reputation: 6405

To work with scanf, you need to make sure that everything entered gets read. In your example, you first expect an integer, and then a double. What does the user type to 'finish' entering the integer? Probably a <RET> (or a blank) - and now you need to scanf these too! Or they will 'clog' the input stream.
For example, your second scanf could be scanf(" %lf"... - note the blank before the % sign, it will read (and discard) any number of whitespace (which is <RET>, <TAB>, <space>).

scanf is very powerful, but needs a lot of detail understanding to be used correctly. Most people don't get it, and therefore claim "it's old and bad and shouldn't be used".
In professional software, it is generally avoided; not because it's not capable, but because the chance is too high that is used wrong, or that it is encountered by a developer that changes it and messes it up.

Upvotes: 1

Abdullah Meda
Abdullah Meda

Reputation: 119

This should do the trick by using fgets() in general

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

int main()
{
  // variable to store the message
  char msg[100];

  // prompting the user to enter the message
  printf("Pls enter a msg: ");

  // using fgets() to retrieve a whole sentence from the user
  fgets(msg, 100, stdin);

  // printing the message to stdout
  printf("%s", msg);
}

You can learn more about fgets here: https://www.tutorialspoint.com/c_standard_library/c_function_fgets.htm

Let me know if anything is not clear so I can improve my answer

Upvotes: 1

Related Questions