- Code: Select all
for (char c = file.get(); c != EOF; c = file.get())
{
//The variable "c" now contains the next character from the file
}
Thanks for all the help I have been getting. I am taking this class online through my local college and we watch a recorded video online and then do the homework which doesn't have anything to do with the video we watched. It takes 2-3 days to get a response from the teacher. I'm almost done so I wont be taking a programming class online again. Any ways here is the code that I have so far. The problem that I am having is that I do not know how to make it to allow the user to input any .txt file and I also do not know how output the entire text file or to count any of the uppercase, lowercase, punction or white spaces. I only have the character count.
Here is my code so far
- Code: Select all
#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
using namespace std;
int main()
{
fstream data_store;
char line[100];
int i;
int lower_count = 0;
int upper_count = 0;
int alpha_count = 0;
int space_count = 0;
int punct_count = 0;
data_store.open("hello.txt", ios::in);
while (!data_store.eof())
{
data_store.getline(line, 100);
for (int x = 0; x < strlen(line); x++)
{
char c = line[x];
if (isalpha(c))
{
alpha_count++;
}
else if (ispunct(c))
{
punct_count++;
}
else if (isspace(c))
{
space_count++;
}
if ((line[i] >= 'a' && line[i] <= 'z'))
{
lower_count++;
i++;
}
else if ((line[i] >= 'A' && line[i] <= 'Z'))
{
upper_count++;
i++;
}
}
}
data_store.close();
cout << "-- File contents --" << endl;
cout << "-- End --" << endl;
cout << "File statistics:" << endl;
cout << "Uppercase characters: " << upper_count << endl;
cout << "Lowercase characters: " << lower_count << endl;
cout << "Punctuation characters: " << punct_count << endl;
cout << "Alphabetic characters: " << alpha_count << endl;
cout << "Whitespace characters: " << space_count << endl;
}
