What am I doing wrong here (aside from asking about C in a C++ forum)?

I'm trying to use the fgets function here; in the book I'm using, the original code example uses gets and I don't like that.

But there's a problem. When I try to print my string fullLocation with puts or fputs, all I get printed is a comma.

Here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Example program #2 from Chapter 19 of Absolute Beginner's Guide
// to C, 3rd Edition
// File chap19ex2.c
// Osman Zakir
// 11 / 23 / 2016

/* This program asks a user for their hometown and the two-letter
abbreviation of their home state. It then uses string concatenation
to build a new string with both town and state and prints it using
puts. */

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

int main()
{
	char city[15] = "", st[3] = "";
	
	puts("What town do you live in? ");
	fgets(city, strlen(city), stdin);
	int ch;
	while (((ch = fgetc(stdin)) != '\n') && (ch != EOF));

	
	puts("What state do you live in? (2-letter abbreviation) ");
	fgets(st, strlen(st), stdin);
	while (((ch = fgetc(stdin)) != '\n') && (ch != EOF));

	
	char fullLocation[18] = "";
	strcpy(fullLocation, city);
	strcat(fullLocation, ", ");
	strcat(fullLocation, st);
	
	puts("You live in ");
	fputs(fullLocation, stdout);
	return 0;
}


How do I fix this? What am I missing? Thanks in advance (note: I'm trying to use TDM-GCC to compile this on the MinGW Command Prompt. Using LLVM Clang, I can't compile it if I use gets (plus I have to be using the VS2015 Developer Command Prompt)).
Last edited on
1
2
3
char city[15] = "", st[3] = "";
fgets(city, strlen(city), stdin);
fgets(st, strlen(st), stdin);
your strings start empty, ¿what do you think is their lenght?
use sizeof instead
Oh, so it was that. Thanks. I'll try that and see if it works.

Edit for update: It kind of worked, but now I have a newline after the city name and before the comma.

Here's the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Example program #2 from Chapter 19 of Absolute Beginner's Guide
// to C, 3rd Edition
// File chap19ex2.c
// Osman Zakir
// 11 / 23 / 2016

/* This program asks a user for their hometown and the two-letter
abbreviation of their home state. It then uses string concatenation
to build a new string with both town and state and prints it using
puts. */

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif

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

int main()
{
	char city[15] = "", st[3] = "";
	
	puts("What town do you live in? ");
	fgets(city, sizeof(city), stdin);
	fflush(stdin);

	puts("What state do you live in? (2-letter abbreviation) ");
	fgets(st, sizeof(st), stdin);
	fflush(stdin);

	char fullLocation[18] = "";
	strcpy(fullLocation, city);
	strcat(fullLocation, ", ");
	strcat(fullLocation, st);
	
	puts("You live in ");
	fputs(fullLocation, stdout);
	return 0;
}
Last edited on
man wrote:
fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.

so that's is the intended behaviour (¿?).
To remove it http://stackoverflow.com/a/28462221
I want to take out the newline, especially if that'll clean up the output result.
Topic archived. No new replies allowed.