Inventory of a grocery store - c

I write a program to keep track of the inventory of a grocer store. But in my code i would like to print a value. And value is (Number of units) * (unit price). But somehow i get get the garbage value in my program. So Can you Please help me?
#include<stdio.h>
#include<conio.h>
#define MAX 5
int printinventory(int , int unit[] , float price[]);
int main()
{
int item[MAX],unit[MAX],x,i;
float price[MAX];
printf("Please enter how many category items (up to 5) : ");
scanf("%d",&x);
for(i=0;i<x;i++)
{
printf("\nPlease enter Number of Units #%d : ",i+1);
scanf(" %d",&unit[i]);
printf("\nPlease enter the Unit Price #%d : ",i+1);
scanf(" %f",&price[i]);
}
printinventory(x , unit , price);
getch();
}
int printinventory (int y, int unit[] , float price[])
{
int i,j=0;
float value[MAX];
for(i=0;i<y;i++);
{
value[i] = (unit[i] * price[i]);
}
system("cls");
printf("Item Number of Units Unit Price Value ");
printf("\n\n------------------------------------------------");
for(i=1;i<=y;i++)
{
printf("\n%d",i);
printf("\t %d",unit[j]);
printf("\t\t $%.2f",price[j]);
printf("\t$%.2f",value[j]);
j++;
}
printf("\n\n------------------------------------------------");
printf("\n\t\t\t\tTotal $ ");
getch();
}

The problem seems to be that you've mistakenly included a semicolon at the end of one of your for loops:
for(i=0;i<y;i++);

Related

Having issues with this simple program

The program is just simply supposed to calculate the users age by subtracting their dob from the current year. When I run the program it compiles successfully but I get a long number such as -215863352. The if and else conditions are added just to test them out, I was writing various programs using them to make sure I understand the syntax in c. I figure I'm missing something simple but can't figure it out.
#include <stdio.h>
int main()
{
int year;
int cyear;
int age = cyear - year;
printf("Please enter the year you were born: \n");
scanf("%i", &year);
printf("Now enter the current year: \n");
scanf("%i", &cyear);
if (1+1 == 2){
printf("You must be %i", age);
}
else {
printf("Cannot compute age, GOODBYE:\n");
}
return 0;
}
You are calculating the age before the input is taken from the user. So the age variable is storing a garbage value.
Solution:
Position the calculation of age after taking the input from user that is after taking input of cyear using scanf. The correct code is given below
#include <stdio.h>
int main()
{
int year;
int cyear;
int age =0; //initialise with 0
printf("Please enter the year you were born: \n");
scanf("%i", &year);
printf("Now enter the current year: \n");
scanf("%i", &cyear);
age = cyear - year; //note the change here
if (1+1 == 2){
printf("You must be %i", age);
}
else {
printf("Cannot compute age, GOODBYE:\n");
}
return 0;
}
enter code here
#include <stdio.h>
int main()
{
long long int year;
printf("Please enter the year you were born: \n");
scanf("%lld",&year);
long long int cyear;
printf("Now enter the current year: \n");
scanf("%lld",&cyear);
long long int age = cyear-year;
if (1){
printf("You must be %lld", age);
}
else { printf("Now enter the current year: \n");
scanf("%lld",&cyear);
printf("Cannot compute age, GOODBYE:\n");
}
return 0;
}

How do i print out the names of the items inputted?

i'm new to programming so i'm having a hard time figuring out how to output the names of the items inputted in my loop, all other calculations are executing but i keep getting unknown values when the names of the item is outputted.
#include <stdio.h>
int main()
{
int count,num,a;
char *item [15][100];
float price,net_total, tax, total_pay,grandT;
do{
printf("Please enter the name of the item:");scanf("%s",&item[a]);
printf("\n");
printf("Please enter the price of the item: $");scanf("%f",&price);
net_total+=price;
tax=0.14*net_total;
grandT=net_total+tax;
printf("\nSelect '1' to continue or '0' to exit:\n");
scanf("%d",&num);
count++;
}
while (num!=0);
system ("cls");
printf("\tSummary Information of Items purchsed\n");
printf("Name(s)of the item(s) purchased: \t%s\n ",item[a]);
printf("Net total:\t$%.2f\n",net_total);
printf("Total tax:\t$%.2f\n",tax);
printf("Total payable:\t$%.2f\n ",grandT);
}`
1/ You should indent your code so it becomes easier to read.
2/ You didn't initialize a, count and net_total. (a is an index from 0 to 15).
3/ char item [15][100]
#include <stdio.h>
int main()
{
int count=0,num,a=0;
char item [15][100];
float price,net_total=0, tax, total_pay,grandT;
do
{
printf("Please enter the name of the item:");
scanf("%s",&item[a]);
printf("\n");
printf("Please enter the price of the item: $");
scanf("%f",&price);
net_total+=price;
tax=0.14*net_total;
grandT=net_total+tax;
printf("\nSelect '1' to continue or '0' to exit:\n");
scanf("%d",&num);
count++;
}
while (num!=0);
system ("cls");
printf("\tSummary Information of Items purchsed\n");
printf("Name(s)of the item(s) purchased: \t%s\n ",item[a]);
printf("Net total:\t$%.2f\n",net_total);
printf("Total tax:\t$%.2f\n",tax);
printf("Total payable:\t$%.2f\n ",grandT);
}

I need only one output but it is two or more

I want to find the largest cgpa which is only one cgpa but it's shows 2 or more when I enter same cgpa as input.
How do I find only one cgpa which is largest?
How do I find the exact answer?
**I want to find the largest cgpa which is only one cgpa but it's shows 2 or more when I enter same cgpa as input.**
How do I find only one cgpa which is largest?
input:
Enter the number of student: 3
Enter records for Student 1
Enter Student ID : 2
Enter student name: s
Enter CGPA : 3
Enter records for Student 2
Enter Student ID : 201311
Enter student name: A
Enter CGPA : 4
Enter records for Student 3
Enter Student ID : 201312
Enter student name: B
Enter CGPA : 4
output:
201311 A has got the highest CGPA: 4.00
201312 B has got the highest CGPA: 4.00
code
#include<stdio.h>
struct student
{
int ID;
char name[50];
float cgpa;
};
int main()
{
struct student s[5000];
int i,num;
float large;
printf("Enter the number of student: ");
scanf("%d",&num);
for(i=0; i<num; i++)
{
printf("Enter records for Student %d\n",i+1);
printf("Enter Student ID : ");
scanf("%d",&s[i].ID);
getchar();
printf("Enter student name: ");
gets(s[i].name);
printf("Enter CGPA : ");
scanf("%f",&s[i].cgpa);
getchar();
}
large=s[0].cgpa;
for(i=0; i<num; i++)
{
if(s[i].cgpa>large)
{
large=s[i].cgpa;
}
}
for(i=0; i<num; i++)
{
if(s[i].cgpa==large)
{
printf("%d %s has got the highest CGPA: %.2f\n",s[i].ID,s[i].name,s[i].cgpa );
}
}
}
It's printing multiple answers when more than one is tied for the largest. However, if you want it to stop after printing the first one, all you need to do is add a break statement after the printf:
if(s[i].cgpa==large)
{
printf("%d %s has got the highest CGPA: %.2f\n",s[i].ID,s[i].name,s[i].cgpa );
break;
}
If you want to print only the first one then use break or return just after the statement which is printing the output.
You don't need to compare them again after you've found the largest already.
This code below should do the trick.
#include<stdio.h>
struct student {
int ID;
char name[50];
float cgpa;
};
int main() {
struct student s[5000];
int i, num;
int largestIdx = 0;
printf("Enter the number of student: ");
scanf("%d", &num);
for (i = 0; i < num; i++) {
printf("Enter records for Student %d\n", i + 1);
printf("Enter Student ID : ");
scanf("%d", &s[i].ID);
getchar();
printf("Enter student name: ");
gets(s[i].name);
printf("Enter CGPA : ");
scanf("%f", &s[i].cgpa);
getchar();
}
for (i = 1; i < num; i++) {
if (s[i].cgpa > s[largestIdx].cgpa) {
largestIdx = i;
}
}
printf("%d %s has got the highest CGPA: %.2f\n", s[largestIdx].ID, s[largestIdx].name, s[largestIdx].cgpa);
}
And do remember: never compare equal with two float variables, you'll get surprised by the output of the following code.. 🤨
float a = 3.3, b=3.0;
printf("a equals %f? %d\n", b+0.3, a==(b+0.3));

scanf in function seems to be writing garbage values to one array but not the other, despite both being identical

So in the function getRoster, I have two arrays that are defined in the same way in main and called in the same way. But for some reason, one of the scanf functions write garbage data to the array so that in line 91 (A debugging line in this case) one value is the one I entered and the other is seemingly random. I've checked this code top to bottom already to see if there were any notations that I added to one array and not the other, and I can't seem to find a single one.
#include <stdio.h>
#include <stdlib.h>
void getRoster(int *jerseyNumbers[10], int *playerRatings[10])
{
int i;
for (i=0; i<5; ++i)
{
printf("Enter player %d's jersey number\n", i+1);
scanf("%d", &jerseyNumbers[i]);
printf("Enter player %d's rating\n", i+1);
scanf("%d", &playerRatings[i]);
}
}
void updateRating(int *jerseyNumbers[10], int *playerRatings[10])
{
int i, n = -1, hold;
printf("Enter a jersey number:\n");
scanf("%d", n);
for (i=0; i<5; ++i)
if(jerseyNumbers[i]==n)
hold = n;
if (n=-1);
printf("Error");
printf("Enter a new rating for player:\n");
scanf("%d", &playerRatings[n]);
}
void aboveRating()
{
}
void replacePlayer()
{
}
void outputRoster(int *jerseyNumbers[10], int *playerRatings[10])
{
int i;
for (i=0; i<5; ++i)
{
printf("Player %d -- Jersey number: %d , Rating: %d \n", i+1, jerseyNumbers[i], playerRatings[i]);
}
}
void menu(int *jerseyNumbers[10], int *playerRatings[10])
{
char menuInput;
printf("\nMENU\n");
printf("u - Update player rating\n");
printf("a - Output players above a rating\n");
printf("r - Replace player\n");
printf("o - Output roster\n");
printf("q - Quit\n");
printf("\nChoose an option:\n");
scanf(" %c", &menuInput);
if (menuInput == 'u')
{updateRating(&jerseyNumbers[10], &playerRatings[10]);
}
else if (menuInput == 'a')
{aboveRating();
}
else if (menuInput == 'r')
{replacePlayer();
}
else if (menuInput == 'o')
{outputRoster(&jerseyNumbers[10], &playerRatings[10]);
}
else if (menuInput == 'q')
printf("Reached Quit");
else
printf("Input Error\n");
}
int main()
{
int *jerseyNumbers[10];
int *playerRatings[10];
char menuInput;
int quitFlag = 0;
getRoster(&jerseyNumbers[10], &playerRatings[10]);
printf("%d %d", jerseyNumbers[0], playerRatings[1]);
menu(&jerseyNumbers[10], &playerRatings[10]);
return 0;
}
Refer to #NaveenKumar and #DeiDei's comment to your question.
Actually, the logic of your program is alright. But where you've gone wrong is the syntax regarding how you have declared the arrays and passed them as arguments to the functions. I have listed the changes to be made below:
First of all Dylan, do not declare an array as: int
*jerseyNumbers[10] and int *playerRatings[10]. For the regular array which you actually need, just declare as:
a. *jerseyNumbers and *playerRatings... Remove the size. OR
b. jerseyNumbers[10] and playerRatings[10]... Remove the * from the declaration.
When you're passing these arrays as arguments to a function, don't ever send it how you've done. Just pass the array name as an argument. Like this: getRoster(jerseyNumbers, playerRatings); and menu(jerseyNumbers, playerRatings);.
In the function definition, the parameters representing the array should be either *arrayname or arrayname[size]. Since we have used the first during declaration, use the same here as follows:
void getRoster(int *jerseyNumbers, int *playerRatings) {...},
void updateRating(int *jerseyNumbers, int *playerRatings){...},
void outputRoster(int *jerseyNumbers, int *playerRatings) {...},
void menu(int *jerseyNumbers, int *playerRatings) {...}.
And as #PaulSm4 has suggested don't use the semi-colon at the end of an if conditional. Though this isn't the cause of your problem here, it is a practice you need to follow and mistake to be avoided.
I have attached the working code below, along with the output.
CODE:
#include <stdio.h>
#include <stdlib.h>
void getRoster(int *jerseyNumbers, int *playerRatings)
{
int i;
for (i=0; i<5; ++i)
{
printf("Enter player %d's jersey number\n", i+1);
scanf("%d", &jerseyNumbers[i]);
printf("Enter player %d's rating\n", i+1);
scanf("%d", &playerRatings[i]);
}
}
void updateRating(int *jerseyNumbers, int *playerRatings)
{
int i, n = -1, hold;
printf("Enter a jersey number:\n");
scanf("%d", n);
for (i=0; i<5; ++i)
if(jerseyNumbers[i]==n)
hold = n;
if (n=-1)
printf("Error");
printf("Enter a new rating for player:\n");
scanf("%d", &playerRatings[n]);
}
void aboveRating()
{
}
void replacePlayer()
{
}
void outputRoster(int *jerseyNumbers, int *playerRatings)
{
int i;
for (i=0; i<5; ++i)
{
printf("Player %d -- Jersey number: %d , Rating: %d \n", i+1, jerseyNumbers[i], playerRatings[i]);
}
}
void menu(int *jerseyNumbers, int *playerRatings)
{
char menuInput;
printf("\nMENU\n");
printf("u - Update player rating\n");
printf("a - Output players above a rating\n");
printf("r - Replace player\n");
printf("o - Output roster\n");
printf("q - Quit\n");
printf("\nChoose an option:\n");
scanf(" %c", &menuInput);
if (menuInput == 'u')
{updateRating(jerseyNumbers, playerRatings);
}
else if (menuInput == 'a')
{aboveRating();
}
else if (menuInput == 'r')
{replacePlayer();
}
else if (menuInput == 'o')
{outputRoster(jerseyNumbers, playerRatings);
}
else if (menuInput == 'q')
printf("Reached Quit");
else
printf("Input Error\n");
}
int main()
{
int jerseyNumbers[10];
int playerRatings[10];
char menuInput;
int quitFlag = 0;
getRoster(jerseyNumbers, playerRatings);
printf("%d %d", jerseyNumbers[0], playerRatings[1]);
menu(jerseyNumbers, playerRatings);
return 0;
}
OUTPUT:
Enter player 1's jersey number
1
Enter player 1's rating
10
Enter player 2's jersey number
2
Enter player 2's rating
20
Enter player 3's jersey number
3
Enter player 3's rating
30
Enter player 4's jersey number
4
Enter player 4's rating
40
Enter player 5's jersey number
5
Enter player 5's rating
50
1 20
MENU
u - Update player rating
a - Output players above a rating
r - Replace player
o - Output roster
q - Quit
Choose an option: o
Player 1 -- Jersey number: 1 , Rating: 10
Player 2 -- Jersey number: 2 , Rating: 20
Player 3 -- Jersey number: 3 , Rating: 30
Player 4 -- Jersey number: 4 , Rating: 40
Player 5 -- Jersey number: 5 , Rating: 50
Hope this helps.
STRONG SUGGESTION:
Get in the habit of using curly braces often - even when you don't need them.
void updateRating(int *jerseyNumbers, int *playerRatings)
// You probably only need a pointer or an array - but probably not both ;)
{
int i, n = -1, hold;
printf("Enter a jersey number:\n");
scanf("%d", n);
for (i=0; i<5; ++i) { // Curly brace here...
if(jerseyNumbers[i]==n) { // And here...
hold = n;
}
}
if (n=-1) { // BUG ALERT: deleted ";" (and added a brace)
printf("Error"); // BUG ALERT: fixed this by removing ";"?
} else {
printf("Enter a new rating for player:\n");
scanf("%d", &playerRatings[n]);
}
Or perhaps better:
void updateRating(int *jerseyNumbers, int *playerRatings)
{
int n;
printf("Enter a jersey number:\n");
scanf("%d", n);
printf("Enter a new rating for player:\n");
scanf("%d", &playerRatings[n]);
}
And, as Rishikesh Raje correctly pointed out:
The definition of the array's and the function calls for OP is also
not correct. Please mention this also.

How do I print outputs for two inputs at the same time?

Code:
#include <stdio.h>
int main()
{
int w,x,y,z;
float v;
printf("Enter the driver salary\n");
scanf("%d",&x);
printf("Enter the car mileage in km per litre\n");
scanf("%d",&y);
printf("Enter the cost of petrol per litre\n");
scanf("%d",&z);
printf("Enter the taxi fare for a km\n");
scanf("%d",&w);
printf("Enter the distance of travel\n");
scanf("%f",&v);
if(w==200 && y==10 && z==60 && x== 20 && v==10.5)
printf("Minimal cost travel is by taxi\n");
else
printf("Minimal cost travel is by audi\n");
return 0;
}
For two different set of values of inputs for w,y,z,x,v, I need to print both output statements at same time. I am getting first output, but how do I get two outputs at the same time?
If you want to output at same time have a flag array to store results.Also you may want to store the value of 10.5 in a float.Read more.I added a few lines in your code check if it works :
#include<stdio.h>
int main()
{
float l=10.5; //to be safe about float rounding up
int i,fl[2]; //stores results for output
for(i=0;i<2;i++) //add this
{
int w,x,y,z;
float v;
printf("Enter the driver salary\n");
scanf("%d",&x);
printf("Enter the car mileage in km per litre\n");
scanf("%d",&y);
printf("Enter the cost of petrol per litre\n");
scanf("%d",&z);
printf("Enter the taxi fare for a km\n");
scanf("%d",&w);
printf("Enter the distance of travel\n");
scanf("%f",&v);
if(w==200 && y==10 && z==60 && x== 20 && v==l)
fl[i]=1;
else
fl[i]=0;
}
for(i=0;i<2;i++)
{
if(fl[i]==1)
printf("Case %d : Minimal cost travel is by taxi\n",i+1);
if(fl[i]==0)
printf("Case %d : Minimal cost travel is by audi\n",i+1);
} //close braces
return 0;
}
You need to wrap the core functionality in a for/while loop. I am going to suggest putting the core functionality in a function too.
void processInput()
{
int w,x,y,z;
float v;
printf("Enter the driver salary\n");
scanf("%d",&x);
printf("Enter the car mileage in km per litre\n");
scanf("%d",&y);
printf("Enter the cost of petrol per litre\n");
scanf("%d",&z);
printf("Enter the taxi fare for a km\n");
scanf("%d",&w);
printf("Enter the distance of travel\n");
scanf("%f",&v);
if(w==200 && y==10 && z==60 && x== 20 && v==10.5)
printf("Minimal cost travel is by taxi\n");
else
printf("Minimal cost travel is by audi\n");
}
int main()
{
int i;
for ( i = 0; i < 2; ++i )
{
processInput();
}
return 0;
}

Resources