Result is not correct--C Programming - c

Good day to everyone.
I have a question. How to fix my incorrect result as shown here, on my Point Obtained part, Round 1, the result is not correct? The answer is should be 34 for player 1, 45 for player 2, 50 for player 3.
Also, I also know that fflush(stdin) is not suitable to be implemented in coding. But when I remove fflush(stdin), the fgets is skipping my input. Can anyone suggest me how to fix this?
This is my code:
#include<stdio.h>
int main(void)
{
int round;
int player;
char name1[30];
char name2[30];
char name3[30];
int total1;
int total2;
int total3;
int times;
int point[3];
total1=0;
total2=0;
total3=0;
printf("======League of Legends======\n");
printf("How many round?:");
scanf("%d", &round);
printf("How many player:");
scanf("%d", &player);
fflush(stdin);
printf("Name player 1:");
fgets(name1, 30, stdin);
printf("Name player 2:");
fgets(name2, 30, stdin);
printf("Name player 3:");
fgets(name3, 30, stdin);
for(times=1; times<=round; times++)
{
printf("## Round %d ##\n", times);
printf("Point for player 1(%s):",name1);
scanf("%d", &point[0]);
printf("Point for player 2(%s):", name2);
scanf("%d", &point[1]);
printf("Point for player 3(%s):", name3);
scanf("%d", &point[2]);
total1=total1+point[0];
total2=total2+point[1];
total3=total3+point[2];
}
for(times=1; times<=round; times++)
{
printf("******Point obtained******\n");
printf("## Round %d ##\n", times);
printf("Point for player 1(%s): %d\n", name1, point[0]);
printf("Point for player 2(%s): %d\n", name2, point[1]);
printf("Point for player 3(%s): %d\n", name3, point[2]);
}
printf("++++++TOTAL POINT++++++\n");
printf("Total point player 1(%s):%d\n", name1, total1);
printf("Total point player 2(%s):%d\n", name2, total2);
printf("Total point player 3(%s):%d\n", name3, total3);
return 0;
}

The reason of getting wrong output is that the array point is overwritten by information of later round. You have to store the data separately.
To fix fgets() problem, I will read all lines via fgets() to have them read newline characters.
possible fix:
#include<stdio.h>
#include<stdlib.h>
int readInt(void) {
char buffer[512];
int ret;
fgets(buffer, sizeof(buffer), stdin);
sscanf(buffer, "%d", &ret);
return ret;
}
void readString(char* str, size_t str_size) {
char* p;
fgets(str, str_size, stdin);
for (p=str; *p!='\0' && *p!='\n'; p++);
*p='\0';
}
int main(void)
{
int round;
int player;
char name1[30];
char name2[30];
char name3[30];
int total1;
int total2;
int total3;
int times;
int (*point)[3];
total1=0;
total2=0;
total3=0;
printf("======League of Legends======\n");
printf("How many round?:");
round=readInt();
printf("How many player:");
player=readInt();
printf("Name player 1:");
readString(name1, sizeof(name1));
printf("Name player 2:");
readString(name2, sizeof(name2));
printf("Name player 3:");
readString(name3, sizeof(name3));
point=malloc(sizeof(*point)*round);
for(times=1; times<=round; times++)
{
printf("## Round %d ##\n", times);
printf("Point for player 1(%s):",name1);
scanf("%d", &point[times-1][0]);
printf("Point for player 2(%s):", name2);
scanf("%d", &point[times-1][1]);
printf("Point for player 3(%s):", name3);
scanf("%d", &point[times-1][2]);
total1=total1+point[times-1][0];
total2=total2+point[times-1][1];
total3=total3+point[times-1][2];
}
for(times=1; times<=round; times++)
{
printf("******Point obtained******\n");
printf("## Round %d ##\n", times);
printf("Point for player 1(%s): %d\n", name1, point[times-1][0]);
printf("Point for player 2(%s): %d\n", name2, point[times-1][1]);
printf("Point for player 3(%s): %d\n", name3, point[times-1][2]);
}
free(point);
printf("++++++TOTAL POINT++++++\n");
printf("Total point player 1(%s):%d\n", name1, total1);
printf("Total point player 2(%s):%d\n", name2, total2);
printf("Total point player 3(%s):%d\n", name3, total3);
return 0;
}

