Reveiw my code not sure on error. This is C. Code as follows #include<stdio.h> #
ID: 3845503 • Letter: R
Question
Reveiw my code not sure on error. This is C.
Code as follows
#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];
printf("Enter a title for the data: ");
scanf("%s",title);
printf("Your entered: %s ",title);
printf("Enter the column 1 header ");
scanf("%s",col1);
printf("Youe entered: %s ",col1);
printf("Enter the column 2 header ");
scanf("%s",col2);
printf("Youe entered: %s ",col2);
int count = 0;
char dataPoint[50];
while(count <50){
printf("Enter a data point(-1 to stop input): ");
scanf("%s",dataPoint);
if(atoi(dataPoint)==-1){
break;
}
int commas = 0;
int i=0;
int intFound = 0;
char integerValue[5] = "";
int commaAfterInt = 0;
while(dataPoint[i] != ''){
//printf("%c------> ",dataPoint[i]);
if(dataPoint[i] == ','){
commas++;
if(intFound == 1){
commaAfterInt = 1;
}
}
else if((dataPoint[i]>='a' && dataPoint[i] <='z') || (dataPoint[i] >='A' && dataPoint[i]<='Z')){
names[count][i] = dataPoint[i];
}
else if(isdigit(dataPoint[i])){
intFound = 1;
int j=0;
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("Too many commas in input. ");
}
else if(commaAfterInt == 1){
printf("Comma not followed by an integer ");
}
else{
point[count++] = atoi(integerValue);
}
}
printf(" %s ",title);
printf(" %s ",col1);
printf("%s ",col2);
int i=0;
printf("Count:%d ",count);
while(i<count){
printf("%s %d ",names[i],point[i]);
i++;
}
i=0;
while(i<count){
printf("%s ",names[i]);
int j = 0;
while(j<point[i]){
printf("*");
j++;
}
printf(" ");
i++;
}
return 0;
}
Errors;
main.c: In function 'main': main.c:30:11: warning: implicit declaration of function 'atoi' [-Wimplicit-function-declaration] if(atoi(dataPoint)==-1){ ^~~~
main.c:49:20: warning: implicit declaration of function 'isdigit' [-Wimplicit-function-declaration] else if(isdigit(dataPoint[i])){ ^~~~~~~
The question,
(1) Prompt the user for a title for data. Output the title. (1 pt)
Ex:
(2) Prompt the user for the headers of two columns of a table. Output the column headers. (1 pt)
Ex:
(3) Prompt the user for data points. Data points must be in this format: string, int. Store the information before the comma into a string variable and the information after the comma into an integer. The user will enter -1 when they have finished entering data points. Output the data points. Store the string components of the data points in an array of strings. Store the integer components of the data points in an array of integers. (4 pts)
Ex:
(4) Perform error checking for the data point entries. If any of the following errors occurs, output the appropriate error message and prompt again for a valid data point.
If entry has no comma
Output: Error: No comma in string. (1 pt)
If entry has more than one comma
Output: Error: Too many commas in input. (1 pt)
If entry after the comma is not an integer
Output: Error: Comma not followed by an integer. (2 pts)
Ex:
(5) Output the information in a formatted table. The title is right justified with a width of 33. Column 1 has a width of 20. Column 2 has a width of 23. (3 pts)
Ex:
(6) Output the information as a formatted histogram. Each name is right justified with a width of 20. (4 pts)
Ex:
Explanation / Answer
I ran your code in my compiler and it didnot presented any error or warning as shown by you.
The logic of your code is correct but the main problem i found was that it did not had proper formatting as asked in the question part 5,6.
Instead of using for tab space you could have right aligned the columns .eg %23d would reserve 23 places for the int variable and right aligned it.
The question mentioned string,int format for datapoints, You considered only alphabets in string which might be correct but you could have considered all of them.
I am providing my version of your 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];
printf("Enter a title for the data: ");
scanf("%s",title);
printf("You entered: %s ",title);
printf("Enter the column 1 header ");
scanf("%s",col1);
printf("You entered: %s ",col1);
printf("Enter the column 2 header ");
scanf("%s",col2);
printf("You entered: %s ",col2);
int count = 0;
char dataPoint[50];
while(count <50){
printf("Enter a data point(-1 to stop input): ");
scanf("%s",dataPoint);
if(atoi(dataPoint)==-1){
break;
}
int commas = 0;
int i=0;
int intFound = 0;
char integerValue[5] = "";
int commaAfterInt = 0;
while(dataPoint[i] != ''){
//printf("%c------> ",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;
int j=0;
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("Too many commas in input. ");
}
else if(commaAfterInt == 1){
printf("Comma not followed by an integer ");
}
else{
point[count++] = atoi(integerValue);
}
}
printf(" FORMATTED TABLE ");
printf("%33s ",title);
printf("%23s",col1);
printf(" | %20s ----------------------------------------------------- ",col2);
int i=0;
// printf("Count:%d ",count);
while(i<count){
printf("%23s | %20d ",names[i],point[i]);
i++;
}
printf(" FORMATTED HISTOGRAM ");
i=0;
while(i<count){
printf(" %28s",names[i]);
int j = 0;
while(j<point[i]){
printf("*");
j++;
}
printf(" ");
i++;
}
return 0;
}
I have changed the statement "else if((dataPoint[i]>='a' && dataPoint[i] <='z') || (dataPoint[i] >='A' && dataPoint[i]<='Z'))" with "else if(commas==0)"
and I have changed the formatting.
Hope you find it helpful
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.