Error message in C invalid binary - c

I'm getting an error:
Type invalid operands to binary & (have 'int *' and 'int')
here is my program. The problem occurs at line 34 or the fscanf num1
#include <stdio.h>
#include <stdlib.h>
FILE *infile;
FILE *prnt;
main()
{
int num1, num2, nums;
char complex;
float fcost;
char name [11];
infile = fopen ("F:/DATA.txt", "r");
prnt = fopen ("F:/income.txt", "w");
if (infile == 0)
{
printf ("FILE NOT ON DISK\n");
system("pause");
return 0;
}
fprintf (prnt, "%-15s %-23s %6s\n\n", "ABAHLMAN", "Program 1", "PAGE 1");
fprintf (prnt, "\n");
fscanf (infile, " %i %i %i %f %c", &nums &num1 &num2 &fcost &name);
while (!feof (infile))
{
int area = (nums * 200) + (num1 * 300) + (num2 * 450);
float cost = fcost + (area * 75.00);
double income = 12 * ((nums *450) + (num1 * 550) + (num2 *700));
float payback = cost/ income;
fprintf (prnt, "%-10s %5f %7c %9.2f\n", name, payback, area, cost);
fscanf (infile, " %d %d %d %f %c", &nums &num1 &num2 &fcost &name);
}
fclose (infile);
fclose (prnt);
return 0;
}

You are not separating the arguments with comma in fscanf() statements. They should be:
fscanf (infile, " %i %i %i %f %s", &nums, &num1, &num2, &fcost, name);
and
fscanf (infile, " %d %d %d %f %s", &nums, &num1, &num2, &fcost, name);
Note that name is an array and it gets converted into a pointer when you pass it to fscanf(). So, the & operator needs to be dropped. As noted in the comments, the format should be %c for name.
Also, see: What is array decaying?
I'd also suggest to use a standard definition for main() function. main() {..} is outdated and should be avoided.
Instead, you can write it as int main(int).

I see a few problems. First, no commas to separate the arguments for scanf.
Next, the last argument for fscanf should have %s format and be passed name without &.
Next, feof is not the way to control a loop, you should check the return value from fscanf anyway.
You also use the wrong format specifier %c for area in printf which should be %d
Finally be consisent with the use of %d or %i format specifier. Unless you want the user to input in other number bases than decimal, stick to %d.
So I suggest the loop should be
while (fscanf (infile, " %d %d %d %f %s", &nums, &num1, &num2, &fcost, name) == 5)
{
int area = nums * 200 + num1 * 300 + num2 * 450;
float cost = fcost + area * 75.00;
double income = 12 * (nums * 450 + num1 * 550 + num2 * 700);
float payback = cost / income;
fprintf (prnt, "%-10s %5f %7d %9.2f\n", name, payback, area, cost);
}

Related

can anyone tell me where is the mistakes? [duplicate]

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]).

Handling user's input

when I input a floating point number (eg. 48.3) the result displayed is 48.00 instead of 48.30 and whenever I try to input string with empty space the program ends immediately. I need help, how to fix this problem?
int integer;
char a[50];
float fnum;
char b[50];
printf("Please enter an integer : ");
scanf("%s",&a);
integer = atoi(a);
printf("\nPlease enter a floating-point number : ");
scanf("%s", &b);
fnum = atoi(b);
printf("Output : \n");
printf("%i + %.2f = %.2f \n", integer,fnum,(integer+fnum));
printf("%i - %.2f = %.2f \n", integer,fnum,(integer-fnum));
printf("%i * %.2f = %.2f \n", integer,fnum,(integer*fnum));
You're converting string b into an integer by calling atoi. You want to convert it to a floating point number, so use atof:
fnum = atof(b);
The atoi returns an int.
The atof returns a float.
int integer;
char a[50];
float fnum;
char b[50];
printf("Please enter an integer : ");
scanf("%s",&a);
integer = atoi(a);
printf("\nPlease enter a floating-point number : ");
scanf("%s", &b);
fnum = atof(b);
printf("Output : \n");
printf("%d + %.2f = %.2f \n", integer,fnum,(integer+fnum));
printf("%d - %.2f = %.2f \n", integer,fnum,(integer-fnum));
printf("%d * %.2f = %.2f \n", integer,fnum,(integer*fnum));

C programming Displaying txt FILE in compiler

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

How to parse operations in c?

