fstream

I need to do a system to proceed a booking of two badminton courts. Basically, i need to create a program that input data and read it out. I have just learn C++ and really need lots of help. Can anyone help me see what's wrong with my program?

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class Booking
{
private:
int date;
string name;
int ic;
int hpno;
int selection;
public:
void enterDetails ()
{
cout<<" ~~Welcome to Court booking~~~\n\n\n ";
cout<<"Select your court 1 for B1 or 2 for B2.\n\n";
cin>> selection;
fflush(stdin);
cout<< "Enter your Name: \n";
getline(cin , name);
cout<< "Enter your NRIC number without S and Last letter: \n";
cin>> ic;
cout<< "Enter your telephone number: \n";
cin>> hpno;
};
void showDetails (void)
{
// reading a text file


string line;
ifstream myfile ("courtbooking.txt");
if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl;
}
myfile.close();
}

else cout << "Unable to open file";

return 0;
};

int main()
{
int date;
string name;
int ic;
int hpno;
int selection;
Booking details;

details.enterDetails();
ofstream myfile ("courtbooking.txt");
if (myfile.is_open())
{
myfile << "Name:"<<name<<endl;
myfile << "Ic:"<<ic<<endl;
myfile << "Contact:"<<hpno<<endl;
myfile.close();
}
else cout << "Unable to open file";

details.showDetails();

return 0;
}


You had two mistakes in lines 44 and 45.
At line 44 you used return 0; but the function is type void, which means that you can't return a value. If you just want to end the function and return to the main program you have to use return;
At line 45 you forgot to close the brackets for the void showDetails (void)
I have your code here with those corrections so you can see them
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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class Booking
{
private:
   int date;
   string name;
   int ic;
   int hpno;
   int selection;
public:
   void enterDetails ()
   {
      cout<<" ~~Welcome to Court booking~~~\n\n\n ";
      cout<<"Select your court 1 for B1 or 2 for B2.\n\n";
      cin>> selection; 
      fflush(stdin);
      cout<< "Enter your Name: \n"; 
      getline(cin , name);
      cout<< "Enter your NRIC number without S and Last letter: \n";
      cin>> ic;
      cout<< "Enter your telephone number: \n";
      cin>> hpno;
   };
   void showDetails (void)
   {
      // reading a text file

      string line;
      ifstream myfile ("courtbooking.txt");
      if (myfile.is_open())
      {
         while (! myfile.eof() )
         {
            getline (myfile,line);
            cout << line << endl;
         }
         myfile.close();
      }
      else cout << "Unable to open file"; 
      //return 0; You function is void, you can't return value from void
   }//You forgot the close bracket for the function
};

int main()
{
   int date;
   string name;
   int ic;
   int hpno;
   int selection;
   Booking details;
   details.enterDetails();
   ofstream myfile ("courtbooking.txt");
   if (myfile.is_open())
   {
      myfile << "Name:"<<name<<endl;
      myfile << "Ic:"<<ic<<endl;
      myfile << "Contact:"<<hpno<<endl;
      myfile.close();
   }
   else cout << "Unable to open file";

   details.showDetails();
   return 0;
}


Please use the code button ( # ) to post code or use the words [code] and [/code] before and after your code respectively

[EDIT]
Also at lines 60,61 and 62 you use the variables name, ic and hpno without giving them a value first.
Hope this helps
Last edited on
maxwell
have u successfully written the code?
Topic archived. No new replies allowed.