0
if(command=="insert")
{
    int i=0;
    while(i>=0)
    {
        string textToSave[i];
        cout << "Enter the string you want saved: " << endl;
        cin >>textToSave[i];

        ofstream saveFile ("Save.txt");
        saveFile << textToSave;
        saveFile.close();
        i++;
        break;
    }
}

I want to store the array that i input into .txt file. But i having problem to create the array to store. I also dilemma to choose in between whileloop and forloop, but think that while loop is more suitable because unknow how many time need to insert the words. Please help. Thanks.

3
  • Is there some user input that should cause the code to stop prompting for input? Commented Apr 3, 2013 at 12:56
  • Yes, at first this program will prompt to ask user enter command, when user enter "insert" and it will run the function above. And it will ask user to insert string, and when user enter it will store in string file. If user want to enter again need to enter the command "insert" again. Commented Apr 3, 2013 at 12:58
  • @user1088346: In that case, if you're only reading one string for each "insert" command, you don't want a loop here at all. You want an outer loop for reading and processing the commands. Commented Apr 3, 2013 at 13:00

4 Answers 4

1

You're trying to store the entire array of strings, instead of just the current one. Not sure why you need the i and have an array at all though, since you're just reading and writing a single string at a time, anyway.

It could be something like:

if(command=="insert")
{
    string textToSave;

    cout << "Enter the string you want saved: " << endl;
    cin >>textToSave;

    ofstream saveFile ("Save.txt");
    saveFile << textToSave;
    saveFile.close();

    break;
}
Sign up to request clarification or add additional context in comments.

3 Comments

overwriting file constantly
Hi Unwind, before that i did do the exactly same with the solution your provide and it work, but when i enter command to display, it only display the last insert string. Example, first time insert i enter "abcd" second time insert i enter "qwer" but when i display it only display the "qwer" but abcd was gone. Below is my "display" command, did my display command issue causes this?
if(command=="display") { ifstream loadFile; loadFile.open ("Save.txt", ifstream::in); cout << "The file contained: "; while (loadFile.good()) { cout << (char) loadFile.get(); } cout << "" << endl; loadFile.close(); }
0

Some of the most obvious problems you have are:

  • You use a C99 feature called variable-length array when declaring textToSave. It's not legal C++.
  • If the variable-length array declaration would have worked, you would have written to one beyond the last index when getting the user input
  • You open, and overwrite, the file in each iteration.
  • You only loop once, as you break out of the loop, so save only one string anyway.
  • If you didn't use break at the end of the loop, you would loop forever with that condition.

Comments

0

create your array before loop, otherwise you are constantly creating arrays of size incremented by 1. also change file name if you want each to be saved in new file, otherwise you are overwriting same file constantly.

string textToSave[string_count];
if(command=="insert")
{
int i=0;
    while(i<string_count)
        {
            cout << "Enter the string you want saved: " << endl;
            cin >>textToSave[i];

            ofstream saveFile ("Save"+i+".txt");
            saveFile << textToSave[i];
            saveFile.close();
            i++;
        }
}

4 Comments

The insert now was look ok, but it unable to display the string data which i store. Example, first insert "Abcd" second insert "qwer" it only display "qwer" but "Abcd" was gone. Isn't because the display function was wrong? if(command=="display") { ifstream loadFile; loadFile.open ("Save.txt", ifstream::in); cout << "The file contained: "; while (loadFile.good()) { cout << (char) loadFile.get(); } cout << "" << endl; loadFile.close(); }
@user1088346 now you have many files with one string inside (so if you want to print==display them all you have to load all files and print content). is this what you wanted to have?
and in addition you have all content of all files in your array textToSave, you can print from it
do you want all content to be saved in one, single file or each word in different file?
0

But i having problem to create the array to store.

Outside the loop, declare an empty vector; this will hold the array:

vector<string> textToSave;

Inside the loop, read a string and append it to the array:

string text;
cin >> text;
textToSave.push_back(text);

or, slightly more compactly:

textToSave.push_back(string());
cin >> textToSave.back();

I also dilemma to choose in between whileloop and forloop

It looks like you don't want a loop here at all, since you're just reading one string. You probably want an outer loop to read the commands, along the lines of

vector<string> textToSave;
for (;;) { // loop forever (until you reach "break")
    string command;
    cin >> command;
    if (command == "quit") { // or whatever
        break;
    } 
    if (command == "insert") {
        cout << "Enter the string you want saved: " << endl;
        textToSave.push_back(string());
        cin >> textToSave.back();
    }
    // other commands ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.