Tuesday 15 January 2013

C++ help regarding class implementation and writing into a created text file -



C++ help regarding class implementation and writing into a created text file -

i have been confused few hours on how should prepare this.. have 2 functions in implementation file class. anyways, i've been trying programme scramble words in file have on computer (which dictionary list of many words), far has been successful.

but, problem i'm trying bring of scrambled version of words onto newly created text document stores them... far, hasn't been working , i'm confused. bolded problem in code.

please note: i'm pupil who's learning c++ , code little unorganized until can finish general core of program. instructor says not allowed utilize string class, only cstring.

area needs help:

"cout << tmpword << endl; //what want written new text file **** help" , "new_dictionary<

thanks in advance!

void scrambler::scrambleletters_in_file() { char * tmpword = nullptr; //null // compare every single word in dictionary (int = 0; < dictionarysize; i++) { // clean space holding previous word if if (tmpword != nullptr) delete[] tmpword; //prepare space tmpword = new char[strlen(dictionary[i]) + 1]; strcpy_s(tmpword, strlen(dictionary[i]) + 1, dictionary[i]); // unscrambling stuff here // 1 2 3 4 5 6 7 int first = 1; // h c k e n '/0' int lastly = strlen(tmpword) - 2; // = c h c k e // showing difference here, first/last letters // not scrambled. (int = first; < last; i++) { int j = rand() % lastly + 1; // chicken[1-5]'s letters randomized // instance: "hicke" scrambled. char result = tmpword[i]; tmpword[i] = tmpword[j]; tmpword[j] = result; } cout << tmpword << endl; //what want written new text file **** help } } void scrambler::createnew_dictionary() { // ofstream used writing files // we'll create file called scrambled_words.txt char newfilename[25]; char choice3; cout << "\nhow want create new scrambled file? (input 1 or 2) " << endl; cout << " - preset: (\"unscrambled_file.txt\"): 1 " << endl; cout << " - custom:\t\t\t 2 " << endl; cout << "\nchoice ===> "; cin >> choice3; if (choice3 == '1') { strcpy_s(newfilename, 25, "unscrambled_file.txt"); // copies preset filename "newfilename" } if (choice3 == '2') { cout << "\nwhat new file name be? (example: \"test.txt\")" << endl; cout << "~ note: if type in existing file name, overwritten " << endl; cout << "* warning: utilize underscores instead of spaces in text file name! " << endl; // since i'm supposed utilize cstring // instead of string, won't work spaces cout << "\nfile name ====> "; cin >> newfilename; // ^ practing adding user input customization ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ //*to (and practice) - show list of existing .txt files, , select file delete// } ofstream new_dictionary(newfilename); // opens and/or creates specified file if hasn't already. // if couldn't open output file stream writing if (!new_dictionary) { // print error , exit cout << "uh oh, text file not opened writing!" << endl; system("pause"); // included have time read error message // says before programme exited next statement. exit(1); // equivilent "return 0;" in main function } else cout << "file created , opened" << endl; new_dictionary<<scrambleletters_in_file(); // <--- problem ******** /* we'll write 2 lines file new_dictionary << "this line 1" << endl; new_dictionary << "this line 2" << endl; */ cout << "file overwritten" << endl; new_dictionary.close(); cout << "file closed. " << endl; }

in scrambleletters_in_file() outputting strings cout standard output (console default). line:

new_dictionary<<scrambleletters_in_file();

means "calculate result of function scrambleletters_in_file , write new_dictionary". , result void, code doesn't compile.

what need pass stream function:

void scrambler::scrambleletters_in_file(ostream& out) { ... out << tmpword << endl; }

and phone call this:

scrambleletters_in_file(new_dictionary);

c++ class header fstream cstring

No comments:

Post a Comment