0

So I have this code here to take three input from the user, one integer type and 2 character types:

#include <stdio.h>

int main() {

    int carNumber;
    char customerName, carCode;

    printf("Enter Car Number: ");
    scanf("%d", &carNumber);
    printf("Enter Customer Name: ");
    scanf("%s", &customerName);
    printf("Enter Car Code: ");
    scanf("%s", &carCode);

    if (carCode == 'T'){
        printf("Toyota");
    }
    else if (carCode == 'H'){
        printf("Honda");
    }

    printf("\nCar Number: %d", carNumber);
    printf("\nCustomer Name: %s", customerName);
    return 0;

}

While most of it outputs correct, the customerName always prints (null).

max1eee -
  • 45
  • 5
  • 4
    The variables `customerName` and `carCode` are *single characters*. They can't hold null-terminated strings (which you try to read into them). If `carCode` is supposed to be a single character, use the format string `" %c"` to read it (note the leading space in the format string). If `customerName` is supposed to be a string you need an array of characters (e.g. `char customerName[100];`). It almost looks like you skipped some rather important parts of your text-book. – Some programmer dude Nov 26 '21 at 06:15
  • I tried separating them into two different lines as ```char customerName[20];``` and ```char carCode;``` to differentiate them but ```customerName``` outputs nothing now. – max1eee - Nov 26 '21 at 06:26
  • I agree with @Someprogrammerdude max1eee- I have edited your entire program, U may test it. – Urvashi Soni Nov 26 '21 at 06:46

1 Answers1

0

To print a character, its " %c" and not "%s". Either use :

char customerName[30];
printf("Customer Name: %s", customerName);

OR

char customerName;
printf("Customer Name: %c", customerName);

Here is your entire program :

#include <stdio.h>

int main()
{
    int carNumber;
    char carCode;
    char customerName[30];

    printf("Enter Car Number: ");
    scanf("%d", &carNumber);
    printf("Enter Customer Name: ");
    scanf("%s", customerName);
    printf("Enter Car Code: ");
    scanf(" %c", &carCode);

    if (carCode == 'T'){
        printf("Toyota\n");
    }
    else if (carCode == 'H'){
        printf("Honda\n");
    }

    printf("Car Number: %d\n", carNumber);
    printf("Customer Name: %s\n", customerName);
     printf("Car Code: %c\n", carCode);
    return 0;
}
Urvashi Soni
  • 279
  • 1
  • 3
  • 13
  • I will input a name, not just a single character, and string is not accepted to be a keyword in C. Thanks though! – max1eee - Nov 26 '21 at 06:22
  • @Shawn I edited my response, thanks for pointing. – Urvashi Soni Nov 26 '21 at 06:25
  • @max1eee- I edited my ans, please check. – Urvashi Soni Nov 26 '21 at 06:25
  • On an unrelated note: Printing leading newlines is a bad habit. When `stdout` (where `printf` writes) is connected to a terminal then it's supposed to be *line* buffered, meaning output is actually written when there's newline. So by printing a leading newline you flush and print the *previous* line, not the current. – Some programmer dude Nov 26 '21 at 06:34
  • Okay how tf. I did this a while ago and it didn't print anything but when I copied yours and it worked, I am so confused. Regardless, thank you to all! – max1eee - Nov 26 '21 at 06:58
  • Did u notice the space before `%c`? Also take care of the Leading newline as suggested by @Someprogrammerdude If my code works, u can mark it as the accepted ans. – Urvashi Soni Nov 26 '21 at 07:04
  • Is that it? What's the difference with a space and without it? There's nothing written about it on my school's learning materials or in the Internet. Again, thank you, I've accepted it. – max1eee - Nov 26 '21 at 07:24
  • You can refer this : https://stackoverflow.com/a/13543113/6863495 – Urvashi Soni Nov 26 '21 at 07:40