Need Help With a C++ Code

hello everyone. I am in school right now and next week my final project is due. I have the entire project coded out by two parts will not fully run for me.. If i could get a little bit of advice i would be grateful.

The project builds and does not identify any issues but when opselect == 2, findMember() gives the else{error message} and then says "Search member" but says member was not found.

the other issue im having lies within the printReport() function. It does display out the information but It the information displays constantly, and does not stop until I stop running the program. Here is the code:

void printReport()
{
string name, address, city, state = "" ;
int zip, visa_Num = 0 ;

ifstream inFile ;
inFile.open("VisaCard.txt", ios::in);

if(inFile.is_open())
{
// PRIMING READ

displayTitle();
getline(inFile, name, '#') ;
while(!inFile.eof())
{
inFile.ignore(1);
getline(inFile, address, '#') ;
getline(inFile, city, '#');
getline(inFile, state, '#') ;
inFile >> zip ;
inFile >> visa_Num;
inFile.ignore(2);
displayReport(name,address,city,state,zip,visa_Num);
getline(inFile,name,'#');
} // END WHILE
} // END IF

else
{
cout << " File Could not be Opened " << endl ;
}

inFile.close();
system("pause");
void findMember()
{
member_Info arrRecord[maxSize] = { "","","","",0,0};
string name, address, city, state;
int zipCode, visaNum;
string findName;
int location = 0 ;
int index = 0 ;

ifstream inFile;
inFile.open(" VisaCard.txt" , ios:: in);
if(inFile.is_open())
{
getline(inFile, name, '#');
arrRecord[index].name = name;

while(!inFile.eof() && index < maxSize)
{
inFile.ignore(1);
getline(inFile, address, '#');
arrRecord[index].address = address;
getline(inFile, city, '#');
arrRecord[index].city = city;
getline(inFile, state, '#') ;
arrRecord[index].state = state;
inFile >> zipCode;
arrRecord[index].zipCode = zipCode;
inFile >> visaNum;
arrRecord[index].visaNumber = visaNum;
index++;
getline(inFile, name, '#');

} // END WHILE
} // END IF
else {
cout << " File Could Not Be Opened " << endl ;
}
inFile.close();
sortArray(arrRecord, index);

for(int x = 0; x < index; x++)
{
cout << arrRecord[x].name << endl;

} // END FOR LOOP

cout << " Enter the Member's Name you Want to Search " << endl ;
cin.ignore(1);
getline(cin, findName);

cout << arrRecord[index].name << arrRecord[index].address << arrRecord[index].city << arrRecord[index].state << arrRecord[index].zipCode << arrRecord[index].visaNumber << endl;

location = searchArray(arrRecord, index, findName);

if(location > -1)
{
cout << name << " Was Found at Location " << location << endl ;
}

else {

cout << " Member was not Found " << endl ;
}

system("pause");

} // END FIND MEMBER
Please edit your code and add code tags!.

Look at your print function and note the added comments:

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
void printReport()
{
    string name, address, city, state; // not needed >> = "" ;
    int zip, visa_Num = 0 ;

    /* Not necessary:
    ifstream inFile ;
    inFile.open("VisaCard.txt", ios::in); */
    
    // Should just be:
    ifstream infile("VisaCard.txt");  // Note: no ios::in, an ifstream is always ios::in, always, can't be changed.

    if(inFile.is_open())
    {
// PRIMING READ

        displayTitle();
        getline(inFile, name, '#') ;

        //while(!inFile.eof())  // You really shouldn't use eof() to control your read. Use the actual read operation instead,. (See note 1.)
        while(getline(infile, name, '#'))
        {
            inFile.ignore(1);   // What are you trying to ignore here? 
            getline(inFile, address, '#') ;
            getline(inFile, city, '#');
            getline(inFile, state, '#') ;
            inFile >> zip ;
            inFile >> visa_Num;
            inFile.ignore(2);  // What are you trying to ignore here?
            displayReport(name, address, city, state, zip, visa_Num);
            // getline(inFile, name, '#'); // Not needed when using the read to control the loop.
        } // END WHILE
    } // END IF

    else
    {
        cout << " File Could not be Opened " << endl ;
    }

    // inFile.close(); Not necessary, let the destructor do it's job.
    //system("pause"); // Why are you pausing here??
}

Please post a small sample of your input file.


Note 1: Don't forget that reading a file can generate other errors besides eof(). If your file is not properly structured you could be generating one of these other error messages. This is probably the biggest reason to avoid using eof() to control a read loop.




Last edited on
Where I have inFile.ignore(1) and ignore(2) we were taught that we are ignoring the '#'..
System("pause") is used so the program "pauses" and the results are displayed. Otherwise it would go back to the opselect menu without displaying results. (again this is what we were taught in class)

Do you want me to post a sample of the actual file that it saves on my computer or the ofstream outFile part ?
Where I have inFile.ignore(1) and ignore(2) we were taught that we are ignoring the '#'..

But don't forget that getline() extracts and discards the delimiter. When using the extraction operator it might be better to extract the delimiter to a variable instead of using ignore().

System("pause") is used so the program "pauses" and the results are displayed. Otherwise it would go back to the opselect menu without displaying results.


The output will be displayed without the pause, however if your menu function is clearing the screen you might not see the display, and IMO clearing the screen would probably also a bad practice.

Do you want me to post a sample of the actual file that it saves on my computer or the ofstream outFile part ?

I want to see your input file, which may not necessarily be your output file.

Also when posting the input file please be sure to place the text into code tags to preserve formatting.

Have you considered reading the file once, placing the file contents into a vector/array of your structure, then passing this structure to and from your functions instead of continually reading from the file? Reading from a file is hundreds of times slower than reading from memory.

void newVisa()
{

// DECLARE VARIABLES:
string name = "" ;
string address = "" ;
string city = "" ;
string state = "" ;
ofstream outFile;
int visa_Number = 0 ;
int zipCode = 0;

outFile.open(
"VisaCard.txt"
,ios::app);
if (outFile.is_open())
{

getName(name);

getAddress(address) ;

getCity(city);

getState(state);

zipCode = getZipCode();

visa_Number= getVisa();

displayReport(name, address, city, state,zipCode, visa_Number);

system(" pause ");


outFile << name << '#' << address << '#'<<city << '#' << state << '#' << zipCode <<'#' << visa_Number << '#' << endl;
}

else
{
cout <<
"File Not Opened"
<< endl ;
}

outFile.close();


}
Last edited on
Please edit your post and add code tags!

What jlb is saying about code tags is:
1
2
3
"[ code ]"
    Put all code in between the tags.  
"[ /code ]"
Topic archived. No new replies allowed.