The code works like a charm at the moment. But, the last 2 lines of output are identical as you can see here.
What is the problem here?
The datas came from a txt file that was build earlier.
1 CADBURY 999 1.900000
2 PEPSI 999 2.500000
3 IPHONE 976 2500.000000
4 SPIRULINA 100 50.000000
2 PAIPSI 100 0.900000
10 BLACKMORE 98 30.000000
17 TROPICANA 13 1.500000
17 TROPICANA 13 1.500000
Here's the code:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>
int addProduct();
struct product {
int quantity, reorder, i, id;
char name[20];
float price;
};
int main() {
FILE *fp;
int i = 0;
struct product a;
system("cls");
char checker;
int counter;
do {
fp = fopen("addproduct.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') {
fp = fopen("addproduct.txt", "r");
while (!feof(fp)) {
fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price);
printf("%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
}
fclose(fp);
}
return(0);
}
while (!feof(fp)) does not work as expected: feof(fp) only becomes true after input has failed. During the last iteration, fscanf() fails, but you do not check its return value and the values from the previous iterations are used by the last printf().
You should instead write:
while (fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price) == 4) {
printf("%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
}
When you are printing out the contents of the file at the end of your program, the fscanf function does not set feof until it attempts to read past the end of the file.
This means that after you read the last line in the file, the feof is still not true.
So the loop continues and fscanf then attempts to read another line but fails. So the variables a.id, a.name etc are the same as what they were after the previous loop execution of fscanf.
You should check that fscanf has returned the expected number of fields before you print out the results.
For example,
...
if (fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price) == 4)
printf("%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
...
would fix the issue.
edit: Sorry 4 parameters not 5, fixed
Related
I want to take input from user using structure. So I'm using the code like below. But it's not printing the values which I'm entering. Can anyone help me?
#include <stdio.h>
int main()
{
struct book
{
char name;
float price;
int pages;
};
struct book b1, b2;
printf("Enter the Name, Price and Pages of Book 1: ");
scanf("%c %f %d", &b1.name, &b1.price, &b1.pages);
printf("Enter the Name, Price and Pages of Book 2: ");
scanf("%c %f %d", &b2.name, &b2.price, &b2.pages);
printf("Here is the data you've entered: \n");
printf("Name: %c Price: %f Pages: %d\n", b1.name, b1.price, b1.pages);
printf("Name: %c Price: %f Pages: %d\n", b2.name, b2.price, b2.pages);
return 0;
}
But I'm not getting the output as desired. My
Output Image
Your question is similar to this one: scanf() leaves the newline character in the buffer
And can be corrected like this:
#include <stdio.h>
int main()
{
struct book
{
char name;
float price;
int pages;
};
char buffer[255];
struct book b1, b2;
printf("Enter the Name, Price and Pages of Book 1: ");
scanf(" %c %f %d", &b1.name, &b1.price, &b1.pages);
printf("Enter the Name, Price and Pages of Book 2: ");
scanf(" %c %f %d", &b2.name, &b2.price, &b2.pages);
printf("Here is the data you've entered: \n");
printf("Name: %c Price: %f Pages: %d\n", b1.name, b1.price, b1.pages);
printf("Name: %c Price: %f Pages: %d\n", b2.name, b2.price, b2.pages);
return 0;
}
Note the leading space before the %c input.
This question already has an answer here:
C Access violation writing location scanf_s
(1 answer)
Closed 1 year ago.
While the program is running , in the middle of it (after typing the Vaccine name ) it stops and gives me {Exception Thrown} can anyone help me fix this issue? I have also included a photo of the error it gives me
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
int main()
{
int choose, dosage, quantity;
char code[10];
char name[32];
char produce[64];
float population;
{
printf("COVID-19 Vaccination System \n");
printf("1:- Inventory Creation \n");
printf("2:- Update Vaccine Quantities \n");
printf("3:- Search Vaccine \n");
printf("4:- Produce a list of all Vaccines and their distributed Quantities \n");
printf("5:- EXIT \n\n");
printf("==>");
scanf_s("\n%d", &choose);
if (choose == 1)
{
char ch;
FILE* cv;
errno_t err;
err = fopen_s(&cv, "vaccine.txt", "w");
for (int i = 0; i < 5; i++)
{
printf("Please Enter Vaccine Full information %d \n", i + 1);
printf(" Name of Vaccine : ");
scanf_s("\n%s", &name);
printf("Please Enter Vaccine Code %c \n", i + 1);
printf(" Vaccine Code : ");
scanf_s("\n%s", &code);
printf("Please Enter Vaccine Producing Country %c \n", i + 1);
printf("Producing Country : ");
scanf_s("\n%s", &produce);
printf("Please Enter Dosage Required %d \n", i + 1);
printf("Dosage Required (MAX=2 - MIN=1) : ");
scanf_s("\n%d", &dosage);
printf("Please Enter Population Covered %d \n", i + 1);
printf("Population Covered (%) : ");
scanf_s("\n%f", &population);
printf("Please Enter Vaccine Quantity %d \n", i + 1);
scanf_s("\n%d", &quantity);
fprintf("%s %s %s %d %f %d", name, code, produce, dosage, population, quantity);
}
}
}
return 0;
}
The line
fprintf("%s %s %s %d %f %d", name, code, produce, dosage, population, quantity);
is wrong. You have to pass a file pointer as the first argument of fprintf(). It should be:
fprintf(cv, "%s %s %s %d %f %d", name, code, produce, dosage, population, quantity);
Also
You should check if fopen_s succeeded before proceeding to next operation and stop operation if it failed.
You should remove & before the array names (code, name, produce) used in scanf_s. Array in expressions (excluding some exceptions) are automatically converted to pointers to the first element, so no explicit & is not needed here. Also it will make the type wrong scanf() expects char* for %s, but things like &code are an pointers to arrays (like char(*)[10]).
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();
Here is the input from TEXT FILE :
19 BISON-BURGER 10 15.000000
10 BRAISED-COD 5 17.000000
23 MOJITO 8 11.000000
3 IRISH-COFEE 6 2.300000
7 LAMB-SHOULDER 8 23.000000
The output came out from compiler after input being key in was :
10 BRAISED-COD 5 17.000000
3 IRISH-COFEE 6 2.300000
7 LAMB-SHOULDER 8 23.000000
Why the compiler skipping some of the lines ? Is there any changes I need to make?
Please help. Thank you so much.
Here is the full code :
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <ctype.h>
struct product
{
int quantity, reorder, i, id;
char name[20];
float price;
};
int main()
{
FILE * fp;
int i=0;
struct product a;
system("cls");
char checker;
do
{
fp = fopen("addproduct.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')
{
fp = fopen("addproduct.txt","r");
while(fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price)==4)
{
fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price);
printf("%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
}
fclose(fp);
}
return(0);
}
You're reading two lines from the file on each iteration of the output loop. Get rid of the extra scanf:
while(fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price)==4)
{
printf("%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
}
You're calling fscanf() twice each time through the while loop. First in the while condition, then in the body of the loop. You're only printing the variables read by the second one, so the lines read by the first fscanf() are being ignored. Get rid of the second fscanf().
while(fscanf(fp, "%d %s %d %f", &a.id, a.name, &a.quantity, &a.price)==4)
{
printf("%d %s %d %f\n\n", a.id, a.name, a.quantity, a.price);
}
My code ( gist link )
//pre-processor directives
#include <stdio.h>
#include <math.h>
//Main function
int main() {
int month, day, year, lowest_temp, highest_temp;
float temp;
char fname[30];
FILE * ifp;
printf("Tell me about your dragon's preferred temperature for flying: What is the coldest temperature they can fly in?\n");
scanf("%d", lowest_temp);
printf("What is the hottest temperature they can fly in?\n");
scanf("%d", highest_temp);
printf("Please enter the name of the weather data file for Dragon Island.\n");
scanf("%s", fname);
ifp = fopen(fname, "r");
fscanf(ifp, "%d %d %d %f, &month &day &year &temp");
printf("%d %d %d %f, &month &day &year &temp");
return 0;
}
Crashes after first scanf no errors
If you can tell me what the error is I'd appreciate it. Also if you can help me with the next steps in my problem that would be awesome as well.
My assignment
https://gist.github.com/anonymous/e9292f14b2f8f71501ea/88e9e980632871f1f1dedf7589bb518f1bba43e8
scanf("%d", lowest_temp);
...
scanf("%d", highest_temp);
%d require address of int argument you pass it just int variable . Pass their address -
scanf("%d",&lowest_temp);
...
scanf("%d",&highest_temp);
And these both statements-
fscanf(ifp, "%d %d %d %f, &month &day &year &temp");
printf("%d %d %d %f, &month &day &year &temp");
should be-
fscanf(ifp, "%d %d %d %f", &month &day &year &temp);
printf("%d %d %d %f", month ,day,year ,temp);