Welcome to the forum. Also, it's nice to see a good first post - clear questions, relevant data, code in code tags, and proper "main()" structure. (All too often, first posts lack most, if not all, of these things.)
Anyway, onto the code. The first thing that would help is using consistent indentation in your code. Here's what it looks like with proper indentation (I took the liberty of moving your structure declaration above "main()") - notice how much easier it is to scan over with your eyes:
Code:
#include<stdio.h>
#include<stdlib.h>
#define FILENAME "file.txt"
#define SIZE 100
typedef struct
{
char last_name[15];
char name[15];
double car_price;
double down_payment;
int loan_years;
} Sales;
int main(void)
{
FILE *salescount;
Sales buyer[SIZE];
double x=0; // monthly payment
double y=0; // car price
double z=0; //down payment
int yearsx=0;
int count=0;
salescount = fopen(FILENAME, "r");
printf("\tLast Name \t \t Name \t \t Car Price \t Down Payment\tLoan Years\tMonthly Payment \n\n");
while((fscanf(salescount, "%s %s %d %d %i",
&buyer[count].last_name,
&buyer[count].name,
&buyer[count].car_price,
&buyer[count].down_payment,
&buyer[count].loan_years))>1)
{
printf("%15s \t", buyer[count].last_name);
printf("%15s \t", buyer[count].name);
printf("%d \t \t ", buyer[count].car_price);
printf("%d \t \t ", buyer[count].down_payment);
printf("%i \t \t ", buyer[count].loan_years);
y = buyer[count].car_price;
z = buyer[count].down_payment;
yearsx = buyer[count].loan_years;
x = ((y)-(z))/(12*yearsx));
printf("%f \n", x);
count++;
}
return 0;
}
Secondly, you should be compiling with warnings set to maximum (how to do so depends on your compiler/IDE). All warnings and errors should be fixed as they are found.
Code:
main.c||In function 'main':|
main.c|34|warning: format '%s' expects type 'char *', but argument 3 has type 'char (*)[15]'|
main.c|34|warning: format '%s' expects type 'char *', but argument 4 has type 'char (*)[15]'|
main.c|34|warning: format '%d' expects type 'int *', but argument 5 has type 'double *'|
main.c|34|warning: format '%d' expects type 'int *', but argument 6 has type 'double *'|
main.c|38|warning: format '%d' expects type 'int', but argument 2 has type 'double'|
main.c|39|warning: format '%d' expects type 'int', but argument 2 has type 'double'|
main.c|46|error: expected ';' before ')' token|
main.c|46|error: expected statement before ')' token|
||=== Build finished: 2 errors, 6 warnings ===|
A few warnings, and one error. This should not have compiled for you. As indicated, you have an error on line 46 (referenced to the code above in my post). Counting parenthesis, you have an extra closing ')' at the end.
If you look at the line numbers of the warnings, and the corresponding code, you should see several mis-matches in your "fscanf()" arguments versus format string.
>> warning: format '%s' expects type 'char *', but argument 3 has type 'char (*)[15]'
When reading a string this way, you don't need the '&' in front of the array name where that string is to be stored. This is because the "scanf()" family needs a pointer to the variable of interest, and the name of the array without the element notation acts as a pointer to that array.
>> warning: format '%d' expects type 'int *', but argument 5 has type 'double *'
This is straight-forward. You use "%d" (for integer) in your format string, but are trying to store the value into a variable of data type double. Instead of "%d", you should be using "%lf".
>> warning: format '%d' expects type 'int', but argument 2 has type 'double'
Looking at the line numbers, this warning corresponds to a "printf()" statement. Similar issue as above - you're trying to use "%d" (for integer) to print a double. Ditto on the next line.
A couple of other things to note:
- You should check that the file has opened successfully before trying to access it (you can do this by making sure the FILE pointer returned is not NULL)
- You should close the file before exiting the program
- The first line of your data file is a header, which does not contain useful data - this should be handled accordingly in your code
- Don't use "magic numbers" for the array sizes of your strings in the struct. You used #define for the size of the Sales array - you should do something similar for the length of strings in your struct.
- Kudos for checking the return value of "fscanf()". It might be better to check explicitly that it read all five data items (!= 5) instead of just more than one (>1).
Give some of these corrections a shot and see if you can work towards a working program on your own. If you still can't get it to work, post your updated code along with the issues you are facing. Feel free to ask if you need clarification on any of the points I have made.