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);
}
6
  • Perhaps using %s is not a good idea - see scanf - One assumes that some titles are multiple words Commented Jun 11, 2016 at 11:10
  • Only give space for the second and fourth scanf statement Commented Jun 11, 2016 at 11:10
  • %s doesnt work with even one word, and gets and puts are demonstrating the same problem Commented Jun 11, 2016 at 11:22
  • 1
    As an starting point change scanf(" %s", title); to scanf("%s", title); Commented Jun 11, 2016 at 11:24
  • 2
    never use scanf("%s", ..); (why do this so many beginners?). better use fgets(), alternative use scanf("%"MAXLEN"s",...). otherwise you can get a array overflow and UB really fast. Commented Jun 11, 2016 at 12:29

3 Answers 3

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);
}
Sign up to request clarification or add additional context in comments.

Comments

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);
}

1 Comment

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 :)
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.