Writing to an Output File

Hello, I have my whole program complete except I need to write everything to the screen as well as to an output file names OutMenu.txt. I have everything already writing to the screen but I am stuck trying to get it to write the txt file. I keep only getting it to write the last part of the program to the txt file. I deleted everything that I have tried so far because it became extremely messy and I figured you guys would be able to help me better with a cleaner code.

As always, thank you for your time!

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
102
103
104
105
106
107
108
109
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;


struct menuItemType 
{
    string itemName;
    double itemCost;

};

void getData(menuItemType menu[]);
void showMenu(menuItemType menu[]);
void printCheck(menuItemType menu[]);

int main()
{				

cout<< "          Welcome to Out Diner!         " << endl;
cout<< "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout<< "  *To select an Item from the list below,  " << endl;
cout<< "just indicate the Item number for the item \n\n" << endl;


    menuItemType menu [8];
    getData(menu); 
    showMenu(menu);

    system("pause");

    return 0;
}

void getData(menuItemType item[]) 
{
    int i=0;
    ifstream inData;
    inData.open("InMenu.txt");
    cout << fixed << setprecision(2);

    while(!inData.eof()) 
	{
        inData >> item[i].itemName >> item[i].itemCost;
        ++i;
    }
}

void showMenu(menuItemType item[]) 
{
    int choice = 0;
    double total = 0.0, totalF = 0.0, tax = 0.0;
    char exit = 'y';
    int j = 1, z = 1, i = 1;

    for (int i=0; i<8; i++)
	{
        cout << j << ". " << setw(18) << left << item[i].itemName << "$" << setw(10) << item[i].itemCost << endl;
        j++;
		
    }
		
    }

    cout << endl;

    while(exit == 'y' || exit == 'Y') 
	{

        cout << "Please Enter your Selection or 0 to Exit : ";
        cin >> choice;

        if(cin.fail()) {
            cout << "~~~~~~~~~Invalid selection~~~~~~~~~" << endl;
            cin.clear();
            cin.ignore(1000,'\n');
        } 
		else if (choice==0) {break; }
            else 
			{
                choice--;
                total += (item[choice].itemCost);
                tax = (total * .05);
                totalF = total + tax;
                cout << endl;
            }
          cout << endl;

          cout << item[choice].itemName << " " << item[choice].itemCost << endl;
          cout << "======================================================" << 
          cout << "Do you want to continue (Y/N): ";

			printCheck;
        }
void printCheck (menuItemType item[]);
 {
         cout << "\n\nThanks for Dinning With Us!" << endl;
	 cout << "<><><><><><><><><><><><><><>\n" << endl;
	 cout << "Customer Receipt            " << endl;
	 cout << "SubTotal:               $" << total <<endl;
	 cout << "Tax:                    $" << tax << endl;
	 cout << "                        -----" << endl;
	 cout << "Total:                  $" << totalF << endl;
	 cout << "Please Come Again!" << endl;
 }
}
http://www.cplusplus.com/doc/tutorial/files/

dont know what your problem is. either you close the file after each output, so the next time you start at the beginning again or, if you have to close it, you are not using stream pointers.
if you comment out line 66, it almost compiles.
line 93 needs terminator

Looks like the error after that points to line 96 which I think should be
printCheck(somevalue);

If you comment out line 96, it compiles for me but don't know what you expect it to do at that point.

Use the link by darkmaster to look up how to use ofstream.


I'm surprised the code even compiles.
Erase the "}" line 66.
Sorry guys, I copy and pasted my code into here and then deleted the messiness and all the things I tried/commented out so I messed something up on here when I erased it, my code which compiles is below.
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
102
103
104
105
106
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;


struct menuItemType 
{
    string itemName;
    double itemCost;

};

void getData(menuItemType menu[]);
void showMenu(menuItemType menu[]);
void printCheck(menuItemType menu[]);

int main()
{

cout<< "          Welcome to Our Diner!         " << endl;
cout<< "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout<< "  *To select an Item from the list below,  " << endl;
cout<< "just indicate the Item number for the item \n\n" << endl;

    menuItemType menu [8];
    getData(menu); 
    showMenu(menu);

    system("pause");

    return 0;
}

void getData(menuItemType item[]) 
{
    int i=0;
    ifstream inData;
    inData.open("InMenu.txt");
    cout << fixed << setprecision(2);

    while(!inData.eof()) 
	{
        inData >> item[i].itemName >> item[i].itemCost;
        ++i;
    }
}

void showMenu(menuItemType item[]) 
{
	int choice = 0;
    double total = 0.0, totalF = 0.0, tax = 0.0;
    char exit = 'y';
    int j = 1, z = 1, i = 1;

    for (int i=0; i<8; i++)
	{
        cout << j << ". " << setw(18) << left << item[i].itemName << "$" << setw(10) << item[i].itemCost << endl;
        j++;
		
    }

    cout << endl;

    while(exit == 'y' || exit == 'Y') 
	{

        cout << "Please Enter your Selection or 0 to Exit : ";
        cin >> choice;

        if(cin.fail()) {
            cout << "~~~~~~~~~Invalid selection~~~~~~~~~" << endl;
            cin.clear();
            cin.ignore(1000,'\n');
        } 
		else if (choice==0) {break; }
            else 
			{
                choice--;
                total += (item[choice].itemCost);
                tax = (total * .05);
                totalF = total + tax;
                cout << endl;
            }
            cout << endl;

            cout << item[choice].itemName << " " << item[choice].itemCost << endl;
            cout << "======================================================" << endl;
            cin >> exit;

			printCheck;
        }
void printCheck (menuItemType item[]);
 {
         cout << "\n\nThanks for Dinning With Us!" << endl;
	 cout << "<><><><><><><><><><><><><><>\n" << endl;
	 cout << "Customer Receipt            " << endl;
	 cout << "SubTotal:               $" << total <<endl;
	 cout << "Tax:                    $" << tax << endl;
	 cout << "                        -----" << endl;
	 cout << "Total:                  $" << totalF << endl;
	 cout << "Please Come Again!" << endl;
 }
}
Last edited on
Topic archived. No new replies allowed.