I was wondering what I'm doing wrong here. This is my first time using file I/O and I can't seem to figure out what’s going on. It's supposed to take a paragraph or some words from the input file and output the results onto another file and on the console.
Here’s what I've got at the moment.
// Include necessary header files
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
// Declare the namespace
using namespace std;
// Define the main() function
int main()
{
// Initialize the variables
int tot_chars=0,tot_nonwhite=0,tot_letters=0;
// Declare the variables
string str;
ifstream input;
ofstream output;
// Open the file
input.open("inputfile.txt");
// Check the condition
if(input.fail())
{
// Print the statement
cout << "input file did not open please check it\n";
// Return one
return 1;
}
// Open the output file
output.open("outputfile.txt");
// Get the input
getline(input, str);
// Pass the input as an argument
while(input)
{
// For loop to iterate the sentence
for(int i=0; str[i]!='\0'; i++)
{
/* Increment the value of the variable tot_chars by 1 */
tot_chars++;
// Check the condition
if(isalpha(str[i]))
/* Increment the value of the variable tot_letters by 1 */
tot_letters++;
// Check the condition
if(!isspace(str[i]))
/* Increment the value of the variable tot_nonwhite by 1 */
tot_nonwhite++;
}
/* Increment the value of the variable tot_nonwhite by 1 */
tot_nonwhite++;
// Get the input
getline(input, str);
}
// Print the output to the output file
output << "Total Characters: " << tot_chars << endl;
// Print the output to the output file
output << "Total non-whitespace characters: " << tot_nonwhite << endl;
// Print the output to the output file
output << "Total letters: " << tot_letters << endl;
// Print the output on the screen
cout << "Total Characters: " << tot_chars << endl;
// Print the output on the screen
cout << "Total non-whitespace characters: " << tot_nonwhite << endl;
// Print the output on the screen
cout << "Total letters: " << tot_letters << endl;
// Close the output file
output.close();
// Close the input file
input.close();
// Return zero
return 0;
}