1

I'm trying to create a small program using C programming. Essentially while running in the console, the program should receive a sentence from a user.

Deliverables:

  1. I want to be able to ask users their name
  2. prompt the user to enter a write a short sentence
  3. output a user name and their sentence prompt
  4. when the user types into the text area and presses Enter, it will Submit.

**Not sure if I have to create an HTML file to connect my c file to the form for the submit action. Below is what I have made so far. Thank you!

#include <stdio.h>
  
int main()
{
  char word[100];
  char name;

  printf("what is your name? ");
      scanf("%c", &name);
    
  printf("Enter your sentence: ");
    
    scanf("%s", word);
    printf("Output : %s", word);
    return 0;
}
Clifford
  • 88,407
  • 13
  • 85
  • 165
iosvaldo
  • 23
  • 8
  • 4
    Don't use `scanf`, use `fgets` to get the whole line and then you can use `sscanf` or another function to parse the line. – Pablo Sep 08 '22 at 03:19
  • 2
    Note that currently your program only allows a person's name to be exactly one character. – paddy Sep 08 '22 at 03:19
  • 1
    "when the user types into the text area and presses Enter, it will Submit" -- this does not sound like a C program. – pynexj Sep 08 '22 at 03:22
  • 2
    `stdin` is line based. Is a "sentence" same thing as a line? Or a line which ends in a full stop? Or something else? – hyde Sep 08 '22 at 03:24

3 Answers3

1

To get user input either use gets_s() or fgets().

#include <stdio.h>
  
int main()
{
  char name[100];
  printf("what is your name? ");
  gets_s( name, sizeof(name) );
    
  char sentence[100];
  printf("Enter your sentence: ");
  gets_s( sentence, sizeof(sentence) );

  printf( "Hi %s! You wrote: %s\n", name, sentence );
  return 0;
}

I prefer fgets() because you can determine whether or not you read the user’s entire line by whether or not a newline at the end of the input string.

I am uncertain how you intend to use this with an HTML form. Are you messing with a server CGI program? Do you have Apache or something else useful set up to test it all with? Is this for employment or personal?

[edit] As per commentary and continuing from above, fgets() is my typical go-to solution. However, it needs some help. At the very minimum you must check for and remove the newline. A helper function is useful:

char * getline( FILE * f, char * s, size_t n )
{
  if ((n < 1) or !fgets( s, n, f )) return NULL;
  char * p = strchr( s, '\n' );
  if (p) *p = '\0';
  else
  {
    // The input line is larger than (n-1), meaning
    // that the entire line of input was not read --
    // only the part of it that fit in `s` was read.
    // The way you deal with this condition is up to you.
  }
  return s;
}

I personally prefer a solution that allocates and returns the input buffer (within a reasonable size limit, say, 4K bytes). That is overkill for a lot of stuff, though.

Dúthomhas
  • 8,200
  • 2
  • 17
  • 39
  • Hey @Duthomhas, thank you for your input; for some reason, the gets_s wasn't working for me, so I modified it a bit. Here's the code below. – iosvaldo Sep 09 '22 at 19:47
0

The code below seems close to what I'm looking for, but I cannot get the exclamation mark to show after the user's name and not join with the user's sentence. For example ...

Hi, username! You Wrote: I love programming.

#include <stdio.h>

int main(){
  char name[100];
  printf("Hi, What is your name ? ");
  fgets( name, sizeof(name),stdin);

  char sentence[100];
  printf("Enter you sentence: ");
  fgets( sentence,sizeof(sentence),stdin);

  printf( "Hi %s You Wrote: %s\n", name, sentence);
  return 0;
}
iosvaldo
  • 23
  • 8
  • As always, read the documentation. `fgets()` does _not_ discard the newline (Enter key) from the input — you must do that yourself. `strchr()` can help with that. – Dúthomhas Sep 09 '22 at 23:40
  • @Dúthomhas Great! By the way, is this the documentation you're referring to? https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf – iosvaldo Sep 10 '22 at 04:48
-2

In your code you have enterd %c in name but it will only accept one character of the entered string but instead of this if you will user %s then it will accept complete string untill whitespace character. So In order to accept complete string with whitespace character you have to write %[^\n] or %[^\n]s.

#include<stdio.h>
int main(){
char word[100], name[50];
printf("What is your name ?");
scanf("%[^\n]",name);
printf("Enter your statement : ");
scanf(" %[^\n]",word);
printf("Output : %s",word);
return 0;
}

Method : 2

If you want to use another method to accept user input as string you have to add include one library of c which is <String.h> and then you can use gets function to accept complete string from the user.

Code :-

#include<stdio.h>
#include<string.h>
int main(){
char word[100], name[50];
printf("What is your name ?");
gets(name);
printf("Enter your statement : ");
gets(word);
printf("Output : %s",word);
return 0;
}
Om Maniya
  • 24
  • 1
  • 4
    `scanf("%[^\n]",name);` is as bad as [`gets()`](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) as it has no width limit. Consider `fgets()`. – chux - Reinstate Monica Sep 08 '22 at 03:58
  • 1
    Please don't teach beginners to use functions that were flagged obsolete in the 1990s. I have no idea why you are on about string.h either. – Lundin Sep 08 '22 at 11:13
  • Always, always, always _bound_ your inputs or enjoy the consequences of [buffer overflow](https://en.wikipedia.org/wiki/Buffer_overflow). – Dúthomhas Sep 08 '22 at 20:02