I need to use the strlen function to get a total # of characters. when I run the program I get 1 for the number of characters. what am I missing?
// C++ program that opens a file and reads one character at a time.
// Also keeps count of total character type.
#include <iostream>
#include <cstring>
#include <string>
#include <cctype>
#include <fstream>
#include <cstdlib>
using namespace std;
// Function Protocols
void openInputFile(ifstream & , string str);
int myStrLength(char *);
int main() {
// Variables
int totalAlpha = 0; // Accumulator for total characters
int totalNum = 0; // Alpha counter
int totalPunc = 0; // Number counter
int totalSpace = 0; // Punctuation counter
char ch;
char str[150];
int len;
string inFileName = "lab14Q1.txt";
ifstream infile;
// Calls openInputFile function
openInputFile(infile, inFileName);
// Process file
infile.get(ch);
cout << "Contents of File: \n";
// Character validation
while (infile)
{
cout << ch;
str[ch] = ch;
if (isalpha(ch)) // Validates if alpha then stored in variable
{
totalAlpha += 1;
}
else if (isdigit(ch)) // Validates if numeric then stored in variable
{
totalNum += 1;
}
else if (ispunct(ch)) // Validates if punctuation then stored in variable
{
totalPunc += 1;
}
else if (isspace(ch)) // Validates if white space then stored in variable
{
totalSpace += 1;
}
infile.get(ch); // Next character
}
infile.close(); // Closes file
len = myStrLength(&ch);
A C-style string is just an array of char with a null terminating character. When you pass an array as an argument to a function (like you did), the size of the array is lost. So, you have to pass the size of the array with it; this defeats the point of your function myStrLength(char*).
Also, why are you creating another function to get the length of the string? Functions are supposed to be used so that your code is more simpler, modular, and reusable. But creating a function which returns the length of a string by calling a function already there for that is more work than just calling strlen().
So, instead of calling your own function myStrLength(char*) just call strlen(). Get rid of your function and use strlen().
Meaning replace len = myStrLength(&ch) with len = strlen(ch).
as part of the lab it asks to use the strlen function to get a total number of characters read from a file.
That sounds a bit of an odd thing for the lab requirement. It would mean you needed a character array large enough to hold the entire file contents. You wouldn't know how large that array should be unless you already knew how many characters were in the file. Also, if the file happened to contain any '\0' bytes, strlen would give an incorrect result.
Is there any reason why you can't simply add 1 to a total for each iteration of your main while loop?
Or is it asking for the total number of different characters, which is another matter altogether?
This part of the code, the comments don't match the names:
1 2 3 4
int totalAlpha = 0; // Accumulator for total characters
int totalNum = 0; // Alpha counter
int totalPunc = 0; // Number counter
int totalSpace = 0; // Punctuation counter
Perhaps you really do need another accumulator after all?