#include <stdio.h>
#include <stdlib.h>
struct patients{
char last_name[15];
int passport_number;
char disease[30];
char doctors_last_name[15];
};
int main (){
int n,i;
char enter_doctors_last_name [15];
struct patients mas_struct[3]={{"Ivanov",5457401,"COVID-18","Davis"},{"Petrov",2864228,"COVID-19","Davis"},{"Petrova",63863380,"COVID-19","Dixon"}};
printf("\nPatients:");
printf("\n Last name | Passport number | \tDisease | Doctor's last name ");
for (i=0;i<3;i++)
printf("\n %s \t%d \t%s \t%s",mas_struct[i].last_name,mas_struct[i].passport_number,mas_struct[i].disease,mas_struct[i].doctors_last_name);
printf("\n");
printf("\nEnter doctor's last name:");
scanf("%s", enter_doctors_last_name);
printf("\nPatients:");
for (i=0;i<3;i++)
if(mas_struct[i].doctors_last_name == enter_doctors_last_name)
printf("\n %s \t%d \t%s \t%s",mas_struct[i].last_name,mas_struct[i].passport_number,mas_struct[i].disease,mas_struct[i].doctors_last_name);
return 0;
}
Got trouble in comparing chars, it doesn't work and I can't come up with the right words for googling it.
In the last lines when I type "Davis" or "Dixon" for enter_doctors_last_name
Output is just Patients
I also tried to use gets function
You can't compare strings with == operator, use strcmp instead:
if (!strcmp(mas_struct[i].doctors_last_name,enter_doctors_last_name)){/*...*/}
scanf with "%s" specifier is very unsafe use "%14s" instead, the -1 character is to reserve space for the null-terminator.
If you need names with more than 1 word you should use "%14[^\n]", reads everything until the newline character is found.
You should use strcmp() function for comparing strings.
Related
The console will not do anything after the user has entered a string.
I have got the code to work using char string="enteraword" and taking out the whole printf and scanf function, however I need the code to work with a scanf function.
#include <stdio.h>
#include <string.h>
int main()
{
char* string;
printf("Enter a word: ");
scanf("%s", string);
char c=string[1];
printf("The second letter in %s is %c", string, c);
return 0;
}
This code has undefined behavior, you're passing an uninitialized pointer to scanf(), asking it to store a string there.
Also, remember that %s will stop at whitespace, so it's very unclear what "terms" should mean here.
Try e.g.:
char string[1024];
if(scanf("%1023s", string) == 1 && string[0] != '\0')
{
const char c = string[1];
printf("The second letter of '%s' is '%c'\n", string, c);
}
When I give the input then only first alphabet is showing.
I want to print the complete name which is I just entered.
#include <stdio.h>
int main()
{
char name;
char grades;
int i;
printf("Name of the Student:");
scanf("%c",&name);
printf("Name your Just entered is : %c",name);
return 0;
}
I agree with the others - but add some error checking and ensure no buffer overruns i.e
#include <stdio.h>
int main() {
char name[101];
printf("Name of the student:");
if (scanf("%100s", &name) == 1) {
printf("Name you just entered: %s\n", name);
return 0;
} else {
printf("Unable to read name of student\n";
return -1;
}
}
EDIT
As you have edited the question so that it does not have the same meaning as before I will leave my previous solution here.
But what you want is to use fgets - this allows for white space in the name
ie.
#include <stdio.h>
int main()
{
char name[100];
printf("Name of student:");
fflush(stdout);
fgets(name, 100, stdin);
printf("Students name is %s\n", name);
return 0;
}
Replace char name; with char name[100];. This will define name as array of chars, because you handled with it as single character.
For scanf replace it with scanf("%s",&name[0]);, and printf with printf("Name your Just entered is : %s",name);. %s means string, so it will scan whole string, not just single character. In scanf &name[0] points to beginning of array.
You need to scanf into an array, rather than into a single character:
#include <stdio.h>
int main() {
char name[100];
printf("Name of the student:");
scanf("%s", &name);
printf("Name you just entered: %s\n", name);
}
You are trying to store a array of characters(string) in a character. So only the first character is taken.To rectify this initialize the name as:
char name[40];
take input as :
scanf("%s",name);
and print as:
printf("name is %s",name);
name is a char and scanf will only catch one character when you use %c. You can use a char array to store the name instead :
char name[40];
/* edit the size for your need */
Also edit your scanf and printf to use a %s
You are reading (and printing) a single char using %c. If you want to handle stirngs, you should use a char[] and handle it with %s:
#include <stdio.h>
int main()
{
char name[100]; /* Assume a name is no longer than 100 chars */
char grades;
int i;
printf("Name of the Student: ");
scanf("%s",&name);
printf("Name your Just entered is : %s",name);
return 0;
}
#include "stdafx.h"
int main() {
char name;
printf("What is your name:"); // I enter my name..
scanf_s("%c", &name); // Should grab my name in this case (Brian)
printf("Hello, %c\n", name); //Should print "Hello, Brian."
return 0;
}
What's wrong? Why is it not storing the whole name and just the first letter?
Because name is only a single char and you've used %c in scanf (and printf()).
Looks like what you want is:
char name[32]; /* assumes the name is 31 chars or less */
printf("What is your name:"); // I enter my name..
scanf_s("%s", &name); // Should grab my name in this case (Brian)
printf("Hello, %s\n", name); //Should print "Hello, Brian."
But since you have flagged the question as C++, then there are better ways of doing it than this!
Missing parameter:
The fscanf_s function is equivalent to fscanf except that the c, s, and [ conversion specifiers apply to a pair of arguments ... That argument is immediately followed in the argument list by the second argument, which has type rsize_t and gives the number of elements in the array pointed to by the first argument of the pair. C11 §K.3.5.3.2 4
// scanf_s("%c", &name);
scanf_s("%c", &name, (rsize_t) 1);
This fixes the code's undefined behavior, but still will only save/print 1 char of input.
Code should read in a number of char as a string.
#define NAME_SIZE 100
char name[NAME_SIZE];
scanf_s("%s", &name, (rsize_t) NAME_SIZE);
printf("Hello, %s\n", name);
I am trying to copy a sentence into a char array. I have tried using scanf("%[^\n]) and scanf("%[^\n]\n within an if statement but it doesn't work. Can someone please help me figure it out? I am using C language. It works with the first code but not the second.
File #1
#include <stdio.h>
int main ()
{
char c[10];
printf ("Enter text.\n");
scanf("%[^\n]", c);
printf ("text:%s", c);
return 0;
}
File #2
#include <stdio.h>
#include <string.h>
int main(void)
{
char command[10];
char c[10];
printf("cmd> ");
scanf( "%s", command);
if (strcmp(command, "new")==0)
{
printf ("Enter text:\n");
scanf("%[^\n]", c);
printf ("text:%s\n", c);
}
return 0;
}
Put a space before %[^\n] like so:
#include <stdio.h>
#include <string.h>
int main(void)
{
char command[10];
char c[10];
printf("cmd> ");
scanf( "%s", command);
if (strcmp(command, "new")==0)
{
printf ("Enter text:");
scanf(" %[^\n]", c); // note the space
printf ("text:%s", c);
}
return 0;
}
It should work now. The space makes it consume any whitespace of the previous inputs.
Here's my output when I tested it without the space:
cmd> new
Enter text:text:#
------------------
(program exited with code: 0)
And with the space:
cmd> new
Enter text:test
text:test
------------------
(program exited with code: 0)
According to the man page of scanf function, the usual skip of leading white space is suppressed if you use the [ character in the format string. So in the first case, it accepts all the characters until it meets the \n character; In the second case, after the first call of scanf function, the \n character (you press the Enter key in the first call) is still in the input buffer, so if you uses the format string "%[^\n]" in the scanf function, it reads an empty string into the buffer (As already mentioned, it skips the white space in this format case). So you can use the format string " %[^\n]" to force the scanf function to skip the white space.
I have a question simple like that: Let user enter some words from keyboard,one word per line until a '.' (period) entered then print out result, for example:
Enter a word: word1
Enter a word: word2
Enter a word: .
You have entered 2 word(s):
word1
word2
OK here my try but when I run it said file has stopped working after let me enter first word
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
char *word[50]; //each word has maximum 49 character
int i=0, number_of_word;
do
{
printf ("Enter a word: ");
scanf("%s", &word[i]);
i++;
}
while (word[i][0]!='.');
number_of_word =i;
printf ("You entered %d word(s):\n", number_of_word);
for (i=0; i<number_of_word; i++)
{
printf("%s\n", &word[i]);
}
return 0;
}
-----------------------------------------------------------------------
EDIT 1:
OK I try this, it worked but I am still looking for best way to declare an unknown size array of character string since I don't know neither how many word user may enter nor how many letter of each word, in C++ it may called dynamic allocation array, I have no idea how to do it in C
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int main ()
{
char word[20][50]; //array has maximum 20 words, each word maximum 50 character
int i=0, number_of_word;
do
{
printf ("Enter a word: ");
scanf("%s", word[i]);
i++;
}
while (word[i-1][0]!='.');
number_of_word =i-1;
printf ("You entered %d word(s):\n", number_of_word);
for (i=0; i<number_of_word; i++)
{
printf("Word %d is %s\n", i, word[i]);
}
return 0;
}
You are not assigning any memory to store the individual strings, so your program invokes undefined behaviour.
This:
char *word[50];
defines an array of 50 pointers, but no further storage.
And when you do this:
scanf("%s", &word[i]);
you're writing into the pointer array.
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
int main () {
char *word[50]; //each word has maximum 50 word
char enter_word[50];//maximum 49 character
int i=0, number_of_word;
while(1){
printf("Enter a word: ");
scanf("%49s", enter_word);
if(*enter_word == '.')break;
word[i++]=strdup(enter_word);
}
number_of_word = i;
printf ("You entered %d word(s):\n", number_of_word);
for (i=0; i<number_of_word; i++){
printf("%s\n", word[i]);
}
for(i=0;i<number_of_word; ++i)
free(word[i]);
return 0;
}
When you declare
char *word[50];
You have 50 char pointers pointing to random memory. What you want is something like this:
char word[50][50];
Note that you can have only 49 words (and the '.'), and each can have less than 50 characters (don't forget the \0, so a word with 50 chars will overflow).
And you will want to change your scanf call to something like:
scanf("%s", word[i]);
Note that you do not need the &, since word[i] is already a pointer.
char *word[50];
You have an array of 50 pointers. You want an array of 50 byte chunks. How many words do you want to read? If you know the maximum, you can allocate the space in advance. If you don't, you need something like a linked list of strings, to which you can append a new item when you read it.
Its your printf Call. Remember to add the end character \0 to the end of the word. Printf when printing strings prints everything in the char array till it reads an end character \0
BasicAlly... Hos should printf know how long the word is and when it should stop printing?(Right now it continues forever)
You've done nothing to allocate memory for the individual strings.
Char *word[50] just declares a pointer to one array of char pointers.
EDIT
I'll try to illustrate this here with some pseudo code...formatted code on a phone is nearly impossible :) Given a prior char * input[50]:
char * nextString;
bool entering=true;
do{
nextString =calloc(50);
// enter the string to nextString
if (nextString[0] != '.'){
input[i] = nextString;
i++;
} else{
entering=false;
}
}
}while(entering)
"char *word[50]" have done nothing to allocate the memory for the individual string.
you should allocate it by using the function of alloc
you just try to use "%[^\n]" place of "%s" in scanf().
because "%s" stores characters until it found first space in the string.
try this with your very first asked Program
May its help to you.