-1

So I was writing a quick program to get information about a patient from a hospital and it keeps skipping the scanf() at a certain point (at around line 34) and moves on to the scanf() after it. Here's the part that keeps bothering the life out of me:

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

int main(void){
    char choice_1, choice_2, *blood_group, *spec_conditions, *allergies;
    printf("Enter the patient's medical details.\n\n");
    printf("Enter your blood group: ");
    scanf("%s",&blood_group);
    printf("Does the patient have any allergies:(y/n)");
    scanf("%c",&choice_2);
    if (choice_2 == 'y'){
        printf("Kindly enter the allergies: ");
        scanf("%s",&allergies);
    }else{
        allergies = "No allergies";
    }
    printf("\nDoes the patient have any special conditions:(y/n)");
    scanf("%c",&choice_1);
     if (choice_1 == 'y'){
        printf("Kindly enter the condtion: ");
        scanf("%s",&spec_conditions);
    }else{
        spec_conditions = "No special conditions";
    }
    printf("Displaying details...\n");
    sleep(2);
    system("cls");
    printf("\t\t\tPATIENT DETAILS\n");
    sleep(1);
    return 0;
}
sweenish
  • 4,793
  • 3
  • 12
  • 23
Overwatch
  • 19
  • 2
  • 5
    Duplicate. Your `%c` reads the newline left behind by `%s`. The fix is to use `” %c”` to skip white space. – Jonathan Leffler Oct 15 '22 at 15:57
  • Your scanf pretend to store on unitialized pointers. – ulix Oct 15 '22 at 16:04
  • See these [guidelines for calling `scanf` simply and safely](https://stackoverflow.com/questions/72178518#72178652). – Steve Summit Oct 15 '22 at 16:09
  • 2
    In your question, you are referring to line 34. However, your posted code only has 32 lines and therefore does not have a line 34. Please [edit] your question to clarify it. – Andreas Wenzel Oct 15 '22 at 16:17

1 Answers1

2

Problem 1:

char *blood_group, *spec_conditions, *allergies;
scanf("%s",&blood_group);
scanf("%s",&allergies);
scanf("%s",&spec_conditions);

What this does is it attempts to write a string of an unknown length to the address of char * variables, resulting in a 'buffer' overflow if the string is larger than a pointer, or a wild pointer that points to a random location.

Do not pass the address of the pointer to scanf, pass the pointer itself. Furthermore, the pointer must point to writable memory. This can be done by declaring it as an array.

Problem 2:

scanf("%c") reads the newline left when the user hits enter to read the string and moves on. Use scanf(" %c") instead to skip leading whitespace and read the actual character.

Try:

int main(void){
    char choice_1, choice_2, blood_group[256], spec_conditions[256], allergies[256];
    printf("Enter the patient's medical details.\n\n");
    printf("Enter your blood group: ");
    scanf("%255s",blood_group);
    printf("Does the patient have any allergies:(y/n)");
    scanf(" %c",&choice_2);
    if (choice_2 == 'y'){
        printf("Kindly enter the allergies: ");
        scanf("%255s",allergies);
    }else{
        strcpy(allergies, "No allergies");
    }
    printf("\nDoes the patient have any special conditions:(y/n)");
    scanf(" %c",&choice_1);
     if (choice_1 == 'y'){
        printf("Kindly enter the condtion: ");
        scanf("%255s",spec_conditions);
    }else{
        strcpy(spec_conditions, "No special conditions");
    }
    printf("Displaying details...\n");
    sleep(2);
    system("cls");
    printf("\t\t\tPATIENT DETAILS\n");
    sleep(1);
    return 0;
}
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
user16217248
  • 3,119
  • 19
  • 19
  • 37