Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C programming. Need my code fixed please, its printing out random letters #include #include #include #include int main(void) { char title[50]; char col1[50]; char col2[50];

C programming. Need my code fixed please, its printing out random letters

#include

#include

#include

#include

int main(void)

{

char title[50];

char col1[50];

char col2[50];

int point[50];

char names[50][50] = {'\0'};

int count = 0;

char dataPoint[50];

int commas = 0;

int i = 0, j = 0, k = 0, p = 0, len;

char integerValue[5] = "";

int commaAfterInt = 0;

printf("Type a title for the data: ");

fgets(title, 50, stdin);

printf("You typed: %s ", title);

printf("Type the column 1 header: ");

fgets(col1, 50, stdin);

printf("You typed: %s ", col1);

len = strlen(col1);

if (len > 0 && col1[len - 1] == ' ')

{

col1[--len] = '\0';

}

printf("Type the column 2 header: ");

fgets(col2, 50, stdin);

printf("You typed: %s ", col2);

while (count

{

printf("Type a data point (-1 to stop input): ");

fgets(dataPoint, 50, stdin);

if (atoi(dataPoint) == -1)

{

break;

}

commas = 0;

i = 0;

strcpy(integerValue, "");

commaAfterInt = 0;

while (dataPoint[i] != '\0')

{

if (dataPoint[i] == ',')

{

commas++;

if (!isdigit(dataPoint[i + 1]) && !isdigit(dataPoint[i + 2]))

{

commaAfterInt = 1;

}

}

else if (commas == 0)

{

names[count][i] = dataPoint[i];

}

else if (isdigit(dataPoint[i]))

{

for (j = 0; integerValue[j] != '\0'; j++)

;

integerValue[j] = dataPoint[i];

integerValue[j + 1] = '\0';

}

i++;

}

if (commas == 0)

{

printf("No comma in string. ");

}

else if (commas > 1)

{

printf("Error: Too many commas in input. ");

}

else if (commaAfterInt == 1)

{

printf("Error: Comma not followed by an integer. ");

}

else

{

point[count++] = atoi(integerValue);

k = 0;

p = 0;

strcpy(names[count], "");

while (dataPoint[p] != ',')

{

names[count][k] = dataPoint[p];

k++;

p++;

}

names[count][k] = '\0';

printf("Data string: %s ", names[count]);

printf("Data integer: %d ", point[count - 1]);

printf(" ");

}

} printf(" "); printf(" %33s", title);

printf("%-20s | %23s", col1, col2);

printf("--------------------------------------------- ");

i = 0;

while (i

{

printf("%-20s | %23d ", names[i], point[i]);

i++;

}

return 0;

}

my output below is my error

image text in transcribed

Expected output below

image text in transcribed

Number f Novels Auth red Author name Number of novels Jane Austen Charles Dickens 20 Ernest Hemingway 9 Jack Kerouacgway F. Scott FitzgeraldI Mary Shelleyzgerald I Charlotte Bronte Mark TwainBronte Agatha Christie Ian Flemmingtie J. K. Rowling Stephen King Oscar Wilde 7 73 14 14 54 Number f Novels Auth red Author name Number of novels Jane Austen Charles Dickens Ernest HemingwayI Jack Kerouac F. Scott Fitzgerald Mary Shelle Charlotte Bronte I Mark Twai Agatha Christie Ian Flemming J.K. Rowling Stephen King Oscar Wilde 20 73 14 14 54 Number f Novels Auth red Author name Number of novels Jane Austen Charles Dickens 20 Ernest Hemingway 9 Jack Kerouacgway F. Scott FitzgeraldI Mary Shelleyzgerald I Charlotte Bronte Mark TwainBronte Agatha Christie Ian Flemmingtie J. K. Rowling Stephen King Oscar Wilde 7 73 14 14 54 Number f Novels Auth red Author name Number of novels Jane Austen Charles Dickens Ernest HemingwayI Jack Kerouac F. Scott Fitzgerald Mary Shelle Charlotte Bronte I Mark Twai Agatha Christie Ian Flemming J.K. Rowling Stephen King Oscar Wilde 20 73 14 14 54

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Database Management

Authors: Heikki Topi, Jeffrey A Hoffer, Ramesh Venkataraman

13th Edition

0134773659, 978-0134773650

More Books

Students also viewed these Databases questions