Reputation: 5
I need to make it so that this loop stops input when nothing is entered (the enter key is pressed) for charTeacherFirstName. I have this but the problem is that if the enter key is pressed the loop just keep asking for stuff as if it didn't read the blank.
int main(void)
{
int intTeacherSalary[50];
char charTeacherFirstName[50][50];
char charTeacherLastName[50][50];
do
{
printf("Enter the teacher's first name: ");
fgets(charTeacherFirstName, 50, stdin);
if (charTeacherFirstName == '\n')
{
break;
}
else
{
printf("Enter the teacher's last name: ");
fgets(charTeacherLastName, 50, stdin);
printf("Enter teacher's salary: ");
fgets(intTeacherSalary, 50, stdin);
}
} while (charTeacherFirstName != '\n');
}
Upvotes: 0
Views: 1018
Reputation: 487
At first your code is wrong. charTeacher...Name
is a 2dim array and you don't use it like this. Then ...Salary
is int
but you read it with fgets
while its incompatible.
Also you cannot compare a string
and a char
, these things differ a lot.
So a char is what the name says. A single character. 'a', 'b', '!', '5'
are chars. When you do an immediate reference on them you need to put them between single quotes '...'
.
Strings are let's say series of chars ("abc", "food", "what a nice weather"
. You may think them as arrays, as you have declared them. They can also be declared as pointers on chars and then you will need to use malloc(N * sizeof(char))
(or something like this) to get the memory needed for your operations. When you want immediate reference on a string you have to put it between double quotes ("..."
). Strings have to end with the NULL
character (that is '\0'
).
I would say it is kind of the same you would say about int
and int []
or int *
in each case.
In order to compare strings you need to use strcmp
. Use man strcmp
to see how it is used and what it returns as well as which library you need to #include
.
Your condition should then be if (strcmp(charTeacherFirstName, "\n") == 0) break;
Upvotes: 1
Reputation: 144780
There are multiple problems in your code:
You do not test the return value of fgets()
. It will return NULL
at end of file, and you should not continue prompting for more input then.
You should read the strings into the next array element for each array.
You do not need a do
/ while
loop, you already correctly test the end case, use a for
loop and stop when the arrays are full.
you read lines into the arrays but do not remove the trailing linefeed.
Here is a corrected version:
#include <stdio.h>
void trim_linefeed(char *buf) {
int len = strlen(buf);
if (len > 0 && buf[len - 1] == '\n')
buf[len - 1] = '\0';
}
int main(void) {
char buf[80];
int intTeacherSalary[50];
char charTeacherFirstName[50][50];
char charTeacherLastName[50][50];
int i, n;
for (n = 0; n < 50; n++) {
printf("Enter the teacher's first name: ");
if (!fgets(charTeacherFirstName[n], 50, stdin))
break;
if (charTeacherFirstName[n][0] == '\n')
break;
trim_linefeed(charTeacherFirstName[n]);
printf("Enter the teacher's last name: ");
if (!fgets(charTeacherLastName[n], 50, stdin))
break;
trim_linefeed(charTeacherLastName[n]);
printf("Enter teacher's salary: ");
if (!fgets(buf, sizeof buf, stdin))
break;
if (sscanf(buf, "%d", &intTeacherSalary[n]) != 1) {
intTeacherSalary[n] = 0;
}
}
printf("%d teachers:\n", n);
for (i = 0; i < n; i++) {
printf("%s %s: %d\n",
charTeacherFirstName[i],
charTeacherLastName[i],
intTeacherSalary[i]);
}
return 0;
}
Upvotes: 0