Hello experts and thanks again in advance for your help!! I'm doing a few lessons here and I've been required to write a very basic program to write lines to a text file until the user signals to stop. The example that I'm modifying uses char[] arrays? for the lines but seeing as how the book just went over the newer string class I figured I'd also update it to use that while completing the exercise.
for some reason I'm getting and issue opening the file and the debugger is pointing to line 33 where I attempt to open the file via the modified string.
I feel like there is something wrong with the string that I'm passing to the ofstream function but I don't think I have enough knowledge of how it works to understand whats wrong. I have created another program that wrote to a file but the filename passed to ofstream was coded inline rather than as a variable based on user input. Below is the error coming from the compiler.
error: no matching function for call to 'std::basic_ofstream<char>::basic_ofstream(std::string&)'
It then lists "possible candidates" but again I don't feel like I know enough to understand what it's telling me. =[
Thanks again for your prospective help!!
I actually enjoy debugging the programs myself but every so often i get something like this. I've not finished checking this one yet, this is just as far as I've gotten, if your expert eye happens to catch something else, don't even sweat it, I like to try to find all the ones I can by myself in the hopes that someday I'll be a pro. Thanks again guys.
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 43 44 45 46 47 48 49
|
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string current, filenm, fileloc, fileinfo, ans1 = "n", ans2 = "y";
int lines = 0;
while(ans1 == "n" || ans1 == "N")
{
cout << "\n\nWelcome to Textfile Writer 1.0. What would you like to name your file?\n (file extension will be added automatically): ";
getline(cin, filenm);
cout << "\n\nPlease enter the path where you'd like to store it: ";
getline(cin, fileloc);
fileinfo = fileloc + "\\" + filenm + ".txt";
correctcheck:
cout << "\n\nThe full path and filename are: " << fileinfo << "\n\nIs this correct? (y/n) ";
getline(cin, ans1);
if(ans1 != "y" || ans1 != "Y" || ans1 != "N" || ans1 != "n")
{
cout << "\n\nInvalid answer: " << ans1;
goto correctcheck;
}
}
ofstream file1(fileinfo);
cout << "\n\nEnter lines of text to be written to file, each ENTER is a new line, a single space + ENTER will end the session";
while(ans2 != " ")
{
getline(cin, current);
file1 << current << endl;
lines++;
}
cout << "\n\n" << lines << " line(s) written successfully!\n\n";
return 0;
}
|