rteurning a value for function that i dont whant to

my problem is that i have declared aprototype with returning value as float
then inside the function i sayed the if a condetion satisfy .read som data and then return it
else compleat the loop

at the end of the function i must return avalue but i dont know what to return
this is part of the code :
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
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

return  ; }//end of the function 


is there any one here can guide me i dont know whaht to do>.<
Last edited on
The code on line 30 of your function will be executed only in case that the opened file doesn't contain the code you're looking for (at least on its first 13 lines - according to your condition x<=13).

This state is obviously an error (similar to the one you test on line 9). Thus you can simply return 1; what (if I'm not mistaken) was your intention for indicating errors.
the purpes of this function is to retun the price of the code
like
01-AB-20 10.0 //the file contains number of codes and there prices
....
....
....

so what i did is wrong in line 30 i did that because i didnt know what to put
since the function is avalue returning function.
in the task that i want to solve they want us to print abill for customers we know what are the code and how many quntity did they bought so the we will serch for the code in some file and then read the price as iv showed up
i can simply use built in function find() but i dont know how to use it kind of new.......
Let's clear the matter up.

There are three return statements in your function (three branches). One of the branches is valid (according to your criteria) returning uniteprice, the other two are invalid. These two should return some value indicating an error (often is used -1).

You can then test the value e.g.
1
2
float Result =  FindProduct(SomeString);
if (Result==-1) { /* appropriate action */ }
Topic archived. No new replies allowed.