Need some help with C programing. I can\'t really seem to figure out this inpuit
ID: 3815200 • Letter: N
Question
Need some help with C programing.
I can't really seem to figure out this inpuit errror that i am running into as my "Error: Comma not followed by an integer." to work correctly.
Here is the error
Here is my code.
#include <stdio.h>
#include <string.h>
int main(void) {
char title[50];
char col1[50];
char col2[50];
int point[50];
char names[50][50] = {
''
};
int count = 0;
char dataPoint[50];
int commas = 0;
int i = 0, j = 0, k = 0, p = 0, len;
int intFound = 0;
char integerValue[5] = "";
int commaAfterInt = 0;
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);
len = strlen(col1);
if (len > 0 && col1[len - 1] == ' ') {
col1[--len] = '';
}
printf("Enter the column 2 header: ");
fgets(col2, 50, stdin);
printf("You entered: %s ", col2);
while (count < 50) {
printf("Enter a data point (-1 to stop input): ");
fgets(dataPoint, 50, stdin);
if (atoi(dataPoint) == -1) {
break;
}
commas = 0;
i = 0;
intFound = 0;
strcpy(integerValue, "");
commaAfterInt = 0;
while (dataPoint[i] != '') {
if (dataPoint[i] == ',') {
commas++;
if (intFound == 1) {
commaAfterInt = 1;
}
} else if (commas == 0) {
names[count][i] = dataPoint[i];
} else if (isdigit(dataPoint[i])) {
intFound = 1;
for (j = 0; integerValue[j] != ''; j++);
integerValue[j] = dataPoint[i];
integerValue[j + 1] = '';
}
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);
k = 0;
p = 0;
strcpy(names[count], "");
while (dataPoint[p] != ',') {
names[count][k] = dataPoint[p];
k++;
p++;
}
names[count][k] = '';
names[count][k + 1] = '';
printf("Data string: %s ", names[count]);
printf("Data integer: %d ", point[count - 1]);
}
}
printf(" FORMATTED TABLE ");
printf(" %40s ", title);
printf(" %-20s | %20s ", col1, col2);
printf(" ----------------------------------------------------- ");
i = 0;
while (i < count) {
printf(" %s | %10d ", names[i], point[i]);
i++;
}
printf(" FORMATTED HISTOGRAM ");
i = 0;
while (i < count) {
printf(" %s ", names[i]);
j = 0;
while (j < point[i]) {
printf("*");
j++;
}
printf(" ");
i++;
}
return 0;
}
Explanation / Answer
#include <stdio.h>
#include <string.h>
int main(void) {
char title[50];
char col1[50];
char col2[50];
int point[50];
char names[50][50] = {
''
};
int count = 0;
char dataPoint[50];
int commas = 0;
int i = 0, j = 0, k = 0, p = 0, len;
int intFound = 0;
char integerValue[5] = "";
int commaAfterInt = 0;
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);
len = strlen(col1);
if (len > 0 && col1[len - 1] == ' ') {
col1[--len] = '';
}
printf("Enter the column 2 header: ");
fgets(col2, 50, stdin);
printf("You entered: %s ", col2);
while (count < 50) {
printf("Enter a data point (-1 to stop input): ");
fgets(dataPoint, 50, stdin);
if (atoi(dataPoint) == -1) {
break;
}
commas = 0;
i = 0;
intFound = 0;
strcpy(integerValue, "");
commaAfterInt = 0;
while (dataPoint[i] != '') {
if (dataPoint[i] == ',') {
commas++;
// printf("hello coma");
if (intFound == 1) {
// printf("hello coma1");
commaAfterInt = 1;
}
}//here i have changed the order of else if codition
//because in the upper if, you are changing commaAfterInt variable
//so first you have to search for int and change int found
else if (isdigit(dataPoint[i])) {
//modified
if(commaAfterInt==0)
intFound = 1;
else
{intFound = 0;
commaAfterInt = 0;
}
for (j = 0; integerValue[j] != ''; j++);
integerValue[j] = dataPoint[i];
integerValue[j + 1] = '';
}
else if (commas == 0) {
//printf("hello coma2");
names[count][i] = dataPoint[i];
}
i++;
}
//printf("%d %d ",commas,commaAfterInt);
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);
k = 0;
p = 0;
strcpy(names[count], "");
while (dataPoint[p] != ',') {
names[count][k] = dataPoint[p];
k++;
p++;
}
names[count][k] = '';
names[count][k + 1] = '';
printf("Data string: %s ", names[count]);
printf("Data integer: %d ", point[count - 1]);
}
}
printf(" FORMATTED TABLE ");
printf(" %40s ", title);
printf(" %-20s | %20s ", col1, col2);
printf(" ----------------------------------------------------- ");
i = 0;
while (i < count) {
printf(" %s | %10d ", names[i], point[i]);
i++;
}
printf(" FORMATTED HISTOGRAM ");
i = 0;
while (i < count) {
printf(" %s ", names[i]);
j = 0;
while (j < point[i]) {
printf("*");
j++;
}
printf(" ");
i++;
}
return 0;
}
output:-
Enter a title for the data:
Number of Novels Authored
You entered: Number of Novels Authored
Enter the column 1 header:
Author name
You entered: Author name
Enter the column 2 header:
Number of novels
You entered: Number of novels
Enter a data point (-1 to stop input):
surya,vamso
Data string: surya
Data integer: 0
Enter a data point (-1 to stop input):
2,2
Data string: 2
Data integer: 22
Enter a data point (-1 to stop input):
2,
Error: Comma not followed by an integer
Enter a data point (-1 to stop input):
surya
Error: No comma in string.
Enter a data point (-1 to stop input):
2,2,3
Error: Too many commas in input.
Enter a data point (-1 to stop input):
-1
FORMATTED TABLE
Number of Novels Authored
Author name | Number of novels
-----------------------------------------------------
surya | 0
surya | 22
FORMATTED HISTOGRAM
surya
surya **********************
Process exited normally.
Press any key to continue . . .
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.