File Data Won't Display

Hello, I am completely new to coding as an entirety and don't particularly have the best professor teaching me. I am trying to create a program that helps a restaurant use an automated billing system. The menu data comes from a text file that I have created in my projects folder. I am trying to just get the text file data to display, but I cannot get it to do so for whatever reason. I don't want anyone to write the code for me so I'm hesitant to post the assignment requirements. I barely have anything right now, but I would greatly appreciate it if someone could explain to me why my data won't display.

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <istream>
#include <string>
#include <conio.h>


using namespace std;

int main()
{
	std::ifstream infile("MenuData.txt");

	const int choice = 8;
	bool flag = bool();
	int selection = 0;


	string itemName[8];
	double itemPrice[choice];
	int itemNum = 0;

	infile >> itemName[itemNum] >> itemPrice[itemNum];

	while (infile && itemNum < 8)
	{
		itemNum++;
		infile >> itemName[itemNum] >> itemPrice[itemNum];
	}

	while (true)
	{
		system("cls");
		cout << "Welcome to Johnny's Restaurant" << endl;
		cout << "-------------MENU-------------" << endl;
		cout << itemName[itemNum] << itemPrice[itemNum];
	}
	return 0;
}
You are both reading and attempting to write beyond the end of the arrays.

Line 26 is passed when itemNum gets to 7, .... then line 28 increases itemNum to 8, ... and you try to read one beyond the end of the array.

The loop starting on line 32 loops forever (provided it works out what it can do with the non-existent array elements on line 37, with itemNum now pointing beyond the end of the array).

Don't use conio.h. It isn't standard c++ and I can't see anything remotely needing it.
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
#include <iostream>
#include <fstream>
#include <string>

int main()
{
	std::ifstream infile("MenuData.txt");

	const int choice = 8;
	bool flag = bool();
	int selection = 0;


	std::string itemName[choice];
	double itemPrice[choice];
	size_t itemNum = 0;

	infile >> itemName[itemNum] >> itemPrice[itemNum];

	while (infile && itemNum < choice)
	{
		itemNum++;
		infile >> itemName[itemNum] >> itemPrice[itemNum];
	}

	for (size_t i = 0; i < itemNum; ++i)
	{
		system("cls");
		std::cout << "Welcome to Johnny's Restaurant" << std::endl;
		std::cout << "-------------MENU-------------" << std::endl;
		std::cout << itemName[i] << itemPrice[i] << std::endl;
	}
}
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
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
//	ifstream infile("MenuData.txt");
	istringstream infile( "Pies  2.50 \n"
	                      "Chips 1.90 \n"
	                      "Beans 0.45 \n"
	                      "Ham   2.20 \n"
	                      "Rice  1.80 \n"
	                      "Eggs  1.45 \n"
	                      "Peas  0.45 \n"
	                      "Sauce 0.30 \n" );

	const int choice = 8;
	string itemName[choice];
	double itemPrice[choice];
	
	int itemNum = 0;
	while ( itemNum < choice && infile >> itemName[itemNum] >> itemPrice[itemNum] ) itemNum++;

	cout << "Welcome to Johnny's Restaurant\n";
	cout << "-------------MENU-------------\n";
	cout << fixed << setprecision( 2 );
	for ( int i = 0; i < itemNum; ++i)
	{
		cout << itemName[i] << '\t' << itemPrice[i] << '\n';
	}
}
Last edited on
Topic archived. No new replies allowed.