0

Please fix this problem in my code !

#include <iostream> #include <fstream> #include <string> using namespace std; int main (){ int x = 0; string Text; ofstream myfile ("input.txt"); cout << "inter what is in your mind:" << endl; while (x == 0){ cin >> Text; myfile << Text; } myfile.close(); } What the code do ? it save the text that you write in file called input.txt What is the problem ? the problem is .... it just save the first word in the text for example : i wrote (Hello world i love potato) .. it save only (hello) in input. txt !!! Please help me

4th Jun 2018, 9:22 PM
Mohammad Alshareef
Mohammad Alshareef - avatar
10 odpowiedzi
+ 2
std::cin only takes any string until it encounters a space, therefore, u only got ‘hello’ in ur txt file. here is a way to take a whole sentence as input (the working version of ur code) : std::string text; std::ofstream ofs(“a_file.txt”); std::getline(std::cin, text); ofs << text; ofs.close(); Mohammad Alshareef
4th Jun 2018, 10:52 PM
Flash
0
Flash Please ... are you sure that cin that take the text until space ?? This very important to me !
5th Jun 2018, 12:12 AM
Mohammad Alshareef
Mohammad Alshareef - avatar
0
std::string a, b; std::cin >> a >>b; std::cin.ignore(); std::cout << a << std::endl << b; //input: joe olson(+hit enter) //output: joe olson btw, instead of asking me if i’m certain or not why don’t u try out!? if u understand the above code, then u should’t be asking this. Mohammad Alshareef don’t let someone’s sololearn level to fool u!! thanks.
5th Jun 2018, 12:37 AM
Flash
0
Flash but this work ! do { cin>>data; MyFile<<data<<" "; }while(data!="exit"); MyFile.close(); }
5th Jun 2018, 12:40 AM
Mohammad Alshareef
Mohammad Alshareef - avatar
0
Mohammad Alshareef would u mind writing the whole code, instead of writing some abstracts?
5th Jun 2018, 12:46 AM
Flash
0
#include <fstream> #include <iostream> #include <cstdlib> using namespace std; int main() { string data; ofstream MyFile; MyFile.open("nodpad.txt"); cout<<"write What in your mind : "<<endl; do { cin>>data; MyFile<<data<<" "; }while(data!="exit"); MyFile.close(); }
5th Jun 2018, 12:50 AM
Mohammad Alshareef
Mohammad Alshareef - avatar
0
nice,! //input: i love exit still cin only takes string untill space, here, it may seem u r taking the input as a whole sentence, but in reality u r providing input 3 times. just put this: cin>data; cout>”???” myfile<< data << “ “ u will see ??? 3 times since it’s loop input sentence was in the memory therefore, cin could access the next string after “ “. nonetheless, u need to put additional “ “ and u r gonna get “exit” in ur output txt file as well. which i don’t think u’d want. but if u still think i’m wrong try this: std::string name; std::cin >> name; std::cout << name; //in: max wild //out: max tell me why not max wild is printed? Mohammad Alshareef
5th Jun 2018, 1:36 AM
Flash
0
Flash you used cout not the stream name ... i just need to print the text in file not in the screen
5th Jun 2018, 3:59 AM
Mohammad Alshareef
Mohammad Alshareef - avatar
0
Mohammad Alshareef that cout was not gonna print ur data on the console, the purpose was to illustrate how many times cin was used. anyway, just a suggestion, as a programmer u should understad why ur code works and vice versa. and if u did then u woudn’t be asking those questions. all the best.
5th Jun 2018, 8:35 AM
Flash
0
Flash Thanks you for every thing ..You helped me alot ...
5th Jun 2018, 3:02 PM
Mohammad Alshareef
Mohammad Alshareef - avatar