0

I am currently attempting to print out an array of input, only one part of the 2D array is being left aligned, while the other part is being sent to the next line, any help would be great! The issue is specifically with my printSongInfo function, but have included the rest of the code to help with understanding. Im looking for the artist to be displayed as left justified and shown in the first 35 characters of the line, and the song to be left justified and shown in the next 35 characters of the same line before going to the next line and repeating the process for all 10 index's.

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

#pragma warning(disable:4996)

int getSongInfo(struct songInfo *pFillInfo, char *artistName, char *songName);
void printSongInfo(struct songInfo songList[10]);

struct songInfo {

    char *songArtist;
    char *songTitle;
};

int main(void)
{
    struct songInfo *fillPtr;
    struct songInfo songList[10];
    fillPtr = &songList[0];

    char tempArtist[30][10];
    char tempSong[30][10];

    int i = 0;
    int counter = 0;
    int arrayCounter = 0;
    while (counter != 10)
    {
        printf("Please enter the artist name: ");
        fgets(tempArtist[counter], sizeof(tempArtist[counter]), stdin);
        printf("Please enter the song name: ");
        fgets(tempSong[counter], sizeof(tempSong[counter]), stdin);

        getSongInfo(&fillPtr[arrayCounter], tempArtist[counter], tempSong[counter]);

        printf("Song and Artist Captured! \n");
        counter++;
        arrayCounter++;
    }

    printSongInfo(fillPtr);
}

int getSongInfo(struct songInfo *pFillInfo, char *artistName, char *songName)
{
    pFillInfo->songArtist = (char*)malloc(strlen(artistName) + 1);
    pFillInfo->songTitle = (char*)malloc(strlen(songName) + 1);

    strcpy(pFillInfo->songArtist, artistName);
    strcpy(pFillInfo->songTitle, songName);



    return 1;
}

void printSongInfo(struct songInfo songList[10])
{
    int counter = 0;


    while (counter != 10)
    {
        printf("%-35s %-35s\n", songList[counter].songArtist, songList[counter].songTitle);
        counter++;
    }

}

Im looking for number that matches to be the same and not be sent to the next line. So 1 & 1 should be seen on the same line.

Current Output:

enter image description here

RocktheFries
  • 221
  • 2
  • 10
  • 1
    Please [edit] your question and add (copy&paste) the input you used, the output you get and how the output should look like. – Bodo May 16 '19 at 14:23
  • Did the compiler show you any warnigs? – Jabberwocky May 16 '19 at 14:24
  • @Bodo Updated with screenshot to show what output Im getting. – RocktheFries May 16 '19 at 14:26
  • @Jabberwocky no warning or errors are being shown. – RocktheFries May 16 '19 at 14:27
  • 1. Please don't post picture of text, post text as text. 2. What output do you expect? – Jabberwocky May 16 '19 at 14:28
  • 1
    Oh and did you read the documentation of `fgets` carefully? What is the last character of the steing read via `fgets`? – Jabberwocky May 16 '19 at 14:29
  • @xing that did the trick I forgot about the newline tacked on with fgets thank you! Also I changed the arrays to align with your above comment as I got them mixed up! Thank you again! – RocktheFries May 16 '19 at 14:33
  • There was a clue in the double line-spacing too. One handy debugging tip is to enclose the output strings in, say, square brackets so you can see exactly what is output, `printf("[%s]", ...)` – Weather Vane May 16 '19 at 14:34
  • @WeatherVane I did try this, and looked through everything but been working on this one program so far to long, so the newline from fgets was far overlooked! Thank you :D – RocktheFries May 16 '19 at 14:36

0 Answers0