help with using strlen function

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);

cout << endl << "Total Alpha characters: " << totalAlpha;
cout << endl << "Total Numeric characters: " << totalNum;
cout << endl << "Total Punctuation characters: " << totalPunc;
cout << endl << "Total White space characters: " << totalSpace;
cout << endl << len;
return 0;
}
/***************************
* Function Definitions *
***************************/

//**********************************************************\\
// The openInputFile function accepts ifstream object infile\\
// and string containing its name. \\
//**********************************************************\/
void openInputFile(ifstream & infile, string inFileName)
{
if (!infile)
{
cout << " Error, file does not exist!";
exit(12);
}
else
infile.open("lab14Q1.txt");
}

int myStrLength(char *ch)
{
int len = strlen(ch);
return len;
}
Why are you trying to use C functions instead of the standard C++ ones?

Unless you have to use strlen() you should get the length like this.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

int main()
{
    std::string str;

    std::cout << "Enter a word/phrase: " << std::flush;
    std::getline(std::cin, str);

    std::cout << "\n\nThe word/phrase is " << str.length() <<
    " characters long.\n";

    std::cin.ignore();
    std::cin.get();

    return 0;
}
@boost lexical cast
as part of the lab it asks to use the strlen function to get a total number of characters read from a file.
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).
Last edited on
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?
Last edited on
Topic archived. No new replies allowed.