C++ Input to .txt -
i've been working on notebook program few days , have ran problem can't seem figure out. code bellow runs fine, final outcome has me scratching head.
#include <iostream> #include <fstream> #include <string> #include <ctime> int main () { time_t = time(0); tm *ltm = localtime(&now); std::string note; int day = ltm->tm_mday; int month = 1 + ltm->tm_mon; int year = 1900 + ltm->tm_year; std::string d = std::to_string(day); std::string m = std::to_string(month); std::string y = std::to_string(year); std::string text; std::getline(std::cin, text); if(text.find("take ") != std::string::npos && text.find("note ") != std::string::npos) { { std::ofstream myfile ("c:\\users\\filepath\\" + m + d + y + ".txt"); if (myfile.is_open()) { std::cin >> note; myfile << note << "\n"; } else { std::cout << "could not create note. write down." << endl; } } } system("pause"); return 0; }
if type command prompt "give invite sander cohen."
when open text file, in is, "give"
thank help!
there 1 problem, cin
inputs first word, not whole line. easy fix, here new code:
#include <iostream> #include <fstream> #include <string> #include <ctime> int main () { time_t = time(0); tm *ltm = localtime(&now); std::string note; int day = ltm->tm_mday; int month = 1 + ltm->tm_mon; int year = 1900 + ltm->tm_year; std::string d = std::to_string(day); std::string m = std::to_string(month); std::string y = std::to_string(year); std::string text; std::getline(std::cin, text); if(text.find("take ") != std::string::npos && text.find("note ") != std::string::npos) { { std::ofstream myfile ("c:\\users\\filepath\\" + m + d + y + ".txt"); if (myfile.is_open()) { std::getline(std::cin, note); // getline myfile << note << "\n"; } else { std::cout << "could not create note. write down." << std::endl; // make sure have std:: befor endl } } } system("pause"); return 0; }
good luck notebook program!
Comments
Post a Comment