Program Help Please!

Program Not Functioning Properly! Please Help! My "Other isn't giving out the right output! Some of my other character are reading as special characters! I also need help with reading this information through a txt. file .


Develop a program that will read in a text file named "text.data" and produce the following reports as it appears, where xx refers an actual number based on the text data. Please follow the definitions in the glossary section given at the end.
Report: 
Text Data: (Content of the text file)
Number of characters: xx
       ---> Number of letters: xx
              --- > Number of upper cases: xx
               ---> Number of lower cases: xx
       ---> Number of digits: xx
       ---> Number of punctuation characters: xx
       ---> Number of special characters (any character other than a letter, a digit, punctuation character, or a space): xx
       ---> Number of spaces: xx
Number of words: xx
Number of numbers: xx
Number of others (any group of characters other than a word, or a number): xx
Glossary:
Punctuation characters: (,) ( ;) (.) (:) (!) (?) (") (/) (\) (-) (+)   
Word: A group of letters ONLY, separated by a punctuation character or a space.   
Number: A group of digits ONLY.

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<stdio.h>
#include<iostream>
#include<iterator>
using namespace std;
main() 
{
int others = 0, numbers = 0, countWords = 0, space = 0, punct = 0, character = 0, letters = 0,upper = 0, lower = 0, digit=0, special=0;
char ch[80];
int i;

printf("\nEnter The String : ");
gets(ch);

for (i = 0; ch[i]!='\0';i++)

{
if (ch[i] != ' ')
character++;
if (ch[i] >= 'a' && ch[i] <= 'z' && ch[i] >= 'A' && ch[i] >= 'Z' && ch[i] != ' ') // Checking for spaces	
letters++;
if (ch[i] >= 'A' && ch[i] <= 'Z')
upper++;
else if (ch[i] >= 'a' && ch[i] <= 'z')
lower++;
else if(ch[i] >='0' && ch[i] <='9')
digit++;
else if (ch[i] != ',' && ch[i] !=';' && ch[i] != '!' && ch[i] != ' ' && ch[i] != '.' && ch[i] != ':' && ch[i] != '?' && ch[i] != '"' && ch[i] != '-' && ch[i] != '+' && ch[i] != '/')
special++;
else if(ch[i]!=' ')
punct++;
else if (ch[i] == ' ')
space++; 
else if (digit >='0' && ch[i] !='9')
numbers++;
else if (ch[i] != '@'&& ch[i] != '#' && ch[i] != '$' && ch[i] != '%' && ch[i] != '^' && ch[i] != '*' && ch[i] != '(' && ch[i] != ')' && ch[i] != '_' && ch[i] != '+' && ch[i] != '{' && ch[i] !='}' && ch[i] != '<' && ch[i] != '>' && ch[i] !='~' && ch[i] != '`')
others++;
} 



printf ("\nNnumber of characters: %d", character);
printf ("\nNnumber of letters: %d", upper + lower);
printf ("\nNumber of Uppercase Letters: %d", upper);
printf ("\nNumber of Lowercase Letters: %d", lower);
printf ("\nNumber of Digits: %d", digit);
printf ("\nThe number of special characters: %d", special);
printf ("\nPunctuation Characters: %d", punct);
printf ("\nNumber of spaces: %d", space);
printf ("\nNumber of words: %d", space +1); 
printf ("\nNumber of numbers: %d" , digit / 2   );
printf ("\nNumber of others:  %d", others);

}


Output:


Enter The String : I like to Eat Yams @ moms.

Nnumber of characters: 20
Nnumber of letters: 18
Number of Uppercase Letters: 3
Number of Lowercase Letters: 15
Number of Digits: 0
The number of special characters: 1
Punctuation Characters: 1
Number of spaces: 7
Number of words: 8
Number of numbers: 0
Number of others: 0
--------------------------------
Process exited after 11.02 seconds with return value 0
Press any key to continue . . .



Need Help with the Number of Numbers and Number of Others! Not Fuctioning Properly!
Last edited on
Line 33: What are you trying to accomplish here? At line 26 you're using digit to count the number of numerics. At line 33 you're testing that counter (digit) to see if it is a numeric character. Those are two different things.

Should line 33 have read else if (ch[i] >= '0' && ch[i] <= '9') ? If so, you have a different problem. You've already checked for numerics at line 26, which if true, this else if will never be executed.

line 5: main() must be type int.

Lines 11, 41-51: It's poor style to mix C and C++ I/O. You should be using cout, not printf.
Last edited on
Hello marqual123,

My first question is this a C program or did you want a C++ program?

The C code you have I can not compile because of the "gets()" function. So I can not test the program to see what is happening.

The if/else statements look like they might work, but I would imagine that one or two of them are not working right.

As for opening a file I can help, but that would be C++ code because I am a little rusty with the C code that would be needed.

For someone just learning I find this code useful. You will need to include the header file "<fstream>" to use files:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
std::string iFileName{ "" };  // <--- Put the name of the input file here.

std::ifstream inFile;  // <--- "ifstream" says input file, so nothing else is need in the next line.

inFile.open(iFileName);

//  Always check to make sure the input has been opened.

if (inFile.is_open())
{
	std::cout << "\n File " << iFileName << " is open" << std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(2));  // <--- Needs header files "chrono" and "thread".
}
else
{
	std::cout << "\n File " << iFileName << " did not open" << std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(3));  // <--- Needs header files "chrono" and "thread".
	exit(1);
}


These lines can be shortened in the future, but for now it is easier to understand. Feel free to use this and you can change any of the variable names that you want. I found this to work best for me.

You should also post the input file so everyone who might work on this is using the same input to get the same output.

Hope that helps,

Andy
Hello marqual123,

After testing the program I found lie 50 to be a problem. The "digit / 2" does not work. You are dividing an int which could end up as a floating point answer and you are printing this as an int which will drop everything to the right of the decimal point and just print zero.

I tried changing the format specifier and that did not work. When I removed the "/ 2" it did print the right number. After all that I saw line 50 as being reduntant because you print the same information at line 45.

I still need to figure what "others" would be and test for that value.

Hope that helps,

Andy
Topic archived. No new replies allowed.