Test for whitespace in a file

This program I need to test for whitespace and newline characters. I'm given a text file with letter and symbols that the program converts so it is readable. I'm having a problem with writing a function #4 for isspace. Can someone please help me figure this out and lead me in the right direction??


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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include <iostream>
#include <iomanip>
#include <cmath>
#include <fstream>

using namespace std;

#define WHITESPACE_CHAR    .01
#define COST_DIGIT         .03
#define COST_LOWERCASE     .05
#define COST_UPPERCASE     .07
#define COST_PUNCTUATION   .11

/*****************************************************************
                  Function Prototypes
*****************************************************************/

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


int main()
{

ifstream inFile;

char ch, convCh;



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

inFile >> ch;

while ( inFile )
  {
  //Process the 1 character by decoding it. A cascading if statement should
  //be used to test the "type" of character (upper case, lower case, digit,
  //punctuation, etc...) and call the appropriate decoding function and
  //increment the appropriate counter
  
  if ( isalpha (ch) )
     {
	 if ( isupper (ch) )
	    {
		convCh = convUpperToLower (ch);
		}
	 else 
	    {
		convCh = convLowerToUpper (ch);
		}
	 }  
  else if ( ispunct (ch) )
     {
     convCh = convPunct (ch);
	 }	   
  else if ( isspace (ch) )
    {
    convCh = convSpecial (ch);
	}	  
	 
  //Display the decoded character
  cout << convCh;

  inFile >> ch;
  }

inFile.close();



}
//***** Functions are coded below main() *****

/*********************************************************
Function 1:
This function will be called for any single character that 
is an uppercase character.  

arguments:
The character to be decoded and returns that character.
*********************************************************/

char convUpperToLower (char ch) 
   {
   char upper;
      
	  if ( ch == 'A' || ch == 'B' || ch == 'C' )
	     {
		 upper = tolower (ch) + 23;
		 }
      else
	     {
	     upper = tolower (ch) - 3;
		 }
   return upper;
   } 

/*********************************************************
Function 2:
This function will be called for any single character that 
is an lowercase character.  

arguments:
The character to be decoded and returns that character.
*********************************************************/

char convLowerToUpper (char ch)
   {
   char lower;
   
      if ( ch == 'x' || ch == 'y' || ch == 'z' )
	     {
		 lower = toupper (ch) - 23;
		 }
      else
	     {
		 lower = toupper (ch) + 3;
		 }
   return lower;
   }

   
/*********************************************************
Function 3:
This function will be called for any single character that 
is a punctuation mark or digits.  

arguments:
The character to be decoded and returns that character.
*********************************************************/

char convPunct (char ch)
   {
   char punct;
      
   switch (ch)
	   {
	   case '>':
	     punct = '.';
		 break;
	   case '<':
	     punct = ',';
		 break;	  	   
	   case '/':
	     punct = '?';
		 break;
	   case '\"':
	     punct = '\'';
		 break;
	   case '\'':
	     punct = '\"';
		 break;
	   case ';':
	     punct = '!';
		 break;
	   case '+':
	     punct = '(';
		 break;
	   case '=':
	     punct = ')';
		 break;
	   case ')':
	     punct = '0';
		 break;
	   case '!':
	     punct = '1';
		 break;
	   case '@':
	     punct = '2';
		 break;
	   case '#':
	     punct = '3';
		 break;
	   case '$':
	     punct = '4';
		 break;
	   case '%':
	     punct = '5';
		 break;
	   case '^':
	     punct = '6';
		 break;
	   case '&':
	     punct = '7';
		 break;
	   case '*':
	     punct = '8';
		 break;
       case '(':
	     punct = '9';
		 break;
	   default:
	      punct = '_';
	      break;
	   
       }
   return punct;
   }
     
/*********************************************************
Function 4:
This function will be called for any single character that 
is not an uppercase, lowercase, or punctuation character.  

arguments:
The character to be decoded and returns that character.
*********************************************************/