Related

C struct glitch? (I am new to programing in C)

I am learning how to create struct's and I am stuck on a program I made. Everything works fine until I try to input "2". When the program prints the symbol it's supposed to be "He" but prints "HeHelium" instead. I can't figure out what's wrong and why it's printing he.symbol and he.name all in one line. Link to image below.
#include <stdio.h>
#include <stdlib.h>
struct Element {
int num;
double mass;
char symbol[2];
char name[20];
};
int main()
{
struct Element h;
h.num = 1;
h.mass = 1.008;
strcpy(h.symbol, "H");
strcpy(h.name, "Hydrogen");
struct Element he;
he.num = 2;
he.mass = 4.0026;
strcpy(he.symbol, "He");
strcpy(he.name, "Helium");
int number;
printf("Please enter the number of the element you want info on. \n");
scanf("%d", &number);
if (number == 1 /*&& !(number > 1)*/) {
printf("Atomic Number: %d \n", h.num);
printf("Atomic Mass: %.3f \n", h.mass);
printf("Atomic Symbol: %s \n", h.symbol);
printf("Atomic Name: %s \n", h.name);
} else if (number == 2) {
printf("Atomic Number: %d \n", he.num);
printf("Atomic Mass: %.3f \n", he.mass);
printf("Atomic Symbol: %s \n", he.symbol);
printf("Atomic Name: %s \n", he.name);
} else {
printf("Invalid number! \n");
printf("Or that element hasn't been added to the date base yet. \n");
printf("Try back later \n");
}
return 0;
}
When I input "2":
You have assigned Element.symbol with only 2byte which can only store string with only one character as the other character will be used for null character. You should write "symbol[3]" as the symbol of elements in periodic table are no longer than 2 characters. Also, your code can become quite messy if you want to assign values for all 118 elements. You can write your code like below:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Elements {
int num;
double mass;
char symbol[3];
char name[20];
};
void print(struct Elements element)
{
printf("Atomic Number: %d \n", element.num);
printf("Atomic Mass: %.3f \n", element.mass);
printf("Atomic Symbol: %s \n", element.symbol);
printf("Atomic Name: %s \n", element.name);
}
void assignElement(struct Elements *givenElement, int num, float mass, char symbol[3],
char name[20])
{
givenElement->num=num;
givenElement->mass=mass;
strcpy(givenElement->symbol,symbol);
strcpy(givenElement->name,name);
}
int main()
{
struct Elements element[119];
assignElement(&element[1], 1, 1.008, "H", "Hydrogen");
assignElement(&element[2], 2, 4.06, "He", "Helium");
assignElement(&element[3], 3, 6.09, "Li", "Lithium");
int number;
printf("Please enter the number of the element you want info on. \n");
scanf("%d", &number);
if (number < 119 && number > 0) {
print(element[number]);
}
else {
printf("Invalid number! \n");
printf("Or that element hasn't been added to the date base yet. \n");
printf("Try back later \n");
}
return 0;
}

Structures in C

