简体   繁体   中英

C++ overwrites on a text file when it is opened again

This program is supposed to open a txt file, record the answers from two different questions, close the file, and then reopen it in a function called the accumulatingFunction so the answers for each question can be counted.

I've managed to get it to work but my problem is that when I close out the program there will be say, 3 instances of each answer, but when I open the program and add 1 answer of each question it will still say that I have 3 instances of that answer.

I'm assuming that its over writing one of the answers that are already there, and I can't for the life of me figure out how to make it stop. (also, right now the accumulatingFunction is only checking for the first answer of each question. I wanted to make sure this was possible before I add in the rest. Or I thought that maybe you guys will have another way of doing this.)

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

double userInput = 0;
string ethnicityQuestion();
void validationFunction(int);
string politicalQuestion();
void accumulatingFunction();



//-----------------------------------------------------------------------------------------------

int main()
{
    string ethnicityAnswer, politicalAffiliationAnswer, userID;
    fstream answerFile;

answerFile.open("F:\\midTermFile.txt");



if (!answerFile)
    cout << "You have a file read error" <<endl;


while (userID != "done")
{

ethnicityAnswer = ethnicityQuestion();
system("cls");

politicalAffiliationAnswer = politicalQuestion();
system("cls");


answerFile << ethnicityAnswer << endl;
answerFile << politicalAffiliationAnswer << endl;

cout << "you made it back to the main function and you chose " << ethnicityAnswer << " as your ethnicity\n"<< endl;
cout << "you made it back to the main function and you chose " << politicalAffiliationAnswer << " as your political affiliation\n"<< endl;

accumulatingFunction();

cout << "Please enter your user ID:  ";
cin >> userID;

}
answerFile.close();


return 0;
}

//-----------------------------------------------------------------------------------------------

string ethnicityQuestion()
{

    string ethnicity;
    int selection = 6;

string A = "Native_American";
string B = "Asian";
string C = "African American";
string D = "Hispanic/Latino";
string E = "Caucasion";
string F = "Other";

cout << "What ethnicity do you claim?\n";
cout << "1. Native American\n";
cout << "2. Asian\n";
cout << "3. African American\n";
cout << "4. Hispanic/Latino\n";
cout << "5. Caucasion\n";
cout << "6. Other\n";

validationFunction(selection);


if (userInput == 1)
    ethnicity = A;
else if (userInput == 2)
    ethnicity = B;
else if (userInput == 3)
    ethnicity = C;
else if (userInput == 4)
    ethnicity = D;
else if (userInput == 5)
    ethnicity = E;
else if (userInput == 6)
    ethnicity = F;

return ethnicity;
}

//------------------------------------------------------------------------------------------------

string politicalQuestion()
{
    string affiliation; 
    int selection = 6;

string A = "Very_Conservative";
string B = "Moderately Conservative";
string C = "Very Liberal";
string D = "Moderately Liberal";
string E = "Neither";
string F = "In the Middle";

cout << "On most political issues, which of the following do you associate with most:\n";
cout << "1. Very Conservative\n";
cout << "2. Moderately Conservative\n";
cout << "3. Very Liberal\n";
cout << "4. Moderatly Liberal\n";
cout << "5. Neither\n";
cout << "6. In the Middle\n";

validationFunction(selection);


if (userInput == 1)
    affiliation = A;
else if (userInput == 2)
    affiliation = B;
else if (userInput == 3)
    affiliation = C;
else if (userInput == 4)
    affiliation = D;
else if (userInput == 5)
    affiliation = E;
else if (userInput == 6)
    affiliation = F;

return affiliation;
}

//-----------------------------------------------------------------------------------

void validationFunction(int choiceAmount)
{
    while ((!(cin >> userInput)) || (userInput > choiceAmount || userInput < 1))
    {                       
        cin.clear();
        cin.ignore(INT_MAX, '\n');
        cout << "Please enter a number between 1 and 6: ";
    }
}

//------------------------------------------------------------------------------------------------

void accumulatingFunction()
{
    string userAnswer;
    double nativeAmerican = 0, veryConservative = 0;



    ifstream countFile;
    countFile.open("F:\\midTermFile.txt");



    while (!countFile.eof()) 
        {countFile >> userAnswer;

            if (userAnswer == "Native_American")
                nativeAmerican += 1;
            else if (userAnswer == "Very_Conservative")
                    veryConservative += 1;
            userAnswer = "";
        }
            cout << nativeAmerican << endl;
            cout << veryConservative << endl;

        countFile.close();
}

open the file the second time using the in argument.

So change this

countFile.open("F:\\midTermFile.txt");

to this

countFile.open("F:\\midTermFile.txt", fstream::in | fstream::app);

Edit: Misread question I thought you were trying to add to the file not read it.

Your problem are the parameters you don't pass to fstream::open. You have to pass fstream::out | fstream::app fstream::out | fstream::app as your second parameter.

Also see the fstream::open reference .

Also, since you are not reading from the file in main() you should use ofstream instead of fstream .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM