i am trying to run a programme implementing a function with structures in c... which is: -
#include<stdio.h>
-
#include<conio.h>
-
struct store
-
{
-
char name[20];
-
float price;
-
int quantity;
-
};
-
-
struct store update (struct store product, float p, int q);
-
-
float mul(struct store stock_value);
-
-
-
main()
-
{
-
int inc_q;
-
float inc_p,value;
-
-
struct store item = {"xyz", 10.895 ,10};
-
-
/*this is where the problem lies, when i am initializing this store item, the values are not being stored by the members i.e. ater this line the members (1)item.name should be "xyz",(2)item.price should be 10.895, (3)item.quantity should be 10; but except the item.name=xyz other members take a garbage value of their own*/
-
-
printf("name = %s\n price = %d\n quantity = %d\n\n",item.name,item.price,item.quantity);
-
-
printf("enter increment in price(1st) and quantity(2nd) : ");
-
scanf("%f %d",&inc_p,&inc_q);
-
-
item = update(item,inc_p,inc_q);
-
-
printf("updated values are\n\n");
-
printf(" name = %d\n price = %d\n quantity = %d",item.name,item.price,item.quantity);
-
-
value = mul(item);
-
-
printf("\n\n value = %d",value);
-
}
-
struct store update(struct store product, float p, int q)
-
{
-
product.price+=p;
-
product.quantity+=q;
-
return(product);
-
}
-
float mul(struct store stock_value)
-
{
-
return(stock_value.price*stock_value.quantity);
-
}
some one please help to remove the error in my code.. reply soon
1 1233
This code: - struct store item = {"xyz", 10.895 ,10};
-
10.895 is a double whereas the struct needs a float. Either change to double in the struct or code 10.905F. Otherwise you get a warning "conversion double to float possible loss of data".
Also, your update function takes a store argument which means the compiler generates a copy for the function to work with. It is this copy that is updated and deleted when the function completes. You need a store* here instead so you update the original variable and not the copy.
I changed that and everything compiled OK. I did not test anything.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: lallous |
last post by:
Hello,
Why is the static variable inside a member function static for all instances
and not static for the current instance of the object?
--
Elias
|
by: Fred Ma |
last post by:
Are there any reasons that would make it bad for C++ to
allow simultaneous declaration and initilization of member data?
Current way:
------------
class DerivedClass : BaseClass {
{
enum {...
|
by: Davor |
last post by:
How to define global variable in main()?
I'm asking because I have an array in main, whose size is determined by
input, so the definition has to be in main ( or in some other funcion ).
And I need...
|
by: MattFit |
last post by:
I just downloaded the LCC-Win Compiler and wrote the following program:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv) {
printf("Hello World\n");
return 0;
}
|
by: Mikko Penkkimäki |
last post by:
I have a struct in C#-project and C++-project. I use the struct in C# but it
has to be filled in C++.
I know that arrays work like this:
In C++ code:
struct teststruct {
char str;
float...
|
by: nospam |
last post by:
Hello All,
Is it possible to place a variable inside of a textbox and have it
viewed as the value? Or even read in as a string. Here's the
problem... I have a text file I would like to read in...
|
by: Boni |
last post by:
Dear all,
how can I initialize shared variable?
i.e
class A
private shared B as boolean
end class
in C++ I would write in the global scope A::B=true
What is the proper way in vb
|
by: lovecreatesbea... |
last post by:
Does the expression *(int *)&s1 below inside the printf() statement
guarantee to refer to the first member of the structure variable s1?
I've tried the code and it seems that it works that way. The...
|
by: Dheeraj Joshi |
last post by:
Hi, I was wondering is there any technique available, so we can access the global variable inside a function if we have a local variable inside the function with the same name as global variable.
...
|
by: Sajini Biju |
last post by:
Can I get a structure variable in function
for eg:- struct stud{
char name}stud1;
void student(char);
void main()
{
char n;
scanf("%s",n);
strcpy(stud1.name,n);
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |