check if my program is correct

I have to make a c++ program who determining the number of:
1. Characters
A character could be any alphabets, digits, punctuation marks, or special
characters.
2. Operators
Operators are those symbols that are used in mathematica expression, such as,'+', '*', '/', '-', and so on.
3. Uppercase letters
Uppercase characters are those from A..Z
4. Numerical digits
A digit is any of the Hindu-Arabic numerals from 0..9

After hours of coding I m not sure if it actually work! This is my code:

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> 
#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 my input
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 m worried about " characters", if someone can tell me if it's correct because I m not sure if that "113" is correct.
Thank you
What about the space characters?

What I would do is something like:

1
2
3
4
5
6
while(std::getline(in, line))
{
    characters += line.size(); //add characters for that line 
    //find all the operators, uppercase, and numbers and increment
   //accordingly
}


You could also just read in 1 character at a time with get then increment accordingly.
Last edited on
Can you explain more ?
Will I explain what more? The space's being characters or getline or [/code]get[/code]?
The getline(in, line
That just reads until the delimiter is found(in this case it is the default newline '\n') instead of reading until the next white space. http://www.cplusplus.com/reference/string/string/getline/

You could also use this version of getline but I prefer the first. http://www.cplusplus.com/reference/istream/istream/getline/

Topic archived. No new replies allowed.