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
Screenshot
Program
//Header files
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
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");
// 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
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();
}
----------------------------------------------------------------
Output
1-214-02031-3 is a valid isbn
0-070-21604-5 is a valid isbn
2-14-241242-4 is a valid isbn
2-120-12311-x doesn't have 10 digits
0-534-95207-x doesn't have 10 digits
2-034-00312-2 is a valid isbn
1-013-10201-2 is a valid isbn
2-142-1223 doesn't have 10 digits
3-001-0000a-4 doesn't have 10 digits
File output
1-214-02031-3 is a valid isbn
0-070-21604-5 is a valid isbn
2-14-241242-4 is a valid isbn
2-120-12311-x doesn't have 10 digits
0-534-95207-x doesn't have 10 digits
2-034-00312-2 is a valid isbn
1-013-10201-2 is a valid isbn
2-142-1223 doesn't have 10 digits
3-001-0000a-4 doesn't have 10 digits
---------------------------------------------------------------------------
Note:-
Are you getting an error?
There is no change needed, it's working perfectly.
Please check and let me know