function argument >.<

hi there :
i dont know if this a big mistake to give afunction atype and put another type in it argumen but i want to pass astring to function and return float value
but it didnt work the erorr is:'initializing' :cannot convert from 'std::string to 'float' thhe erorr in line 43

can any one explain for me what wrong that i made^.^
this is the code if needed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
float FindProduct(string code)
 {
	 //declaring local variabels
	 string codes;
	 float uniteprice;
	 ifstream storeF;
	 //open storfile and cheaking wether it opens or no
	 storeF.open("StoreFile");
	 if (storeF.fail())
	 {
		 cout <<"cannot open store file or it dose not exist"<<endl;
		 return 1;
	 }
	 //reading the first code in store file
	 storeF>>codes;
	 for(int x=1;x<=13;x++)
	 {
		 if (code == codes)
		 {
			 //read price of that code
			 storeF>>uniteprice;
			 storeF.close();
			 return (uniteprice);
		 }
		 else
			 //read the next code
			 storeF >>uniteprice>>codes;
	 }//end of for loop

 }//end of the function

 void Customer(string Name)
 {
	 //declaring output file and locak variabels
	 string custCode;
	 int qunt;
	 double subtotal,total;
	 float uniteprice;

	 for(int ii=1;ii<=3;ii++)
	 {
		 custFile>>custCode>>qunt;
		 float FindProduct(custCode);
		 //calculating the subtotal
		 subtotal=qunt*uniteprice;
		 //formating the output

		 outBill<<setw(24) <<ii<<"      "<<custCode<<"       "<<setprecision(2) << uniteprice <<setw(10)<<qunt <<setw(20) 
	     << setprecision(2) << subtotal<<endl<<endl;

		 total=total+subtotal;
	 }//end of for loop 
	 outBill<< "The total is :  " << total << " Thank you" <<endl<< endl;

	 return;
 }//the end of this function
	

thanks from deeb of my hart^___^
Last edited on
FindProduct() is a function so you can't call it in this way:

float FindProduct(custCode);

What you've actually done in this line is a declaration of variable FindProduct (of type float) and initializing it with the value custCode (which is not a float hence the error).

I guess you want to assign the result of the function to some variable, e.x.

float Result = FindProduct(custCode);
oh, i can see know
so, in the prototype of function FindProduct i must make it by the type string

i have another qustion which it make me confuse after finding the code it will get the price of item
and here i will return the price as i did in the code and the assignment operator will help me to resrve the value of the price <is this what you mean> can i do that
Yeah, no changes to the function itself are necessary.

float Result = FindProduct(custCode);
means: take the result of function FindProduct (which is of float type) and assign it to the variable Result.

Now the result is stored in Result and you can do whatever you want with it.
1) use it in an expression: ex. AnotherResult = Result * 2;
2) display it on your screen: ex. cout << Result;
3) and a thousand of other ways
ok thank you verey much
know i understood how things are working out
best wiches to you ^-^
Topic archived. No new replies allowed.