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";
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
#include <iostream>
#include <fstream>
#include <string>
usingnamespace 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