Program run infinite itself while getting data

Hi Guys,

I'm encountering some problem with program I write here. The program is able compiled without error. But when I try to call "Add Function" and execute system to capture data, it goes wrong. It only allow insert value for name but skip the rest, and program run keeps on run in loops. I check through but find not problem. Can anyone help me??



#include <iostream>
#include <list>
#include <string>
#include <fstream> //open the file for input/output
#include <iomanip>
#include <stdio.h>


using namespace std;

///////////////////////////////////////////////////////////////////////////////////////////////////////////
/////// Add Record Function ///////
///////////////////////////////////////////////////////////////////////////////////////////////////////////
struct RunnerRec // Structure to define variables in the Student Records.
{
char Name[30]; // Nameof the runner.
char BibNum; // Runner's ID.
char PhoneNum[10]; // Phone number of the runner
char Gender; // Gender of the runner
};

///////////////////////////////////////////////////////////////////////////////////////////////////////////
/////// Add Record Function ///////
///////////////////////////////////////////////////////////////////////////////////////////////////////////

void Add()
{
fstream file;
file.open("RunnerRecords.txt",ios::in|ios::out); //open RunnerRecords.txt

if (!file) //Validate RunnerRecords.txt is valid file
{
cerr << "file open error!" << endl;
return ;
}

list < RunnerRec > Runlist;
RunnerRec run;
int RunNum ; // Variable to count how many records insert each time
cout << "Please enter how many records you wish to insert:" <<endl;
cin >> RunNum;

for (int count = 1; count <= RunNum ; count++)
{
cout << "Please enter name:" << endl;
cin >> run.Name;
cout << "Please enter bib number:" <<endl;
cin >> run.BibNum;
cout << "Please enter phone number:" <<endl;
cin >> run.PhoneNum;

do
{
cout << "Please enter gender (M / F):"<< endl;
cin >> run.Gender;
cout << "Please only enter M or F";
}while (run.Gender != 'M' || run.Gender != 'F' || run.Gender != 'm' || run.Gender != 'f');

Runlist.push_back(run); //add
file.write((char*) &run, sizeof(run));
}
file.close();

}



void main() // Open up program
{
int input;
do
{

cout << "***************************************************" << endl;
cout << "*********Runner Records Management System**********" << endl;
cout << "***************************************************" << endl;
cout << "***************************************************" << endl;
cout << "(1) Add a runner record" << endl;
cout << "(2) Change runner's information" << endl;
cout << "(3) Delete a runner record" <<endl;
cout << "(4) Find a runner's information"<< endl;
cout << "(5) Display all runners' information"<< endl;
cout << "(6) Exit Program"<< endl;
cout << "****************************************************" << endl;
cout << "Please Choose:" << endl;
cin >> input;



switch(input)
{
case 1: Add();
break;
case 2: Change();
break;
case 3: Delete();
break;
case 4: Search();
break;
case 5: DisplayAll();
break;
case 6: Quit ();
break;
default: cout << "You did not enter a correct choice!!" << endl;
break;
}
}while(input != 6);

}
I do not think this is the problem, but inside that if statement block where you check if the file has been open there should be something that closes the file for good programming practice.
Last edited on
Topic archived. No new replies allowed.