I decided to refresh my C by wring a simple little program. I tried to read in from a file "myfile.txt", apply operations and print to stdout. The file contains one line:
2 + 3
the output I expect is:
5
but I found this to be much more complicated than I originally expected. At first I tried using getc() but kept getting segfaults, then I tried fscanf() there is no output from addition to stdin except the print statement that prints:
2 1556274040
Why is the output 2 1556274040? And is there a better way to try to apply operations read form a file, like some apply() function that I can use?
Here's my code:
int main()
{
int ans, num1, num2;
char oper;
FILE *pFile;
pFile = fopen("myfile.txt", "r");
if (pFile != NULL) {
fscanf(pFile, "%d", &num1);
fscanf(pFile, "%c", &oper);
fscanf(pFile, "%d", &num2);
printf ("%d %c %d", num1, oper, num2);
if (oper == '+') {
ans = num1 + num2;
printf(ans);
}
fclose(pFile);
}
return 0;
}
printf(ans);
Not a valid syntax to print an int variable.
Try this -
printf("%d\n",ans);
As you ask
you can use fgets instead of using fscanf to read contents of file but make sure you check their return.
Here's a possible solution...
#include <stdio.h>
int main() {
int ans, num1, num2;
char oper;
FILE *pFile;
pFile = fopen("myfile.txt", "r"); // might need to specify binary/text
if (pFile != NULL) {
// fscanf(pFile, "%d", &num1); need spaces in format specifier.
// fscanf(pFile, "%c", &oper);
// fscanf(pFile, "%d", &num2);
// one way to solve...
fscanf(pFile, "%d %c %d", &num1, &oper, &num2);
fclose(pFile); // stopping some errors
printf ("%d %c %d = ", num1, oper, num2);
} // end if
else
puts("fopen returned NULL");
if (oper == '+') {
ans = num1 + num2;
printf("%d",ans);
} // end if
return 0;
} // end main

fscanf reading everything except for first line

int k;
float regionID;
int t;
char string[100];
float avgTemp,totalTemp;
for(k = 0; k < MAX_STATIONS; k++){
if (fgets(string, sizeof(string), fp) == 0){
break;
}
fscanf(fp,"%d %f %d %d %d %d %d %f %f %f", &stationInfo[k].stationID, &stationInfo[k].temperature, &stationInfo[k].year, &stationInfo[k].month, &stationInfo[k].day, &stationInfo[k].hour, &stationInfo[k].minute, &stationInfo[k].location.latitude, &stationInfo[k].location.longitude, &regionID);
printf("%d %1.2f %d %d %d %d %d %f %f\n", stationInfo[k].stationID, stationInfo[k].temperature, stationInfo[k].year, stationInfo[k].month, stationInfo[k].day, stationInfo[k].hour, stationInfo[k].minute, stationInfo[k].location.latitude, stationInfo[k].location.longitude);
}
So, my program reads my file almost perfectly, but it will not read the first line of my file. Anyone know what might be causing this problem and how I might be able to fix it?
You are quite intentionally reading and discarding a line here:
if (fgets(string, sizeof(string), fp) == 0) {
I think you meant to use sscanf instead of fscanf, so that you use the data returned to you by fgets.
sscanf(string,"%d %f %d %d %d %d %d %f %f %f", ... );
The reason why your program is "not reading the first line of the file" is because it actually already read the first line when you called
if (fgets(string, sizeof(string), fp) == 0)
The change to fix this is simple:
for(k = 0; k < MAX_STATIONS; k++)
{
if (fscanf(fp,"%d %f %d %d %d %d %d %f %f %f", &stationInfo[k].stationID,
&stationInfo[k].temperature,
&stationInfo[k].year,
&stationInfo[k].month,
&stationInfo[k].day,
&stationInfo[k].hour,
&stationInfo[k].minute,
&stationInfo[k].location.latitude,
&stationInfo[k].location.longitude,
&regionID) > 10)
{
break;
}
printf("%d %1.2f %d %d %d %d %d %f %f\n", stationInfo[k].stationID,
stationInfo[k].temperature,
stationInfo[k].year,
stationInfo[k].month,
stationInfo[k].day,
stationInfo[k].hour,
stationInfo[k].minute,
stationInfo[k].location.latitude,
stationInfo[k].location.longitude);
}
Note the fscanf's return value:
On success, the function returns the number of items of the argument list successfully filled. This count can match the expected number of items or be less (even zero) due to a matching failure, a reading error, or the reach of the end-of-file.
Hope this is what you are looking for!

Resources