Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Why is my C program not being functioning properly. Agatha christie input is not supposed to be a successful input, it should show an error.

Why is my C program not being functioning properly. Agatha christie input is not supposed to be a successful input, it should show an error. Please modify my code so the (else if) statement works properly. Make sure my program source code outputs the Expected Ouput.

Changing the if else to...... else if (commaAfterInt == 1) { .......will not work

Please spend time on this, I need proper output!

Specifically the input below needs to initiate the (else if) statement properly:

Number of Novels Authored

Author name

Number of novels

Agatha Christie, seventythree

Agatha Christie, seventythree

Agatha Christie, seventythree

Agatha Christie, 73

-1

-------------------------------------------------------------------------------

else if (commaAfterInt == 1) { printf("Error: Comma not followed by an integer "); }

-------------------------------------------------------------------------------

Expected output of program containing input data of Agatha Christie: Image beow:

image text in transcribed

My programs actual output that is incorrect: Image below:

image text in transcribed

Source code to my program:

#include

#include

#include

#include

int main(void) {

char title[50];

char col1[50];

char col2[50];

int point[50];

char names[50][50];

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

fgets (title, 50, stdin);

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

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

fgets (col1, 50, stdin);

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

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

fgets (col2, 50, stdin);

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

col1[strlen(col1) - 1] = '\0';

col2[strlen(col2) - 1] = '\0';

int count = 0;

char dataPoint[50];

while (count

{

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

fgets (dataPoint, 50, stdin);

if (atoi(dataPoint) == -1) {

//exit(0);

break;

}

int commas = 0;

int i = 0;

int intFound = 0;

char integerValue[5] = "";

int commaAfterInt = 0;

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

{

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

{

commas++;

if (intFound == 1) {

commaAfterInt = 1;

}

}

else if (commas == 0) {

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

names[count][i+1]='\0'; //this will end current string otherwise current will overlape to previous in case

} //of invalid string as it was happening in your 2nd input case

else if (isdigit(dataPoint[i]))

{

intFound = 1;

int j = 0;

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

integerValue[j] = dataPoint[i];

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

}

i++;

}

if (commas == 0) {

printf("Error: 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);

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

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

count++;

}

}

// Displaying the formatted table

printf(" *****FORMATTED TABLE***** ");

printf("%33s", title);

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

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

int i = 0;

while (i

{

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

printf(" ");

i++;

}

// Displaying the formatted histogram

printf(" FORMATTED HISTOGRAM ");

i = 0;

while (i

{

printf(" %20s ", names[i]);

int j = 0;

while (j

{

printf("*");

j++;

}

i++;

}

return 0;

}

Enter a title for the data You entered: Number of Novels Au Enter the column 1 header You entered: Author name Enter the column 2 header You entered: Number of novels Expected output starts with Enter a data point (-1 to stop i rror: Comma not follo wed by an Enter a data point (-1 to stop i Error: Comma not followed by an Enter a data point (-1 to stop i rror Comma not followed by an Enter a data point (-1 to stop i Data string: Agatha Christie Data integer: 73

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

Medical Image Databases

Authors: Stephen T.C. Wong

1st Edition

1461375398, 978-1461375395

More Books

Students also viewed these Databases questions

Question

Are there fewer of them to do it (i.e., have you been downsizing)?

Answered: 1 week ago