I am writing a program for a class and I am having a problem that seems very simple yet I can't place my finger on it. I have to decode a message and everything decodes right except what is already there and should be showing up in the output which is things such as quotations and dashes.

the input file and sample output can be seen here

any help be great

http://www.cs.niu.edu/~abyrnes/csci240/pgms/240pgm6.htm

#include <iostream>
#include <iomanip>

using namespace std;

char convUpper ( char );
char convLower ( char );
char convPunct ( char );
char convDigit ( char );
char checkSpecial ( char );

int main()
{
    char ch;
    
    cin >> ch;
    while (cin)
    {        
    if ( isupper(ch) )
	ch = convUpper(ch);
	
	else if ( islower(ch) )
	ch = convLower(ch);
	
	else if ( ispunct(ch) )
	ch = convPunct(ch);
	
	else if ( isdigit(ch) )
	ch = convDigit(ch);
	
	else
	ch = checkSpecial(ch);
		
	//cout << ch;	
    cin >> ch;
    //cout << ch;
    }
    
    
  system ("pause");
  return 0;
}

/***************************************************************/
char convUpper ( char ch )
{
     if (ch == 'A')
        return ('z');
     else
         return (ch + 31);
}
/***************************************************************/
char convLower ( char ch )
{     
     if (ch == 'a')
        return ('Z');
     else
        return (ch - 33);
}
/***************************************************************/
char convPunct ( char ch )
{
     switch ( ch )
     {
         case '!':
         return '0';
         break;
         case '@':
         return '1';
         break;
         case ')':
         return '2';
         break;
         case '(':
         return '3';
         break;
         case '*':
         return '4';
         break;
         case '%':
         return '5';
         break;
         case '&':
         return '6';
         break;
         case '$':
         return '7';
         break;
         case '^':
         return '8';
         break;
         case '#':
         return '9';
         break;
         }
}
/***************************************************************/
char convDigit ( char ch )
{
     switch (ch)
     {
     case '0':
     return ',';
     break;
     case '1':
     return '.';
     break;
     case '2':
     return '$';
     break;
     case '3':
     return '&';
     break;
     case '4':
     return '%';
     break;
     case '5':
     return '*';
     break;
     case '6':
     return '(';
     break;
     case '7':
     return ')';
     break;
     case '8':
     return '?';
     break;
     case '9':
     return '!';
     break;
     }
}
/***************************************************************/
char checkSpecial ( char ch )
{
    if ( (int) ch == 30 )
    return (32);
    if ( (int) ch == 31 )
    return ('\n');    
}

Recommended Answers

All 4 Replies

I think your convPunct( ) should return any character that's not caught by the switch cases in its unchanged state.
Either use the switch's default: or have a return statement after the switch.

Remember that ispunct( ) will be true for all punctuation, not just the 10 particular ones you're dealing with.

Also, you don't need all those break statements. When return is executed, you leave the functions immediately.

ya I had to return after the switch I knew it was something obvious I wasn't seeing thanks

I do not think checkSpecial is coded as per your requirement.

a blank/space was encoded as an ASCII 30
a newline character was encoded as an ASCII 31

char checkSpecial ( char ch )
{
    if ( (int) ch == 30 )
    return (32);
    if ( (int) ch == 31 )
    return ('\n');    
}

It looks to be doing the right thing. A change I would make, instead of returning the arbitrary looking value 32, I'd use return ( ' ' ); and let the system assign whatever encoding value a space is, as OP did with the newline.

And again, this function should return any other character unchanged.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.