Trying to output data into a text file.. please help me?

I'm supposed to write a function that will allow the user to save information into a text file (.dat i believe), however i'm having some dificulties. I have the rest of my program coded and compiling, only this function is still not working properly. I've included my code, as well as the instructions given to me in case I'm not very clear on what i'm trying to do.

INSTRUCTIONS AS GIVEN TO ME:

Mr. Muzik owns Fantastic Music Club that sells CDs by membership. He has four customers and would like you to create a program that will help him to keep track of the CD sales. All members of Muzik CD club must purchase an average of two CDs per year in order to qualify. An annual report is run at the end of year to help Mr. Muzik determines who haven’t fulfilled the volume requirement.

ID Number of Year Number of CD bought
M123 2 3
M225 1 6
M248 2 1
M552 3 5

Define a structure to store the ID, Number of Year, and Number of CD. Then, use array of structures to store all four lines of data given above.

Your program will have a menu of two choices: (A) Display annual report for all customers to the screen, and (B) Write the array of structures into a text file.

Put program for menu choice (A) and (B) into functions. Both functions will pass in an array of structures and the size of the array, no return value is required in both cases.

Function A:
The function will output a report that contains ID, Number of Year, Number of CD bought, and number of CD they still have to purchase.

Function B:
Output the data in the array of structures into a space delimited text file using for loop(s). Prompt the user for file name.


AND HERE IS MY PROGRAM CODE WITH FAULTY TEXT FILE FUNCTION:

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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#include <fstream>
#include <iostream>
#include <stdlib.h> //Or, #include <cstdlib>
#include <iomanip>

using namespace std;

struct cust_struct                 //create structure for each row of table
{
       char cust_id[5];
       int num_year;
       int num_bought;
};

//disp_report function prototype
void disp_report(cust_struct [], int);

//function prototype to write table to file
void report_file(cust_struct [], int);

     

int main()
{
    char goagain = 'y';
    const int table_size = 4;
    char selection;
     //create table of cust_struct arrays
    cust_struct cust_table[table_size] = { {"M123", 2, 3},
                                           {"M225", 1, 6},        
                                           {"M248", 2, 1},
                                           {"M552", 3, 5}
                                          } ; 
     while (goagain == 'y')
     {
      cout << "\n\nFANTASTIC MUSIC CLUB\n"
           "============================\n\n"
           "CUSTOMER TRACKING PROGRAM\n"
           "----------------------------\n\n"
           "Select From The Options Below:\n\n"
           "A) Display Customer Report\n"
           "B) Save Report To File\n\n"
           "Enter The Letter Corresponding to Your Choice:\n";
           cin >> selection;
           
           switch (selection)
           {
                  default:
                            cout << "Invalid Entry";
                            
                         break;
                  case 'a':
                  case 'A':
                       disp_report(cust_table, table_size);
                       break;
                       
                  case 'b':
                  case 'B':
                        report_file(cust_table, table_size);
                        break;
           }
           cout << "\n\nWould You Like To Make Another Selection?(y/n)\n";
           cin >> goagain;
      }
    

  return 0;
}//END OF MAIN()


void disp_report(cust_struct cust_table[], int size)
{
     int row;
     int required;
     

cout << endl << endl;
cout << setw(5) << "Customer ID"
     << setw(20) << " # Years As Member"
     << setw(20) << " # CD's Bought"
     << setw(25) << " # CD's Still Required"
     << endl
     <<"----------------------------------------------------------------------------";
     
for(row = 0; row < size; ++row)
{
        required = (cust_table[row].num_year * 2) - cust_table[row].num_bought;
        if(required < 0)
        {required = 0;}
        
        cout << endl;
        cout << setw(8) << cust_table[row].cust_id
             << setw(14) << cust_table[row].num_year
             << setw(22) << cust_table[row].num_bought
             << setw(22) << required;
}
}//END OF DISP_REPORT()

void report_file(cust_struct cust_table[], int size)
{
     char file_name[81];
      int row;
  
  
  cout << "\nEnter the name of the new customer report file: ";
  cin.getline(file_name, 81);

  ofstream inventory_file(file_name);

  if (!inventory_file)
	 {
		cout << "\nERROR: File could not be opened.";
		
	 }

  inventory_file << setprecision(2)
		          << setiosflags(ios::fixed)
		           << setiosflags(ios::showpoint);

	 //Write inventory record
	 for(row = 0; row < size; ++row)
     {
     inventory_file << cust_table[row].cust_id << " " << cust_table[row].num_year << " " 
     << cust_table[row].num_bought << endl;
      
      }
  cout << "\n\nInventory File " << file_name << " created.";
  inventory_file.close();
}//end of REPORT_FILE 


Thanks Again for any help that anyone is able to give me
Hi,
U need to fflush the stdin, before asking user input.

fflush(stdin);

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
void report_file(cust_struct cust_table[], int size)
{
     char file_name[81];
      int row;
  
  
  cout << "\nEnter the name of the new customer report file: ";
fflush(stdin);
  cin.getline(file_name, 81);

  ofstream inventory_file(file_name);

  if (!inventory_file)
	 {
		cout << "\nERROR: File could not be opened.";
		
	 }

  inventory_file << setprecision(2)
		          << setiosflags(ios::fixed)
		           << setiosflags(ios::showpoint);

	 //Write inventory record
	 for(row = 0; row < size; ++row)
     {
     inventory_file << cust_table[row].cust_id << " " << cust_table[row].num_year << " " 
     << cust_table[row].num_bought << endl;
      
      }
  cout << "\n\nInventory File " << file_name << " created.";
  inventory_file.close();
}//end of REPORT_FILE


Hopefully this will solve ur problem.
Thnx,
Diptendu
thanks, that took care of it. appreciated.
Topic archived. No new replies allowed.