Failing to calculate amount - c

This C program is to print an invoice, the only problem is that it won't output the unit price and calculate the amount. Can someone show me how. This is now the full code. And if there is any other error, please help.
struct PRODUCTSINFO {
int code; // products number
int qty;
char name[ 50 ]; // products name
double unit_price; // account unit_price
}; // end structure PRODUCTSINFO
struct PRODUCTSINFO products[100] ;
int main(){
int p;
int x ;
int i=0;
double amount=0;
printf("Enter the amount of products to be purchased : ");
scanf("%d",&x);
for(i=0;i<x;i++){
printf("\nEnter product code #%d : ",i+1);
scanf(" %d",&products[i].code);
printf("\nEnter product name#%d:",i+1);
scanf("%s",&products[i].name);
printf("\nPlease quantity#%d : ",i+1);
scanf("%d",&products[i].qty);
printf("Enter unit price#%d:",i+1);
scanf("%.lf",&products[i].unit_price);
fflush(stdin);
}
system("cls");
printf("************************INVOICE*******************************\n");
printf("-----------------------------------------------\n);
printf("S/N | CODE | NAME OF PRODUCTS | QUANTITY | UNIT PRICE |AMOUNT \n");
printf("------------------------------------------------------\n");
for(i=0;i<x;i++){
printf("\n%d",i);
printf("\t %d",products[p].code);
printf("\t %s",products[p].name);
printf("\t\t\t%d",products[p].qty);
printf("\t\t%.2f",products[p].unit_price);
p++;
amount=products[p].qty*products[p].unit_price;
printf("\t%.2f\n",amount);
}
}

You haven't shown the complete code.
Q: Is x declared to be an integer (or better,unsigned)?
Q: Are you sure x > 0 when you start the loops?
... AND ...
If you're running the program as a CMD prompt in Windows ... be sure to add getchar() before the end of the program. Otherwise, the program will exit and your window could disappear before you see any output.
ALSO:
Note Weather Vane's suggestion about your scanf formatting errors. Here are two good links:
https://beej.us/guide/bgc/output/html/multipage/scanf.html
https://linux.die.net/man/3/scanf
'Hope that helps

Your code has lot of syntax errors. I just fixed it. And here is the working code of it.
Compare this code with the previous one to realize your mistakes.
struct PRODUCTSINFO {
int code; // products number
int qty;
char name[ 50 ]; // products name
double unit_price; // account unit_price
}; // end structure PRODUCTSINFO
struct PRODUCTSINFO products[100] ;
int main(){
int p;
int x ;
int i=0;
double amount=0;
int total = 0;
printf("Enter the amount of products to be purchased : ");
scanf("%d",&x);
for(i=0;i<x;i++){
printf("\nEnter product code #%d : ",i+1);
scanf(" %d",&products[i].code);
printf("\nEnter product name#%d:",i+1);
scanf("%s",products[i].name);
printf("\nPlease quantity#%d : ",i+1);
scanf("%d",&products[i].qty);
printf("Enter unit price#%d:",i+1);
scanf("%lf",&products[i].unit_price);
}
printf("************************INVOICE*******************************\n");
printf("-----------------------------------------------\n");
printf("S/N | CODE | NAME OF PRODUCTS | QUANTITY | UNIT PRICE |AMOUNT \n");
printf("------------------------------------------------------\n");
for(i=0;i<x;i++){
printf("\n%d",i);
printf("\t %d",products[i].code);
printf("\t %s",products[i].name);
printf("\t\t\t%d",products[i].qty);
printf("\t\t%.2f",products[i].unit_price);
p++;
amount=products[i].qty*products[i].unit_price;
total += amount;
printf("\t%.2f\n",amount);
}
Printf("%d",total);
return 0;
}

Related

What this error mean : expression must have pointer to object type but it has type struct add_stock in c

#include <stdio.h>
int i, n;
struct add_stock
{
char fullname[30];
int stocks;
char com_name[30];
int shares;
float price;
float total;
int totalmoney;
} add;
int main()
{
printf("Enter full name : ");
scanf(" %[^\n]s", add.fullname);
printf("Enter the no. of stocks you want to purchased : ");
scanf("%d", &n);
for (i = 0; i < n; i++)
{
printf("Enter the name of the company : ");
scanf(" %[^\n]s", add[i].com_name);
printf("Enter the no. of shares you want to purchased : ");
scanf("%d", &add[i].shares);
printf("Enter the price of each share : ");
scanf("%f", &add[i].price);
add.total = add.shares * add.price;
printf("Toatl money invested in this stock : ");
scanf("%f", &add[i].total);
}
printf("Total money invested : ");
scanf("%d", add.totalmoney);
return 0;
}
In question it's add_stock , not add stock , i write like this becoz it's not accepting question.
So, i get error for "add" saying subscripted value is neither array nor pointer nor vector.
To declare an array of strucuture, you need to define the variable as below,
struct add_stock
{
char fullname[30];
int stocks;
char com_name[30];
int shares;
float price;
float total;
int totalmoney;
} add[20];
this makes the add as an array of add_stock elements.
then you can access it as, add[0].total, add[1].price and so on.
If you want to declare an array that need to be having dynamic number of elements, you can do so as below.
struct add_stock* arStock;
//allocate memory to hold 10 elements
arStock = (add_stock*)malloc( 10 * sizeof(struct add_stock));
arStock[0]->total=10; //access it with ->, instead of .

New to programming. Need help adjusting code to allow for undetermined number of students

Need help adjusting code to allow for undetermined number of students. Tried to modify with help I received last week, but it seems like I am not doing it correctly.
#include <stdio.h>
int main ()
{
/* variable definition: */
char StudentName[100];
float ExamValue, Sum, Avg;
int students,exams;
// Loop through 5 Students
for (students=0; students <5 ; students++)
{
// reset Sum to 0
Sum =0.0;
printf("Enter Student Name \n");
scanf("%s", StudentName);
// Nested Loop for Exams
for (exams=0; exams < 3; exams++)
{
printf ("Enter exam grade: \n");
scanf("%f", &ExamValue);
Sum += ExamValue;
}
Avg = Sum/3.0;
printf( "Average for %s is %f\n",StudentName,Avg);
}
return 0;
}
Not sure how I need to go about changing the code. Any help will be greatly appreciated
Not sure I understand correctly, I understood you want to loop an undertemined amount of students.
I will assume you want to stop if certain keyword is added as a student name, let's say "quit".
Without changing your structure much, it would look like this.
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
int main (){
char StudentName[100];
float ExamValue, Sum, Avg;
int students,exams;
bool in = true;
while (in) {
Sum =0.0;
printf("Enter Student Name \n");
scanf("%s", StudentName);
if( strcmp(StudentName, "Quit") == 0 || strcmp(StudentName, "quit") == 0){
in = false;
break;
}
for (exams=0; exams < 3; exams++){
printf ("Enter exam grade: \n");
scanf("%f", &ExamValue);
Sum += ExamValue;
}
Avg = Sum/3.0;
printf( "Average for %s is %f\n",StudentName,Avg);
}
return 0;
}

Wrong output by C Program

Can any one find the flaw and explain the below program? I will brief you,
1) Using structures in the program to store information about a products.
2) Finding the lowest price and displaying it.
All works fine but output does not give the lowest value.
#include <stdio.h>
#include <malloc.h>
struct product{
int code;
char name[30];
float price;
int qty;
};
void prodata(struct product *p,int n){
int i;
for (i=0;i<n;i++){
printf("\n Enter the Item Code. : ");
scanf("%d",&p[i].code);
fflush(stdin);
printf("\n Enter the Item Name. : ");
scanf("%s",&p[i].name);
printf("\n Enter the Item Price. : ");
scanf("%f",&p[i].price);
printf("\n Enter the Item Quantity in hand. : ");
scanf("%d",&p[i].qty);
}
}
void dispdata(struct product *p, int n){
int i;
int min;
min = 0;
for ( i = 1 ; i < n ; i++ ){
if (p[i].price < p[min].price) {
min = i;
}
}
printf("\n ** The Cheapest Product ** \n");
printf("\n The Product Code : %d \n",p[min].code);
printf("\n The Product Name is : %s \n",p[min].name);
printf("\n The Product Price is : %f \n",p[min].price);
printf("\n The Product Stock : %d \n",p[min].qty);
}
main(){
struct product *p=NULL;
int n;
printf("\n Product Information. \n");
printf("\n Please Enter the Number of Items : ");
scanf("%d",&n);
while(n<=1){
printf("\n Please Enter correct number of items : ");
scanf("%d",&n);
}
p =(struct product*)malloc(sizeof(struct product)*n);
prodata(p,n);
dispdata(p,n);
return 0;
}
if (p[i].price < min) {
min = i;
}
You're setting the min to be the index, not the actual price of the object.
In addition to Daniel's response make sure to index i at 0. Your for statements should be:
for(i = 0; i < n; i++) {....
Right now your missing 1 whole product since your for loop starts counting at 1 and ends after i iterates to (n-1) and executes the code block in the for loop

I have had a lot of trouble modifying this current code to set an undetermined amount of students for this C code

#include <stdio.h>
int main ()
{
/* variable definition: */
char StudentName[100];
float ExamValue, Sum, Avg;
int students,exams;
// Loop through 5 Students
for (students=0; students <5 ; students++) {
// reset Sum to 0
Sum =0.0;
printf("Enter Student Name \n");
scanf("%s", StudentName);
// Nested Loop for Exams
for (exams=0; exams < 3; exams++)
{
printf ("Enter exam grade: \n");
scanf("%f", &ExamValue);
Sum += ExamValue;
}
Avg = Sum/3.0;
printf( "Average for %s is %f\n",StudentName,Avg);
}
return 0; }
This is the code I need to change so that instead of inputing 5 student names with 3 exam scores, It will be an undetermined amount of students with 3 quiz scores. I could write it so that the user could enter the amount of students they want, but I dont think thats what they mean by "undetermined". What is the way that you could write it so you could enter as many student names you want. Im always willing to learn more, and any help is appreciated. Thank you.
You could do that by replacing the for loop with the while loop:
int bContinue = 1;
while (bContinue)
{
// your code goes in here
printf("Enter 0 if you wanna stop\n");
scanf("%d", &bContinue);
}
#include <stdio.h>
int main ()
{
/* variable definition: */
char StudentName[100];
float ExamValue, Sum, Avg;
int students,exams,num_values;
printf("How many students to find the average for? ");
scanf("%d", &num_values);
// Loop through 5 Students
for (students=0; students < num_values ; students++) {
// reset Sum to 0
Sum =0.0;
printf("Enter Student Name \n");
scanf("%s", StudentName);
// Nested Loop for Exams
for (exams=0; exams < 3; exams++)
{
printf ("Enter exam grade: \n");
scanf("%f", &ExamValue);
Sum += ExamValue;
}
Avg = Sum/3.0;
printf( "Average for %s is %f\n",StudentName,Avg);
}
return 0; }
I figured out that the users could input the amount so I came up with this code. However I am going to practice the other answers given. Thank You!

C : program to calculate GPA for students

Whenever I insert 2 or more students, the program gives the same gpa from the first student. I insert for both every time, and how can I display the max gpa and who got it?
#include <stdio.h>
FILE *f;
struct student
{
char name[20];
int id;
int n;
float sum;
float gpa;
} s[100];
float FUN_GPA(int n);
int main ()
{
int m,i,x,b;
float max=0;
s[i].sum=0;
printf("Please Enter Number Of Students : ");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf("\nStudent %d : \n",i+1);
printf("Name : ");
scanf("%s",&s[i].name);
printf("ID : ");
scanf("%d",&s[i].id);
printf("Number Of Courses : ");
scanf("%d",&s[i].n);
}
printf("\nEnter Student Marks");
for(i=0;i<m;i++)
{
printf("\n\n\t\t\t......[ Student %d ]......\n",i+1);
for(x=0;x<s[i].n;x++)
{
printf("Course %d Mark : ",x+1);
scanf("%d",&b);
s[i].sum+=b;
}
printf("\nSum Of The Courses Marks = %3.f",s[i].sum);
printf("\nGPA For Student %d = %f",i+1,FUN_GPA(s[i].gpa));
}
if (s[i].gpa>max)
max=s[i].gpa;
printf("\n\nHighest GPA is done by Student %d with GPA = %f",i,max);
{
if((f=fopen("d:\\STUDENTS.txt","w"))==NULL)
printf("\ncant open file ");
// if((f=fopen("d:\\STUDENTS.txt","r"))==NULL)
// printf("\ncant open file ");
fprintf(f,"Name : %s\t ID : %d\t GPA = %f \n",s[i].name,s[i].id,s[i].gpa);
fclose(f);
}
}
float FUN_GPA(int i)
{
s[i].gpa=0;
s[i].gpa=s[i].sum/s[i].n;
return s[i].gpa;
}
Replace FUN_GPA(s[i].gpa) by FUN_GPA(i).
Put this inside the loop; not much use having it outside:
if (s[i].gpa>max)
max=s[i].gpa;
The same applies to this initializer, by the way:
s[i].sum=0;
In general, do not use variable i outside the loop; this is bad practice and in your particular case, totally wrong!
i is not the index of the best performing student; it is not even a correct index once the loop ends, since it refers to the array element immediately following the last registered student. Please introduce a new variable to keep track of the index of the best performer so far. Instead of i, use this new variable in the lines to follow:
fprintf(f,"Name : %s\t ID : %d\t GPA = %f \n",s[i].name,s[i].id,s[i].gpa);
Same here; do not forget to add 1 like you did everywhere else already!
printf("\n\nHighest GPA is done by Student %d with GPA = %f",i,max);
General advice: use descriptive names. i is OK afaic; s and n are not so great, m, x and b would have been good for 50 lashes from my old teachers for sure!
This needs to be in a for loop (you're only checking one as it is currently written):
if (s[i].gpa>max)
max=s[i].gpa;
You'll probably also want to keep track of which i had the max.
So something like:
int iwithmax;
for ( i = 0 ; i < m ; i++) {
if (s[i].gpa > max) {
max = s[i].gpa;
iwithmax = i;
}
}
and change print statement accordingly:
printf("\n\nHighest GPA is done by Student %d with GPA = %f",iwithmax,max);

Resources