Do not know how....?

I have this file(menu of a restaurant) and i want to read it and then display it as a regular menu....any ideas please??


8
Plain Egg
0.45
Bacon and Egg
2.45
Muffin
0.99
French Toast
1.99
Fruit Basket
2.49
Cereal
0.69
Coffee
0.50
Tea
0.75


Example:

1-Cereal $0.69
2-Coffe $0.50
....
Any help is appreciated.
Thanks in advance


Last edited on
A File Stream object would be a good start: http://www.cplusplus.com/reference/iostream/ifstream/

How lost are you? Do you think the tutorial on this site might help? http://www.cplusplus.com/doc/tutorial/files/

Why does this file start with an '8'? That is seriously messing with me right now.
Why does this file start with an '8'? That is seriously messing with me right now.


Looks like the number of menu items.
AH! THANK YOU!!! That's an interesting trick if you aren't confident in your ability to keep track of dynamic arrays. It's definatly worth learning how to use "std::vector" though.
i am suposed to use dynamic array, but truth be told i have no idea how.
somehow i am supposed to get the 8 which makes an array size 8 like the menu choices.

I am able to open the file
but i do not know how to get the different lines and different data types(the price in this case)
and how to display it as a Regular Menu like the example up.

1
2
3
4
5
6
7
8
9
10
11
12
13
ifstream openFile;
string menuList;

openFile.open("menu.txt")

if (!openFile is.open())

    cout << "Did not open" << endl;

getline(openFile, menuList)

cout << menuList << endl;
I'll assume you're using the std namespace.

- Remember Line 4 is using a path relative to you PATH environment variable (Your current execution path).

- Line 6 should be more like if(!openFile.is_open()) Reference: http://www.cplusplus.com/reference/iostream/fstream/is_open/

- On Line 10, this version of "getline(...)" is a member function of the 'ifstream' objects: http://www.cplusplus.com/reference/iostream/istream/getline/
So it's more like: openFile.getline(/*You Tell Me What Goes Here?*/);

- If you want your price displayed on the same line as your item you need to modify Line 12.

EDIT: - This cannot be called a Dynamic Array and your teacher should be made to to sit in a corner with their nose to the wall for saying such rot. But you need to make menuList into an array of strings if this is what you plan on accomplishing.
Last edited on
That's an interesting trick if you aren't confident in your ability to keep track of dynamic arrays.


It also makes memory allocation more efficient. If you know how much data there is you can reserve enough space ahead of time instead of possibly having to reallocate several times.
Thanks for taking your time.
I appreciate it a lot.

i want to show what i got so far and i do know it is NOT a lot.

I do need some hand or a guide in the right direction, please.

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

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

const double taxRate = 0.05;

struct menuItemType
{
	string menuItem;
	double menuPrice;
};

void openFile();
void showMenu( menuItemType mList[], int listSize);
void getData();
void makeSelection();//loads the customers selection into an orderList[]


int main()
{
	ifstream inFile;
                string menuList;

	menyItemType *menuList;

	int *orderList;// this is supposed to be an array
	int numOfItems;// this will be 8 i assume from the 8 in the file.txt

	inFile.open("menu.txt");
                if (!inFile.is_open())
		cout << "Did not open" << endl;

	getline(inFile, numOfItems)
	inFile << numOfItems;//i want to get the 8 from menu

	menuList = new menuItemType [numOfItems];
	orderList = new int [numOfItems];



}



Thanks again for the time
Last edited on
?
Last edited on
- Double check line 28. There are too things rong there. <- My Attempt At Being Subtle

- "menuList" still isn't an array.

- If Line 34, then you should do something to prevent the rest of the code from executing. A simple thing would be to return from the program.

- Line 37 is missing something *HINT*-> ';' <-*HINT*

- You need a loop to read through the items in the file.

- You never set the value for numOfItems. There are a number of ways to do this but the simplist way would be to grab the first entry in the file, cast it as an 'int' and assign that to numOfItems.
Last edited on
Topic archived. No new replies allowed.