1

I'm trying to simply read input from the user and store a CD record into some variables. All the variables details are correctly printed out for all the variables except for the second array of char for artist which isn't printing anything. I've tried to deal with the extra character spacing by introducing the space at the front of each of my formatted strings in scanf(), but that hasn't fixed it.

void displayCDInfo(char* title, char* artist, short noOfTracks, int isAlbum, float price);

int main()
{
    char title[61];
    char artist[41];
    short noOfTracks = 0;
    int isAlbum = 0;
    float price = 0.0;
    char albumOrSingleResponse;

    printf("Please enter the title: ");
    scanf(" %s", title);

    printf("Please enter the artist: ");
    scanf(" %s", artist);

    printf("Please enter the number of records: ");
    scanf(" %d", &noOfTracks);

    printf("Please enter whether or not the cd is an album/single (A or S): ");
    scanf(" %c", &albumOrSingleResponse);

    if(albumOrSingleResponse == 'A')
    {
        isAlbum = 1;
    }

    printf("Please enter the price of the CD: ");
    scanf(" %f", &price);

   displayCDInfo(title, artist, noOfTracks, isAlbum, price);

   return 0;
}

void displayCDInfo(char* title, char* artist, short noOfTracks, int isAlbum, float price)
{
    printf("\nThe title of the CD is %s", title);
    printf("\nThe name of the artist is %s", artist);
    printf("\nThe number of tracks on this CD are %d", noOfTracks);
    printf("\nThe CD is an %s", (isAlbum == 1) ? "album" : "single");
    printf("\nThe price of the cd is $%.2f", price);
}
abhishek_naik
  • 1,287
  • 2
  • 15
  • 27
Lewis Briffa
  • 557
  • 3
  • 6
  • 15

3 Answers3

1

Remove the space: scanf("%s", ...)
and add a space: scanf(" %c", ..)

int main()
{
    char title[61];
    char artist[41];
    short noOfTracks = 0;
    int isAlbum = 0;
    float price = 0.0;
    char albumOrSingleResponse;

    printf("Please enter the title: ");

scanf("%s", title);

printf("Please enter the artist: ");
scanf("%s", artist);

printf("Please enter the number of records: ");
scanf("%hu", &noOfTracks);

printf("Please enter whether or not the cd is an album/single (A or S): ");
scanf(" %c", &albumOrSingleResponse);

if(albumOrSingleResponse == 'A')
{
    isAlbum = 1;


   }

    printf("Please enter the price of the CD: ");
    scanf("%f", &price);

   displayCDInfo(title, artist, noOfTracks, isAlbum, price);

   return 0;
}

Because when you add a space before " %c" it consumes newline characters entered with previous inputs.


Edit: From this, you may use "%hu" for printing the short to avoid any warning.

Community
  • 1
  • 1
Ajeet Shah
  • 18,551
  • 8
  • 57
  • 87
1

This will work. Just move the position of char array declaration from top of main body.

void displayCDInfo(char* title, char* artist, short noOfTracks, int isAlbum, float price);

int main()
{
    short noOfTracks = 0;
    int isAlbum = 0;
    float price = 0.0;
    char albumOrSingleResponse;
    char title[61];
    char artist[41];

    printf("Please enter the title: ");
    scanf(" %s", title);

    printf("Please enter the artist: ");
    scanf(" %s", artist);

    printf("Please enter the number of records: ");
    scanf(" %d", &noOfTracks);

    printf("Please enter whether or not the cd is an album/single (A or S): ");
    scanf(" %c", &albumOrSingleResponse);

    if(albumOrSingleResponse == 'A')
    {
        isAlbum = 1;
    }

    printf("Please enter the price of the CD: ");
    scanf(" %f", &price);

   displayCDInfo(title, artist, noOfTracks, isAlbum, price);

   return 0;
}

void displayCDInfo(char* title, char* artist, short noOfTracks, int isAlbum, float price)
{
    printf("\nThe title of the CD is %s", title);
    printf("\nThe name of the artist is %s", artist);
    printf("\nThe number of tracks on this CD are %d", noOfTracks);
    printf("\nThe CD is an %s", (isAlbum == 1) ? "album" : "single");
    printf("\nThe price of the cd is $%.2f", price);
}
Rijul Sudhir
  • 2,064
  • 1
  • 15
  • 19
1

I figured out how to fix this now whilst keeping noOfTracks as a short. Since I was using "%d" for scanf on the Number of tracks variable I was overwriting the space allocated for the previous variable which was artist, hence why it wasn't being displayed. To solve this I had to change the "%d" to "%hu" for short int.

void displayCDInfo(char* title, char* artist, short noOfTracks, int isAlbum, float price);

int main()
{
    char title[61];
    char artist[41];
    short noOfTracks = 0;
    int isAlbum = 0;
    float price = 0.0;
    char albumOrSingleResponse;

    printf("Please enter the title: ");
    scanf("%s", title);

    printf("Please enter the artist: ");
    scanf("%s", artist);

    printf("Please enter the number of records: ");
    scanf("%hu", &noOfTracks);

    printf("Please enter whether or not the cd is an album/single (A or S): ");
    scanf(" %c", &albumOrSingleResponse);

    if(albumOrSingleResponse == 'A')
    {
        isAlbum = 1;
    }

    printf("Please enter the price of the CD: ");
    scanf(" %f", &price);

    displayCDInfo(title, artist, noOfTracks, isAlbum, price);

    return 0;
}

void displayCDInfo(char* title, char* artist, short noOfTracks, int isAlbum, float price)
{
    printf("\nThe title of the CD is %s", title);
    printf("\nThe name of the artist is %s", artist);
    printf("\nThe number of tracks on this CD are %hu", noOfTracks);
    printf("\nThe CD is an %s", (isAlbum == 1) ? "album" : "single");
    printf("\nThe price of the cd is $%.2f", price);
}
Lewis Briffa
  • 557
  • 3
  • 6
  • 15
  • It seems that you were getting a warning as you declared `noOfTracks` as `short` but used `"%d"` (`int`) format to print it. As a side note: You can always compile your program with: `gcc -Wall -Wextra your_file.c` , it will give you `all` and `extra` hidden warnings :) – Ajeet Shah Jun 13 '16 at 19:54