In: Computer Science
C++ question.
I want to write all output to the file "output.txt", but it will write just first character. Can you fix it?
#include
#include
#include
#include
#include
using namespace std;
using std::cin;
using std::cout;
using std::string;
// remove dashes convert letters to upper case
string normalize(const string &isbn) {
string ch;
for (char i : isbn) {
if (i == '-') {
continue; // if "-" then skip it
}
if (isalpha(i)) {
i = toupper(i); // Check uppercase
}
ch += i;
}
return ch;
}
// return the number of digits in to the string
size_t numDigits(string &str) {
size_t numDigits = 0;
for (char ch : str) {
if (isdigit(ch)) {
++numDigits;
}
}
return numDigits;
}
enum ValidationCode {
Ok, // validation passed
NumDigits, // wrong number of digits
ExtraChars,// extra characters in isbn
Done
};
enum ValidationCode validate(string &isbn) {
int Done = 4;
string normal = normalize(isbn);
size_t count = numDigits(normal);
if (normal.size() == Done)
exit(0);
if (count != 10) {
return NumDigits;
}
if (normal.size() == 10 || normal.size() == 11 && normal[10] == 'X') {
return Ok;
}
return ExtraChars;
}
int main() {
// open a file
ofstream file("output.txt");
//The following code is taken from (https://en.cppreference.com/w/cpp/io/basic_ios/fail)
// check if the file can open
if(!file) // operator! is used here
{
std::cout << "File opening failed\n";
return EXIT_FAILURE;
} //end of borrowed code
// read a file
//The following code is referenced from (https://stackoverflow.com/questions/7868936/read-file-line-by-line-using-ifstream-in-c)
std::ifstream ifs("test_data.txt");
// check if the file can read
if (ifs.fail())
{
std::cerr << "test_data.txt could not read" << std::endl;
return -1;
} //end of referenced code
std::string str;
while (ifs >> str){
switch (validate(str)) {
case Ok:
cout << str << " is a valid isbn\n";
file << str << " is a valid isbn\n";
break;
case NumDigits:
cout << str << " doesn't have 10 digits\n";
file << str << " doesn't have 10 digits\n";
break;
case ExtraChars:
cout << str << " has extra characters\n";
file << str << " has extra characters\n";
break;
default:
cout << "ERROR: validate(" << str << ") return an unknown status\n";
file << "ERROR: validate(" << str << ") return an unknown status\n";
break;
}
}
ifs.close();
file.close();
}
test_data.txt
1-214-02031-3
0-070-21604-5
2-14-241242-4
2-120-12311-x
0-534-95207-x
2-034-00312-2
1-013-10201-2
2-142-1223
3-001-0000a-4
done
/* the problem was buffer got full to clear it we have to use flush method */
/* Run code it's working now*/
#include<iostream>
#include<fstream>
using namespace std;
// remove dashes convert letters to upper case
string normalize(const string &isbn) {
string ch;
for (char i : isbn) {
if (i == '-') {
continue; // if "-" then skip it
}
if (isalpha(i)) {
i = toupper(i); // Check uppercase
}
ch += i;
}
return ch;
}
// return the number of digits in to the string
size_t numDigits(string &str) {
size_t numDigits = 0;
for (char ch : str) {
if (isdigit(ch)) {
++numDigits;
}
}
return numDigits;
}
enum ValidationCode {
Ok, // validation passed
NumDigits, // wrong number of digits
ExtraChars,// extra characters in isbn
Done
};
enum ValidationCode validate(string &isbn) {
int Done = 4;
string normal = normalize(isbn);
size_t count = numDigits(normal);
if (normal.size() == Done)
exit(0);
if (count != 10) {
return NumDigits;
}
if (normal.size() == 10 || normal.size() == 11 && normal[10] == 'X') {
return Ok;
}
return ExtraChars;
}
int main() {
// open a file
ofstream file("output.txt");
if(!file) // operator! is used here
{
std::cout << "File opening failed\n";
return EXIT_FAILURE;
} //end of borrowed code
ifstream ifs("test_data.txt");
// check if the file can read
if (ifs.fail())
{
std::cerr << "test_data.txt could not read"
<< std::endl;
return -1;
} //end of referenced code
string str;
while (ifs >> str){
switch (validate(str)) {
case Ok:
cout << str << " is a valid
isbn\n";
file << str << " is a valid
isbn\n";
break;
case NumDigits:
cout << str << " doesn't have 10
digits\n";
file << str << " doesn't have 10
digits\n";
break;
case ExtraChars:
cout <<
str << " has extra characters\n";
file <<
str << " has extra characters\n";
break;
default:
cout <<
"ERROR: validate(" << str << ") return an unknown
status\n";
file <<
"ERROR: validate(" << str << ") return an unknown
status\n";
break;
}
file.flush(); // to clear
buffer
}
ifs.close();
file.close();
}
/* PLEASE UPVOTE */
/* OUTPUT */