I am trying to make a seat reservation program using structures and functions (as part of my effort to try to learn C).
In the following code I have some problems in:
-Lines 82,143 ( seat reservation check is not working consistently) (Solved)
I changed from temp to temp - 1 the code in these lines and everything is normal..
-Lines 109 and 122 where I want to compare two phone-numbers that are saved as unsigned short arrays of 10 elements. (Solved)
#include <stdio.h>
#include <string.h>
int i,j,numberofseats,temp;
char selection;
typedef struct
{
char fullname[40];
unsigned short phonenr[10];
unsigned int seatnr;
}PASSENGERS;
void changeData(PASSENGERS *target){
char firstname[20];
char lastname[20];
char phone[11];
printf("Enter passenger's first name:");
scanf("%s",firstname);
printf("Enter passenger's last name:");
scanf("%s",lastname);
strcpy(target->fullname,firstname);
strcat (target->fullname, " ");
strcat(target->fullname,lastname);
printf("Enter passenger's phone Nr:");
for (i=0;i<10;i++) scanf("%hu",&(target->phonenr[i]));
}
void cancelSeat(PASSENGERS *target){
strcpy(target->fullname,"\0");
for (i=0;i<10;i++)
target->phonenr[i]=0;
printf("Seat Nr %d is now free",temp);
}
int main(void)
{
numberofseats=53;
PASSENGERS passenger[numberofseats];
for (j=0;j<numberofseats;j++)
{passenger[j].seatnr=j+1;
strcpy(passenger[j].fullname,"\0");
}
do{
printf("\n\nNeo Sistima katagrafis thesewn leoforeiou\n");
printf("Please make a selection:\n\n");
printf("0. Exit\n");
printf("1. Empty Seats \n");
printf("2. Book Specific Seat \n");
printf("3. Advanced Search of booked Seats\n");
printf("4. Cancel Seat Booking\n");
printf("5. Show List of booked Seats\n");
scanf(" %c",&selection);
if (selection=='1')
{int freeseats = 0;
for (j=0; j<numberofseats; j++)
{
strcmp(passenger[j].fullname,"\0")==0 ? freeseats = freeseats + 1 : freeseats ;}
printf ("There are %d free seats in this bus \n", freeseats);
printf("Seats that are available are:\n");
for (j=0; j<numberofseats; j++)
{if (strcmp(passenger[j].fullname,"\0")==0)
printf ("%hd\n", passenger[j].seatnr);
}
freeseats = 0;
}
else if (selection=='2')
{
printf("Please give seat nr (between 1 and %d) that you want to book:\n", numberofseats);
scanf("%d",&temp);
if (temp >numberofseats || temp <= 0)
{printf("Error: Seat nr should be between 1 and %d", numberofseats);}
else if (strcmp(passenger[temp].fullname,"\0")!=0)
printf("Error: Seat is already booked");
else
changeData(&passenger[temp-1]);
}
else if (selection=='3')
{
char tempsel,tmpfirst[20],tmplast[20];
unsigned short tempphone[10];
int counter, checkphone;
unsigned int tempseat;
printf("Do you want to search with Name (1) or Phone Nr (2)?\n");
scanf(" %c",&tempsel);
if (tempsel == '1')
{ printf("Enter passenger's first name:");
scanf("%s",tmpfirst);
printf("Enter passenger's last name:");
scanf("%s",tmplast);
strcat (tmpfirst, " ");
strcat(tmpfirst,tmplast);
for (j=0;j<numberofseats;j++)
if (strcmp(passenger[j].fullname,tmpfirst)==0)
printf ("Passenger %s has seat nr #: %hd\n",tmpfirst,passenger[j].seatnr);
}
else if (tempsel == '2')
{ checkphone=0;
printf("Enter passenger's phonenr:");
for (i=0;i<10;i++)
scanf("%hu",&tempphone[i]);
for (j=0;j<numberofseats;j++)
{
counter=0;
for(i=0;i<10;i++)
{
if (passenger[j].phonenr[i]==tempphone[i])
counter=counter+1;
if (counter ==10)
{checkphone=1;
tempseat=passenger[j].seatnr;
}}
}
if (checkphone==1)
{printf ("Passenger has seat #: %hd\n",tempseat);
checkphone=0;}
}
}
else if (selection=='4')
{
printf("Please give seat nr (between 1 and %d) that you want to cancel booking:\n", numberofseats);
scanf("%d",&temp);
if (temp >numberofseats || temp <= 0)
{printf("Error: Seat nr should be between 1 and %d", numberofseats);}
else if (strcmp(passenger[temp].fullname,"\0")==0)
printf("Error: Seat is already free");
else
cancelSeat(&passenger[temp-1]);
}
} while (selection!='0');
}
you have two problems :-
passed integer in strcpy and strcmp.
to print integer data you used %s (for temp)
Do modify your code like that :-
void cancelSeat(PASSENGERS *target){
strcpy(target->fullname,"\0");
for (i=0;i<10;i++)
// strcpy(target->phonenr[i],"\0");
printf("Seat Nr %d is now free",temp);
}
and change you case where you comparing telephone number
if (strcmp(passenger[j].phonenr[i],tempphone[i])==0) ==> if (passenger[j].phonenr[i]==tempphone[i])

