can't find my error !
May 5, 2014 at 12:36am UTC
I have to code a simple program who determining the number of Characters (A character could be any alphabets, digits, punctuation marks, or special , Operators ( Operators are those symbols that are used in mathematica expression, such as,'+', '*', '/', '-', and so on.), Uppercase letters (Uppercase characters are those from A..Z) and Numerical digits ( A digit is any of the Hindu-Arabic numerals from 0..9)
This is my code but I ca not figure out why the output is wrong!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std ;
int main() {
char text;
int Characters = 0;
int Operators = 0;
int Uppercase_letters = 0;
int Numerical_digits = 0;
ifstream inFile;
inFile.open("input_for_p3.txt" );
// Checking if the input file exists. If he do not , the program stop
if (inFile.fail()) {
cout << "No input file found!" << endl ;
cin.get();
return 0;
}
while (inFile >> text) {
if (text >= 'A' && text <= 'Z' ) {
Uppercase_letters++;
}
else if (text >= '0' && text <= '9' ) {
Numerical_digits++;
}
else if (text =='+' || text == '-' || text == '/' || text =='*' || text == '%' || text == '<' || text == '>' ) {
Operators++;
}
else
{
Characters++;
}
}
cout << "The number of characters = " << Characters + Operators + Numerical_digits + Uppercase_letters<< endl;
cout << "The number of operators = " <<Operators<< endl;
cout <<"The number of numerical digits = " <<Numerical_digits<<endl;
cout <<"Uppercase letters = " << Uppercase_letters<< endl;
cin.get();
}
This is my inputfile
This is a possible factorial function in a programming language called LISP
(defun factorial (n)
(if (< n 2)
1
(* n (factorial (1- n)))))
This is my output
The number of characters = 113
The number of operators = 3
The number of numerical digits = 3
Uppercase letters = 5
I think that "characters" is wrong, but I do not know why !
May 5, 2014 at 12:51am UTC
113 is correct for the number of characters, if you don't count spaces (or newlines) as characters.
inFile >> text
discards whitespace, so spaces will never be counted.
May 5, 2014 at 12:55am UTC
who do I add the number of whitespace?
May 5, 2014 at 1:11am UTC
You could use the isspace command. i.e
else if (text== isspace).
May 5, 2014 at 1:22am UTC
Read it character by character using while (inFile.get(text))
instead of while (inFile >> text)
.
Topic archived. No new replies allowed.