#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
using namespace std;
const int num=1000;
int main(int argc, char *argv[]){
if (argc == 2){
ifstream file;
file.open(argv[1]);
}
else cout << "You typed only one file" << endl;
ifstream file;
file.open(argv[1]);
string space;
char word;
int i=0;
int w=0;
int s=0;
char letter[num];
int letter1[num];
while (file>>word)
{
i++;
letter[w]=word;
w++;
}
cout << endl;
cout << "Words: " << i << endl;
file.close();
for (int i=0; i < w; i++){
}
if (letter[0] >='a' && letter[0]<='z' ){
cout << "The first letter is not a capital sorry" << endl;
}
if(letter[i-1] != '.'){
cout << "You forgot a period at the end of your scentence." << endl;
}
file.open(argv[1]);
file >> noskipws;
while(file >> space){
i++;
cout << letter[i];
}
cout << i << s << endl;
for(int i=0;i<s;i++){
}
getline(file,space);
if(isspace(letter1[i]) == ' '){
cout << "You can only have one space." << endl;
}
cout << letter[i];
}
raidernation 0 Newbie Poster
Edited by Nick Evan because: Fixed formatting
Dani AI
Generated
— a few focused fixes will make this work and keep the logic easy to follow.
High-level advice: open the file once and check that it opened; read either line-by-line with std::getline or char-by-char with std::ifstream::get() (formatted extraction >> skips whitespace so it cannot detect double spaces or exact punctuation). Track the first alphabetic character to check capitalization, remember the last non-space character to check sentence-ending punctuation (accept . ? !), and toggle an inWord flag to count words. Avoid fixed-size C arrays for unknown input lengths and always cast to unsigned char before calling std::isalpha/std::isspace.
A minimal, robust pattern (compile with -Wall -Wextra) — reads once, counts words, flags missing end punctuation, checks first alphabetic character, and finds consecutive spaces:
#include <iostream>
#include <fstream>
#include <cctype>
int main(int argc, char* argv[]) {
if (argc != 2) return 1;
std::ifstream in(argv[1]);
if (!in) { std::cerr << "Cannot open file\n"; return 1; }
char c;
bool firstAlphaFound = false;
char firstAlpha = 0;
char lastNonSpace = 0;
bool prevSpace = false, doubleSpace = false, inWord = false;
int words = 0;
while (in.get(c)) {
unsigned char uc = static_cast<unsigned char>(c);
if (!firstAlphaFound && std::isalpha(uc)) { firstAlphaFound = true; firstAlpha = c; }
if (!std::isspace(uc)) {
lastNonSpace = c;
if (!inWord) { ++words; inWord = true; }
prevSpace = false;
} else {
if (c == ' ') { if (prevSpace) doubleSpace = true; prevSpace = true; }
else prevSpace = false;
inWord = false;
}
}
if (firstAlphaFound && !std::isupper(static_cast<unsigned char>(firstAlpha)))
std::cout << "First letter is not capital\n";
if (lastNonSpace != '.' && lastNonSpace != '?' && lastNonSpace != '!')
std::cout << "Missing sentence-ending punctuation\n";
if (doubleSpace) std::cout << "Found consecutive spaces\n";
std::cout << "Words: " << words << '\n';
} Troubleshooting tips: test on small sample files, print intermediate variables when debugging, and handle empty files explicitly. If you want sentence-level checks (multiple sentences, abbreviations like "Mr.") mention that — the logic needs to be extended to parse sentences rather than just the file boundaries.
raidernation 0 Newbie Poster
I can't get the program to work properly I am really new to all this stuff so if anyone can give me pointers I will appreciate it. P.s I have been reading and trying to understand how to solve this for a whole week.=(
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.