C program how to print in table format alignment

The input is retrieve from text file. Which contain information of
product ID
product Name
Product Quantity
product Price
1 RAYBAN 1 450.000000
900 KEYBOARD 100 290.000000
78 MINERALWATER 123 345.000000
2 RAYBAN 2 450.000000
After printing the output through command prompt. It was not align with the 1st item. How to make it align with the title of table. As you can see the input of line 1 and 4 almost the same.
Here is the output.
Here is the full code. With gotoxy function. The display function is on
int displayProduct()
There is a line of code for table titles and also the printf from TXT File.
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>
void gotoxy(int column, int line);
int main();
int addProduct();
int displayProduct(); //prototype
struct product {
int quantity, reorder, i, id;
char name[20];
float price;
};
COORD coord = { 0, 0 };
void gotoxy(int x, int y) {
coord.X = x; coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
int main() {
int choice;
gotoxy(17, 5);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(17, 20);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(22, 8);
printf("1. Add Product\n\n");
gotoxy(22, 10);
printf("2. Display Product\n\n");
gotoxy(22, 12);
printf("3. Search Product\n\n");
gotoxy(22, 14);
printf("4. Reorder Level of Product\n\n");
gotoxy(22, 16);
printf("5. Update Product\n\n");
gotoxy(22, 18);
printf("6. Exit\n\n");
gotoxy(22, 22);
printf("Please Enter Your Choice : ");
scanf(" %d", &choice);
switch (choice) {
case 1:
addProduct();
break;
case 2:
displayProduct();
break;
case 3:
searchProduct();
break;
case 4:
reorderProduct();
break;
case 5:
updateProduct();
break;
case 6:
break;
default:
system("cls");
main();
}
return (0);
}
/*MENU CODE ENDS !*/
int addProduct() {
FILE *fp;
int i = 0;
struct product a;
system("cls");
fp = fopen("inventory.txt", "a+t");
char checker;
do {
system("cls");
gotoxy(17, 5);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(17, 20);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(22, 8);
printf("Enter product ID : ");
scanf(" %d", &a.id);
gotoxy(22, 10);
printf("Enter product name : ");
scanf(" %s", a.name);
gotoxy(22, 12);
printf("Enter product quantity : ");
scanf(" %d", &a.quantity);
gotoxy(22, 14);
printf("Enter product price : ");
scanf(" %f", &a.price);
gotoxy(22, 17);
fprintf(fp, "%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price); //SAVE TO TXT FILE LINE !
printf("Record saved!\n\n");
fclose(fp);
gotoxy(22, 22);
printf("Do you want to enter new product? Y / N : ");
scanf(" %c", &checker);
checker = toupper(checker);
i++;
system("cls");
} while (checker=='Y');
if (checker == 'N') {
main();
} else {
do {
system("cls");
gotoxy(17, 5);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(17, 20);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(18, 8);
printf(">>> Wrong Input! Please Enter Y Or N Only! <<<");
gotoxy(19, 12);
printf("Do You Want To Enter New Product? Y / N : ");
scanf(" %c", &checker);
checker = toupper(checker);
} while (checker != 'Y' && checker != 'N');
if (checker == 'Y'){
addProduct();
}
if (checker == 'N') {
system("cls");
main();
}
}
return(0);
}
/*ADD PRODUCT LINE ENDS !*/
int displayProduct() {
FILE *fp;
struct product a;
char true;
system("cls");
fp = fopen("inventory.txt", "r");
gotoxy(17, 5);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(5, 6);
printf("======================================================================");
gotoxy(5, 7);
printf("Product ID\t\t Product Name\t\t Quantity\t Unit Price\n"); //TABLE TITLES !
gotoxy(5, 8);
printf("======================================================================");
gotoxy(5,10);
while (fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price) == 4) {
printf("%d\t\t\t %s\t\t\t %d\t\t %.2f\n\n", a.id, a.name, a.quantity, a.price); //PRINT FROM TXT FILE TO COMMAND PROMPT.
}
fclose(fp);
printf("\t\t \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
printf("\nPress any key to return to Main Menu.");
getch();
int main();
return (0);
}
Updated one, changes made :
gotoxy(5,10);
while(fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price)==4)
{
printf("%-10d\t\t %-12s\t\t %8d\t %8.2f\n\n", a.id, a.name, a.quantity, a.price);
}
fclose(fp);
The code to print the inventory should use the length specifier in printf format like this:
gotoxy(0, 10);
while (fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price) == 4) {
printf(" %-10d\t\t %-12s\t\t %8d\t %8.2f\n\n", a.id, a.name, a.quantity, a.price);
}
Some notes regarding the code:
it is very bad style to call main() recursively. Use a loop instead.
write a function that prints the header instead of duplicating the code multiple times.
the statement int main(); at the end of displayProduct() is a local declaration for function main, it does not generate a call.
your menu doesn't go back to the main menu after pressing any key,
there you have to use main(); instead ogf int main();

