I have a program for C++ from textbook \"Starting out with C++ byTony Gaddis\" I
ID: 3615509 • Letter: I
Question
I have a program for C++ from textbook "Starting out with C++ byTony Gaddis" I need to open a file named"text.txt" from the student CD. Iwrite C++ program to open the file, but only line one and two thatappeared on the screen. I tried so many different ways to open afile but all of them not working. I also need to find the number ofthe uppercase letters, the lowercase letter, the digits in thefile, and the number of whitespace. Can you help me solve thisproblem?? Thank you so much before that.My program is:
//Program Challenge number 15, Character Analysis #include <iostream> #include <fstream> using namespace std;
int main() { fstream inFile; const int SIZE = 81; char inpFile[] = "text.txt"; char inputFile[SIZE];
cout<<"Displaying the file contents: " <<endl; inFile.open(inpFile);
//Read File
inFile.getline(inputFile,SIZE); cout << inputFile << endl; inFile.getline(inputFile,SIZE); cout << inputFile << endl;
inFile.getline(inputFile,SIZE); cout << inputFile << endl;
inFile.getline(inputFile,SIZE); cout << inputFile << endl;
inFile.getline(inputFile,SIZE); cout << inputFile << endl;
inFile.getline(inputFile,SIZE); cout << inputFile << endl;
inFile.getline(inputFile,SIZE); cout << inputFile << endl;
inFile.getline(inputFile,SIZE); cout << inputFile << endl;
inFile.getline(inputFile,SIZE); cout << inputFile << endl;
inFile.getline(inputFile,SIZE); cout << inputFile << endl;
inFile.close();
system("pause"); return 0; }
The text.txt file: No one is unaware of the name of that famous Englishshipowner, Cunard. In 1840 this shrewd industrialist founded a postal servicebetween Liverpool and Halifax, featuring three wooden ships with400-horsepower paddle wheels and a burden of 1,162 metrictons. Eight years later, the company's assets were increased by four650-horsepower ships at 1,820 metric tons, and in two more years,by two other vessels of still greater power and tonnage. In 1853 the Cunard Co., whose mail-carrying charter had justbeen renewed, successively added to its assets the Arabia, thePersia, the China, the Scotia, the Java, and the Russia, all shipsof top speed and, after the Great Eastern, the biggest ever to plowthe seas. So in 1867 this company owned twelve ships, eight with paddlewheels and four with propellers. If I give these highly condensed details, it is so everyonecan fully understand the importance of this maritime transportationcompany, known the world over for its shrewd management. No transoceanic navigational undertaking has been conductedwith more ability, no business dealings have been crowned withgreater success. In twenty-six years Cunard ships have made 2,000 Atlanticcrossings without so much as a voyage canceled, a delay recorded, aman, a craft, or even a letter lost. Accordingly, despite strong competition from France,passengers still choose the Cunard line in preference to allothers, as can be seen in a recent survey of officialdocuments. Given this, no one will be astonished at the uproar provokedby this accident involving one of its finest steamers.
I need to open a file named"text.txt" from the student CD. Iwrite C++ program to open the file, but only line one and two thatappeared on the screen. I tried so many different ways to open afile but all of them not working. I also need to find the number ofthe uppercase letters, the lowercase letter, the digits in thefile, and the number of whitespace. Can you help me solve thisproblem?? Thank you so much before that.
My program is:
//Program Challenge number 15, Character Analysis #include <iostream> #include <fstream> using namespace std;
int main() { fstream inFile; const int SIZE = 81; char inpFile[] = "text.txt"; char inputFile[SIZE];
cout<<"Displaying the file contents: " <<endl; inFile.open(inpFile);
//Read File
inFile.getline(inputFile,SIZE); cout << inputFile << endl; inFile.getline(inputFile,SIZE); cout << inputFile << endl;
inFile.getline(inputFile,SIZE); cout << inputFile << endl;
inFile.getline(inputFile,SIZE); cout << inputFile << endl;
inFile.getline(inputFile,SIZE); cout << inputFile << endl;
inFile.getline(inputFile,SIZE); cout << inputFile << endl;
inFile.getline(inputFile,SIZE); cout << inputFile << endl;
inFile.getline(inputFile,SIZE); cout << inputFile << endl;
inFile.getline(inputFile,SIZE); cout << inputFile << endl;
inFile.getline(inputFile,SIZE); cout << inputFile << endl;
inFile.close();
system("pause"); return 0; }
The text.txt file: No one is unaware of the name of that famous Englishshipowner, Cunard. In 1840 this shrewd industrialist founded a postal servicebetween Liverpool and Halifax, featuring three wooden ships with400-horsepower paddle wheels and a burden of 1,162 metrictons. Eight years later, the company's assets were increased by four650-horsepower ships at 1,820 metric tons, and in two more years,by two other vessels of still greater power and tonnage. In 1853 the Cunard Co., whose mail-carrying charter had justbeen renewed, successively added to its assets the Arabia, thePersia, the China, the Scotia, the Java, and the Russia, all shipsof top speed and, after the Great Eastern, the biggest ever to plowthe seas. So in 1867 this company owned twelve ships, eight with paddlewheels and four with propellers. If I give these highly condensed details, it is so everyonecan fully understand the importance of this maritime transportationcompany, known the world over for its shrewd management. No transoceanic navigational undertaking has been conductedwith more ability, no business dealings have been crowned withgreater success. In twenty-six years Cunard ships have made 2,000 Atlanticcrossings without so much as a voyage canceled, a delay recorded, aman, a craft, or even a letter lost. Accordingly, despite strong competition from France,passengers still choose the Cunard line in preference to allothers, as can be seen in a recent survey of officialdocuments. Given this, no one will be astonished at the uproar provokedby this accident involving one of its finest steamers.
//Program Challenge number 15, Character Analysis #include <iostream> #include <fstream> using namespace std;
int main() { fstream inFile; const int SIZE = 81; char inpFile[] = "text.txt"; char inputFile[SIZE];
cout<<"Displaying the file contents: " <<endl; inFile.open(inpFile);
//Read File
inFile.getline(inputFile,SIZE); cout << inputFile << endl; inFile.getline(inputFile,SIZE); cout << inputFile << endl;
inFile.getline(inputFile,SIZE); cout << inputFile << endl;
inFile.getline(inputFile,SIZE); cout << inputFile << endl;
inFile.getline(inputFile,SIZE); cout << inputFile << endl;
inFile.getline(inputFile,SIZE); cout << inputFile << endl;
inFile.getline(inputFile,SIZE); cout << inputFile << endl;
inFile.getline(inputFile,SIZE); cout << inputFile << endl;
inFile.getline(inputFile,SIZE); cout << inputFile << endl;
inFile.getline(inputFile,SIZE); cout << inputFile << endl;
inFile.close();
system("pause"); return 0; }
The text.txt file: No one is unaware of the name of that famous Englishshipowner, Cunard. In 1840 this shrewd industrialist founded a postal servicebetween Liverpool and Halifax, featuring three wooden ships with400-horsepower paddle wheels and a burden of 1,162 metrictons. Eight years later, the company's assets were increased by four650-horsepower ships at 1,820 metric tons, and in two more years,by two other vessels of still greater power and tonnage. In 1853 the Cunard Co., whose mail-carrying charter had justbeen renewed, successively added to its assets the Arabia, thePersia, the China, the Scotia, the Java, and the Russia, all shipsof top speed and, after the Great Eastern, the biggest ever to plowthe seas. So in 1867 this company owned twelve ships, eight with paddlewheels and four with propellers. If I give these highly condensed details, it is so everyonecan fully understand the importance of this maritime transportationcompany, known the world over for its shrewd management. No transoceanic navigational undertaking has been conductedwith more ability, no business dealings have been crowned withgreater success. In twenty-six years Cunard ships have made 2,000 Atlanticcrossings without so much as a voyage canceled, a delay recorded, aman, a craft, or even a letter lost. Accordingly, despite strong competition from France,passengers still choose the Cunard line in preference to allothers, as can be seen in a recent survey of officialdocuments. Given this, no one will be astonished at the uproar provokedby this accident involving one of its finest steamers.
No one is unaware of the name of that famous Englishshipowner, Cunard. In 1840 this shrewd industrialist founded a postal servicebetween Liverpool and Halifax, featuring three wooden ships with400-horsepower paddle wheels and a burden of 1,162 metrictons. Eight years later, the company's assets were increased by four650-horsepower ships at 1,820 metric tons, and in two more years,by two other vessels of still greater power and tonnage. In 1853 the Cunard Co., whose mail-carrying charter had justbeen renewed, successively added to its assets the Arabia, thePersia, the China, the Scotia, the Java, and the Russia, all shipsof top speed and, after the Great Eastern, the biggest ever to plowthe seas. So in 1867 this company owned twelve ships, eight with paddlewheels and four with propellers. If I give these highly condensed details, it is so everyonecan fully understand the importance of this maritime transportationcompany, known the world over for its shrewd management. No transoceanic navigational undertaking has been conductedwith more ability, no business dealings have been crowned withgreater success. In twenty-six years Cunard ships have made 2,000 Atlanticcrossings without so much as a voyage canceled, a delay recorded, aman, a craft, or even a letter lost. Accordingly, despite strong competition from France,passengers still choose the Cunard line in preference to allothers, as can be seen in a recent survey of officialdocuments. Given this, no one will be astonished at the uproar provokedby this accident involving one of its finest steamers.
Explanation / Answer
Hi. I believe the problem is the SIZE variable you defined. Youset SIZE = 81. Try making it a lot bigger, SIZE = 300 forexample. Also, you don't need to keep calling getline over and overagain. You should put it in a loop that will know when the fileends. This is better practice and will be more accurate: #include <iostream> #include <fstream> using namespace std;int main() { fstream inFile; const int SIZE = 300; char inpFile[] = "text.txt"; char inputFile[SIZE];
cout<<"Displaying the file contents: " <<endl; inFile.open(inpFile);
//Read File while(!inFile.eof()) //if notat end of file, continue reading from file { inFile.getline(inputFile,SIZE); cout << inputFile << endl; //you should put some code here to parsethrough the contents stored in the inputFile variable and to keeptrack //of upper case, lower case, etc. This willinvolve more loops. But do all of that here because at the end ofthis //outer loop, the variable inputFile will beoverwritten with a new line from the file...so then you parse thatand store //the information. } inFile.close(); system("pause"); return 0; } #include <fstream> using namespace std;
int main() { fstream inFile; const int SIZE = 300; char inpFile[] = "text.txt"; char inputFile[SIZE];
cout<<"Displaying the file contents: " <<endl; inFile.open(inpFile);
//Read File while(!inFile.eof()) //if notat end of file, continue reading from file { inFile.getline(inputFile,SIZE); cout << inputFile << endl; //you should put some code here to parsethrough the contents stored in the inputFile variable and to keeptrack //of upper case, lower case, etc. This willinvolve more loops. But do all of that here because at the end ofthis //outer loop, the variable inputFile will beoverwritten with a new line from the file...so then you parse thatand store //the information. cout << inputFile << endl; //you should put some code here to parsethrough the contents stored in the inputFile variable and to keeptrack //of upper case, lower case, etc. This willinvolve more loops. But do all of that here because at the end ofthis //outer loop, the variable inputFile will beoverwritten with a new line from the file...so then you parse thatand store //the information. } inFile.close(); system("pause"); return 0; } system("pause"); return 0; } Good Luck!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.