Question: What is my issue in this C program, the probkem is with spacing. Source code: #include #include #include #include int main(void) { char title[50]; char
What is my issue in this C program, the probkem is with spacing.
Source code:
#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("Too many commas in input. ");
}
else if (commaAfterInt == 1) {
printf("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;
}
Here is the problem I am having:

4: Compare output Output differs. See highlights below. Special character legend Number of Novels Authored Author name Number of novels Input Ernest Hemingway 9 9 Ernest Hemingway Ernest Hemingway, 9 Enter a title for the data: You entered: Number of Novels Authored Enter the column 1 header: You entere d: Author name Enter the column 2 header: You entered: Number of novels Your output starts with Enter a data point (-1 to stop input): Error: No comma in string Enter a data point (-1 to stop input): Error: No comma in string Enter a data point (-1 to stop input): Data string: Ernest Hemingway Data integer: 9* Enter a title for the data: You entered: Number of Novels Authored Enter the column 1 header: You entered:Author name Enter the column 2 header: You entered: Number of novels Expected output Enter a data point (-1 to stop input): starts wit Error: No comma in string Enter a data point (-1 to stop input): Error: No comma in string. Enter a data point (-1 to stop input): Data string: Ernest Hemingway Data integer: 9
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