C Program GOTOXY on second screen

The first screen, gotoxy code is working. But in second screen. Nothing happens, like, it does not read the gotoxy code at all. Please enlighten me about the problem.
Here is the 1st screen :
Here is the 2nd screen :
Here is the code. I would love to learn more about gotoxy.
Thank you in advance.
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>
void gotoxy( int column, int line );
int main();
int addProduct();
struct product
{
int quantity, reorder, i, id;
char name[20];
float price;
};
COORD coord = {0, 0};
void gotoxy (int x, int y)
{
coord.X = x; coord.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}
int main()
{
int choice;
gotoxy(17,5);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(17,18);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(22,8);
printf("1. Add Product\n\n");
gotoxy(22,10);
printf("2. Display Product\n\n");
gotoxy(22,12);
printf("3. Search Product\n\n");
gotoxy(22,14);
printf("4. Reorder Level of Product\n\n");
gotoxy(22,16);
printf("5. Update Product\n\n");
gotoxy(22,20);
printf("Please Enter Your Choice : ");
scanf(" %d", &choice);
switch(choice)
{
case 1 : addProduct();
break;
case 2 : displayProduct();
break;
case 3 : searchProduct();
break;
case 4 : reorderProduct();
break;
case 5 : updateProduct();
break;
default : printf("Wrong input. Please try again.");
system("cls");
main();
}
return (0);
}
int addProduct()
{
FILE * fp;
int i=0;
struct product a;
system("cls");
char checker;
gotoxy(17,5);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2 SYZ INVENTORY PROGRAM \xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
gotoxy(17,18);
printf("\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2\xB2");
do
{
fp = fopen("inventory.txt","a+t");
system("cls");
printf("Enter product ID : ");
scanf(" %d", &a.id);
printf("Enter product name : ");
scanf(" %s", a.name);
printf("Enter product quantity : ");
scanf(" %d", &a.quantity);
printf("Enter product price : ");
scanf(" %f", &a.price);
fprintf(fp, "%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
printf("Record saved!\n\n");
fclose(fp);
printf("Do you want to enter new product? Y / N : ");
scanf(" %c", &checker);
checker = toupper(checker);
i++;
system("cls");
}
while(checker=='Y');
if(checker == 'N')
{
main();
}
else
{
do{
printf("Do you want to enter new product? Y / N : ");
scanf(" %c", &checker);
checker = toupper(checker);
}while(checker != 'Y' && checker != 'N');
if(checker == 'Y'){addProduct();}
if(checker == 'N'){
system("cls");
main();}
}
return(0);
}

Running into errors trying to read Text File

Okay so I'm building a program for work to track volume each day and also split the volume up to how much each different location receives. There are 8 lines in the building. The Text file consists of the day's date, the total volume for the building and the volume for each location (PEN).
Im running into two problems right now both are segregated to my readDay function. where result = fscanf(pi->Date, 40, fp);
issues are
1. Incompatible integer to pointer conversion passing 'int' to parameter of type 'const char*'
2. Incompatible pointer types passing 'char[40]' to parameter of type 'FILE*'(aka 'struct_sFILE*')
Can anyone explain to me what these issues mean. Or if you need any more info in order to help me feel free to ask.
#include <stdio.h>
#include <stdlib.h>
struct Day {
char Date[40];
int Total;
int Pen1;
int Pen2;
int Pen3;
int Pen4;
int Pen5;
int Pen6;
int Pen7;
int Pen8;
};
const char fileName [] = "UPS.txt";
void StartRecord(void);
void printDay(struct Day* pi);
void writeDay(FILE *fp, struct Day *pi);
int readDay(FILE *fp, struct Day *pi);
void readFile(void);
void addDay(void);
int main(int argc, const char * argv[]) {
StartRecord();
int choice;
printf("Enter 1 to read this file or enter 2 to add to it.");
scanf("%d", &choice);
switch (choice) {
case 1: readFile();
break;
default: printf("Not a valid choice\n");
break;
}
getchar();
getchar();
}
void StartRecord(void) {
int count;
int i;
struct Day aDay;
FILE *fp;
fp = fopen(fileName, "w");
if (fp != NULL) {
printf("Enter volume for how many days? ");
scanf("%d", &count);
for (i = 0; i < count; ++i) {
printf("What is today's date? ");
gets(aDay.Date);
printf("What was the total volume for the building for the day?");
scanf("%d", &aDay.Total);
printf("What was the volume for Pen 1? ");
scanf("%d", &aDay.Pen1);
printf("What was the volume for Pen 2? ");
scanf("%d", &aDay.Pen2);
printf("What was the volume for Pen 3? ");
scanf("%d", &aDay.Pen3);
printf("What was the volume for Pen 4? ");
scanf("%d", &aDay.Pen4);
printf("What was the volume for Pen 5? ");
scanf("%d", &aDay.Pen5);
printf("What was the volume for Pen 6? ");
scanf("%d", &aDay.Pen6);
printf("What was the volume for Pen 7? ");
scanf("%d", &aDay.Pen7);
printf("What was the volume for Pen 8? ");
scanf("%d", &aDay.Pen8);
getchar();
writeDay(fp, &aDay);
}
fclose(fp);
}else {
printf("\nError opening file.\n");
}
}
void readFile(void) {
FILE *fp;
struct Day aDay;
fp = fopen(fileName, "r");
if (fp != NULL) {
while (readDay(fp, &aDay)) {
printDay(&aDay);
}
fclose(fp);
} else {
printf("\nError opening File!\n");
}
}
void writeDay(FILE *fp, struct Day *pi) {
fprintf(fp, "%s\n", pi->Date);
fprintf(fp, "%d\n", pi->Total);
fprintf(fp, "%d\n", pi->Pen1);
fprintf(fp, "%d\n", pi->Pen2);
fprintf(fp, "%d\n", pi->Pen3);
fprintf(fp, "%d\n", pi->Pen4);
fprintf(fp, "%d\n", pi->Pen5);
fprintf(fp, "%d\n", pi->Pen6);
fprintf(fp, "%d\n", pi->Pen7);
fprintf(fp, "%d\n", pi->Pen8);
}
int readDay(FILE *fp, struct Day *pi) {
int result;
fgetc(fp); // read the endline
result = fscanf(pi->Date, 40, fp);
if (result == EOF) {
return 0;
}
fscanf(fp, "%d", &pi->Total);
fscanf(fp, "%d", &pi->Pen1);
return 1;
}
void PrintDay(struct Day *pi) {
printf("Records for the day of: %s\n", pi->Date);
printf("Total Volume: %d", pi->Total);
printf("Volume for Pen 1: %d", pi->Pen1);
printf("Volume for Pen 2: %d", pi->Pen2);
printf("Volume for Pen 3: %d", pi->Pen3);
printf("Volume for Pen 4: %d", pi->Pen4);
printf("Volume for Pen 5: %d", pi->Pen5);
printf("Volume for Pen 6: %d", pi->Pen6);
printf("Volume for Pen 7: %d", pi->Pen7);
printf("Volume for Pen 8: %d", pi->Pen8);
printf("\n\n");
}
In one sense, your arguments are the wrong way round. This:
fscanf(pi->Date, 40, fp);
should be:
fscanf(fp, pi->Date, 40);
just like you already have it a few lines further down.
In another sense, what you're trying to do is very strange - you can't read anything into a literal integer. You probably want something more like:
fscanf(fp, "%s", pi->Date);
or possibly:
fscanf(fp, "%39s", pi->Date);
or similar.

Resources