MY isdigit is not working!

Hey guys, so for some reason my isdigit is not detecting the numbers in a text file. Even tho the output shows the numbers. This program is supposed to count the amount of digits in the text file. I am not going to post the whole code since I worked hard on it, and it is a project due tonight. But the important stuff is here:

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include <iostream>
#include <iomanip>
#include <fstream>
#include <ctype.h> 

using namespace std;

char convUpperToLower(char);
char convLowerToUpper(char);
char convPunct(char);
char convSpecial(char);

int main()
{
ifstream inFile;

char ch,decode;
int i=0;
double total = 0,total2 = 0, total_2=0, total3 = 0,total4 = 0, total5 = 0, total6 = 0;
 



inFile.open( "mystery.txt" );
if( inFile.fail() )
  {
  cout << "Input file failed to open" << endl << endl;
  system("pause");
  exit(-1);
  }

inFile >> ch;

while ( inFile )	
 
  if(isupper(ch))
{
decode=convUpperToLower(ch);
cout<<decode;
total2++;
}
else if(islower(ch))
{
  decode=convLowerToUpper(ch);
  cout<<decode;
 total_2++; 
}
else  if(ispunct(ch))
{
      decode=convPunct(ch);
	  total4++;
}
 else 
{
decode=convSpecial(ch);
if(ch == '\n')
	total6++;
else
	total5++;

}

if (isdigit (ch))
{
decode=convPunct(ch);
	total3++;
}

	total++;
  inFile >> ch;
  }
  

inFile.close();

cout << "\n\n\n";

cout << "There are " << total << " characters in the article";

cout << "\n\n";

cout << "Alphabetic: "<< total- total3-total4-total5-total6; 

cout << "\n";
cout << "	Upper case: " << total_2;
cout << "\n";
cout << "	Lower case: " << total2;
cout << "\n\n";
cout << "Digits:" << total3;
cout << "\n\n";
cout << "Pun:" << total4;
cout << "\n\n"; 
cout << "Spaces:" << total5;
cout << "\n\n";
cout << "Lines:" << total6;

cout << endl << endl;
system("pause");

return 0;


and the function is:

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
char convPunct( char ch )
{
     char letter;
     switch(ch)
     {
      case ')':cout<<0;
      break;
      case '!':cout<<1;
      break;
      case '@':cout<<2;
      break;
      case '#':cout<<3;
      break;
      case '$':cout<<4;
      break;
      case '%':cout<<5;
      break;
      case '^':cout<<6; 
      break;
      case '&':cout<<7;
      break;
      case '*':cout<<8;   
      break;
      case '(':cout<<9;
      break;
      case '>':cout<<'.';
      break;
      case '<':cout<<',';
      break;
      case '/':cout<<'?';
      break;
      case ';':cout<<'!';
      break;
      case '+':cout<<'(';
      break;
      case '=':cout<<')';
      break;
    
     }
     return letter;
}
The only time you call convPunct is when isdigit(ch) returns true.

So, ch is guaranteed not to be any of the cases you have in the switch.
thanks for the response. I am sorry, but i honestly have no clue what you mean. Could you please point out the problem in code. Thanks
there's got to be somebody out there whom can help just point out the problem or push me in the right direction
1
2
3
4
5
if (isdigit (ch))
{
decode=convPunct(ch);
	total3++;
}


If it's a digit, convert punctuation. Does that make sense to you?
Ok so I created a new function:
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
char convDigit (char ch)
{
	 char letter;
	 switch(ch)
	 {	  
	  case '0':cout<<')';
	  break;
	  case '1':cout<<'!';
	  break;
	  case '2':cout<<'@';
	  break;
	  case '3':cout<<'#';
	  break;
	  case '4':cout<<'$';
	  break;
	  case '5':cout<<'%';
	  break;
	  case '6':cout<<'^';
	  break;
	  case '7':cout<<'&';
	  break;
	  case '8':cout<<'*';
	  break;
	  case '9':cout<<'(';
	  break;
	}

	return letter;
}


and then changed the if statement

1
2
3
4
5
if (isdigit (ch))
{
decode=convDigit(ch);
	total3++;
}


is that better? Or am I still doing something wrong?
Last edited on
Looks good to me. Does it do what you want?
Nope for some reason the output still shows 0 even tho our teacher said:

Any character in msg that was a digit ('0' ... '9') was converted as follows:

0 --> )
1 --> !
2 --> @
3 --> #
4 --> $
5 --> %
6 --> ^
7 --> &
8 --> *
9 --> (

I have no clue why it still shows Digits: as 0

the text file should have 21 Digits
Last edited on
closed account (DSLq5Di1)
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
char convDigit (char ch)
{
	 char letter;
	 switch(ch)
	 {	  
	  case '0':cout<<')';
	  break;
	  case '1':cout<<'!';
	  break;
	  case '2':cout<<'@';
	  break;
	  case '3':cout<<'#';
	  break;
	  case '4':cout<<'$';
	  break;
	  case '5':cout<<'%';
	  break;
	  case '6':cout<<'^';
	  break;
	  case '7':cout<<'&';
	  break;
	  case '8':cout<<'*';
	  break;
	  case '9':cout<<'(';
	  break;
	}

	return letter;
}
....that was a complete copy of what i posted above.....wait...are you talking about the letter holding variable?
Last edited on
closed account (DSLq5Di1)
Yes it was, the lines in bold indicate the problem.

are you talking about the letter holding variable?
Yup, see any problems?
Last edited on
what do u mean that the letter holding variable is wrong? Could you please point out a fix to the problem. I am very very confused right now
closed account (DSLq5Di1)
Well, the value returned from convDigit() is the value written to file, you are returning letter, but what does letter equal? you never assign a value to it.
Last edited on
How would I assign a value in the return section of a function? What would you do?
closed account (DSLq5Di1)
I assume you intended to return the converted character, correct me if I am 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
char convDigit (char ch)
{
	 char letter;
	 switch(ch)
	 {	  
	  case '0': letter = ')';
	  break;
	  case '1': letter = '!';
	  break;
	  case '2': letter = '@';
	  break;
	  case '3': letter = '#';
	  break;
	  case '4': letter = '$';
	  break;
	  case '5': letter = '%';
	  break;
	  case '6': letter = '^';
	  break;
	  case '7': letter = '&';
	  break;
	  case '8': letter = '*';
	  break;
	  case '9': letter = '(';
	  break;
	}
	cout << letter;

	return letter;
}
it did not work, but it's ok. I am limited on time, so I have to turn in with what I've got. Thanks for your help though. I really appreciated it.
Topic archived. No new replies allowed.