char convSpecial (char ch)
   {
   char special;
   
      if ( ch == 15 || ch == 16 )
	     {
		 special = ;
		 }
      else if (
	     {
		 
		 }
   return special;
   }

Last edited on
So your program only deals with alpha characters A, B, C, x, y, z ?

Are you aware there is are built in functions toupper and tolower, or was that part of the assignment?

To check for whitespace such as spaces, tabs, newlines etc find out their ascii codes like you have done in the convSpecial function.

With the convPunct function, why the translation of the chars to something else?

If you really want to cheat, read this:

http://zanasi.chem.unisa.it/download/C.pdf


It is really old, and for the C language, but still a good reference I think. You should be aware they are many different ways of doing things in C++. Newbies quite often write C programs with some cout statements, not realising that it would be done quite differently in C++.

Good Luck !!!
everything I have done up to the isspace function has been done the way the teacher is requiring.

This is the file that we have to convert:

tKHQxGYHUWLVLQJxJHPDJDCLQHUHOHDVHGLWVSLFNVIRUWKHEHVW!))DGFDPSDLJQVRIWKH@)WKFHQWXUB<LWZDVQRVXUSULVHWKDWWKHZRUOGRIEHHUDGYHUWLVLQJZDVZHOOUHSUHVHQWHG>xIWHUDOO<IHZFDQIRUJHWyXEEDpPLWKDQGaLFNyXWNXVDUJXLQJWKDWHWHUQDOGHEDWH<'qDVWHVdUHDW--iHVVcLOOLQJ>'iLNHZLVH<PDQBDEHHUGULQNHUFDQVWLOOZKLVWOHWKDWLQIHFWLRXVMLQJOH<'eHBjDEHO--yODFNiDEHO<'WKRXJKWKHSRSXODUWHOHYLVLRQFRPPHUFLDOVKDYHQRWDLUHGIRU#)BHDUV>pR<ZKDWPDGHWKHVHDQGRWKHUFODVVLFEHHUFRPPHUFLDOVJUHDW/pXUHOB<IURPWKHEHHUPDNHU"VVWDQGSRLQW<DFRPPHUFLDO"VVXFFHVVFDQXOWLPDWHOBEHMXGJHGEBRQOBRQHFULWHULRQ:LWVLPSDFWRQEHHUVDOHV>yXW<ZH<WKHRIW-MDGHGYLHZHUV<WDN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 15 to a space and 16 for a new line. Anything else will remain the same
Ok, so you are decoding a file.

So for your question, a char is just a small int, so to convert a char to some other char, just assign it the ascii value of the char you want to convert to. That is how this works:

lower = toupper (ch) - 23;

the variable ch is a small int which has 23 subtracted from it, which gives another char.
i doing the same one... i think i did the ASCII 15 and 16 right though, but i get unneeded additional characters following the letters A B C x y z such as, "?, {". help would be nice

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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
  
#include <iostream>
#include <iomanip>
#include <fstream>

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,total2,total3,total4;
 



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

inFile >> ch;

while ( inFile )
  {
  //Process the 1 character by decoding it. A cascading if statement should
  //be used to test the "type" of character (upper case, lower case, digit,
  //punctuation, etc...) and call the appropriate decoding function and
  //increment the appropriate counter
  
  //Display the decoded character
  
 
  if(isupper(ch))
{
decode=convUpperToLower(ch);
cout<<decode;

}
 if(islower(ch))
{
  decode=convLowerToUpper(ch);
  cout<<decode;
  
}
  if(ispunct(ch))
{
      decode=convPunct(ch);
}
 else 
{
decode=convSpecial(ch);


}

  inFile >> ch;
  }
  

inFile.close();

 

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

return 0;
}//end main



char convUpperToLower( char ch )
{
     char letter;
    letter=tolower(ch-3);
    switch(ch)
    {
    case 'A':cout<<'x';
    break;
    case 'B':cout<<'y';
    break;
    case 'C':cout<<'z';
    break;
}
    
return letter;
}

char convLowerToUpper( char ch)
{
     char letter;
     letter=toupper(ch+3);
     switch(ch)
     {
      case 'x':cout<<'A';
      break;
      case 'y':cout<<'B';
      break;
      case 'z':cout<<'C';
      break;
      }
     
     return letter;
}

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;
}
char convSpecial( char ch )
{
     char letter;
     if(ch==15)
     {
      cout<<" ";
     }
     else if (ch==16)
     {
          cout<<"\n"<<endl;
     }
}
 

 
Welcome to the club cody0023. I have been having the same exact problem. I went to the lab session but they were of no help to me. Since we have a very similar layout I can make an educated guess and say that I think our functions are calling the same holding variable through out the code.

Example:

char letter;
1
2
3
char convUpperToLower( char ch )
{
     char letter;


1
2
3
char convPunct( char ch )
{
     char letter;


But yet again I am not 100% sure. I have been having troubles with my totals, since I end up couting junk values instead of actual values *sigh*
Topic archived. No new replies allowed.