Help with counting Characters !
Apr 30, 2014 at 12:55am UTC
Hi I'm working on a program that determine the number of Characters, Operators, Uppercase letters, and Numerical digits. The following code seems correct to me but steel doesn't work propriety, someone can help me?
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 53
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
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.eof()) {
inFile >> text ;
if (text >= 'A' && text <= 'Z' ) {
Uppercase_letters++;
}
else if (text >= '0' && text <= '9' ) {
Numerical_digits++;
}
else if (text =='+' || '-' || '/' || '*' || '%' ) {
Operators++;
}
else
{
Characters++;
}
}
cout << Uppercase_letters << endl;
cout << Numerical_digits << endl;
cout << Operators << endl;
cout << Characters++ << endl;
}
cin.get();
}
Last edited on Apr 30, 2014 at 1:00am UTC
Apr 30, 2014 at 1:02am UTC
You have pretty much all of your code inside the
if (inFile.fail())
block.
There should be a } after the
return 0;
on line 22.
(Then get rid of the one on line 48.)
Also, instead of
24 25
while (!inFile.eof()) {
inFile >> text ;
use this:
24 25
while (inFile >> text) {
// ...
Apr 30, 2014 at 1:31am UTC
I still do not get the correct answer. this is my input file
This is a possible factorial function in a programming language called LISP
(defun factorial (n)
(if (< n 2)
1
(* n (factorial (1- n)))))
and this is the output
Apr 30, 2014 at 1:45am UTC
The third part of your if conditional branching needs to re-compare against "text" every time.
else if (text =='+' || '-' || '/' || '*' || '%' ) { //<---Modify this then it will work
Other than that everything else is fine.
The count is good, it just simply never makes it past the third if condition.
Last edited on Apr 30, 2014 at 1:46am UTC
Apr 30, 2014 at 1:49am UTC
how am I suppose to modify that ?
Apr 30, 2014 at 1:52am UTC
Hint: Added a few more text(s):
else if (text =='+' || text == '-' || text == '/' || '*' || '%' ) { //<---Go! all the way!
Topic archived. No new replies allowed.