0

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;
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 2
    You have some stray characters in your file. You can see and remove them [here](https://onlinegdb.com/xP0ZsFiy5) – Jason Nov 27 '22 at 03:44
  • 1
    Unicode non-breaking space characters are encoded as `“\302\240”` (generally the result of copying source code from a website) – David C. Rankin Nov 27 '22 at 03:48
  • `using namespace std;` did not declare a namespace. It did something far more insidious and potentially destructive: It pulled the entire std namespace, which is freaking huge, into the global namespace where it can easily conflict with your stuff. Exercise caution when doing this because when it messes things up the results are nigh-inscrutable. See [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/q/1452721/4581301) for details. – user4581301 Nov 27 '22 at 04:37
  • There are at least 398 strange characters in the source code. All 398 are [NO-BREAK SPACE](https://www.utf8-chartable.de/unicode-utf8-table.pl?utf8=0x) (Unicode code point [U+00A0](https://www.utf8-chartable.de/unicode-utf8-table.pl?utf8=0x)). They can be searched for (and replaced) with regular expression `\x{00A0}` in any modern text editor). – Peter Mortensen Apr 18 '23 at 21:56
  • This is a super duplicate. This is a ***very*** common error when copying code from web pages, [PDF](https://en.wikipedia.org/wiki/Portable_Document_Format) documents, through chat (e.g. [Skype Chat](https://en.wikipedia.org/wiki/Features_of_Skype#Skype_chat) or [Facebook Messenger](https://en.wikipedia.org/wiki/Facebook_Messenger)), etc. The canonical question is *[Compilation error: stray ‘\302’ in program, etc.](https://stackoverflow.com/questions/19198332)*. – Peter Mortensen Apr 18 '23 at 21:57
  • 302 240 (octal) → 0xC2 0xA0 (hexadecimal) → UTF-8 sequence for Unicode code point U+00A0 ([NO-BREAK SPACE](https://www.charset.org/utf-8)). – Peter Mortensen Apr 25 '23 at 16:32

0 Answers0