Input from a file.

I have a file I am trying to read its parts as a struct. the first part being the name of the Item (a string) and the second part being the price (a double).
my conundrum is I get the string to be read but the double doesn't identify as valid. My question is how do I set it up so the program will read the string and input the double. mainly how do I make line 64 work without screwing up the rest of the program

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <iostream>
#include <string>
#include <fstream>

using namespace std;


const int MAXSIZE= 256;

struct menuItemType 
{
	string menuItem [MAXSIZE] ;
	double menuPrice[MAXSIZE];
}itemlist ;

int  initializeFile ();
void showAll (int lineCount);
int menu ();
double actions (int response);
int main ()
{
	int totalLines;
//	int tempItem;
//	int tempPrice;

totalLines = initializeFile ();

showAll(totalLines);
 cout<< "\n\n what would you like to order?";
menu ();
	system("pause");

	return 0;
}

int initializeFile ()
{
	ifstream inFile;
	int index=0;
	string item; 
	double price = 0;
	int lineCount=0;
	inFile.open("menuItems.txt");
	

    while (!inFile.eof())
{		
			getline(inFile, item );
			lineCount ++; 
}
	
	inFile.close();
	inFile.open("menuItems.txt");
	
	while (!inFile.eof())
	{
		for (index= 0; index <lineCount; index++ )
		{
			
			 getline(inFile,item);
			itemlist.menuItem[index] = item;
		
			
		 price = inFile.get();
			itemlist.menuPrice[index]= price;
			
		}
	
	}
	
 return lineCount;
}

void showAll (int lineCount)
{
	int index =0;
	int  totalitems = lineCount/2;
	for (index= 0; index <totalitems; index =index++ )
		{
			cout <<"item#"<< index << " "<< itemlist.menuItem[index] << endl;
			cout << itemlist.menuPrice[index] << endl;
		}
}

int menu ()
{
int response;
cout<< "\n\n what would you like to order?";
cin >>response;
return response;
}

double actions (int response)
{
	double tempprice= itemlist.menuPrice[response];

	cout << "you ordered a" << itemlist.menuItem[response];
	cout << "price was " << itemlist.menuPrice[response];

	return tempprice;
}


get(), IIRC, reads a single character from the file which I don't think is what you want. You can read using the extraction operators >> into the double or use getline() to read into a temporary string then convert it to a double.
makes sense but what is the command to convert a string to a double aren't they incompatible
http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2


1
2
3
   std::istringstream i(someString);
   double x;
    i >> x;


Topic archived. No new replies allowed.