+ 2

data files

why is this program printing an extra letter at the end https://code.sololearn.com/cCy5k26t27R7/?ref=app

6th Aug 2018, 7:51 PM
Saurabh Tiwari
Saurabh Tiwari - avatar
2 odpowiedzi
+ 1
You need to declare the length of char c.
6th Aug 2018, 11:55 PM
Dominic Nicholas
Dominic Nicholas - avatar
+ 1
Use string for read/write buffer instead of char array, you have its header included already, why we use char array when string is available? #include <string> #include <iostream> #include <fstream> int main() { std::string name; std::ofstream f1("name.txt"); std::cout << "Enter some names:\n\n"; // Read names and write to file while(true) { std::getline(std::cin, name); if(!std::cin.good()) break; f1 << name << "\n"; } f1.close(); int lines {0}; std::ifstream f2("name.txt"); // Read file and write names on screen // Calculate and show number of lines while(std::getline(f2, name)) { std::cout << name << "\n"; ++lines; } f2.close(); std::cout << "\nLines read: " << lines; } Hth, cmiiw
7th Aug 2018, 6:18 AM
Ipang