C++ help - swtich statement error???

Discussion in 'C++' started by Vani3863, Aug 28, 2010.

  1. Vani3863

    Vani3863 New Member

    Joined:
    Aug 28, 2010
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi All,

    I'm a relative newbie having trouble completing a program that is required to read the text character by character from a .txt file and to then encode certain characters to something thing else. It is then supposed to be output to a separate .txt

    What seems to happen is that not all the letters that need to be changed are being changed and I can't get it to include the 1 before the encoded letter as specified in the question.

    Why has it worked for some letters and not the others? What changes should I make? or am I going about this the wrong way? Please help by putting me in the right direction. I assumed the switch statement would be the easiest way to do this, but if I'm wrong can someone please let me know what section to read up so that I can answer this question?

    The Question Basics:
    Write a program that reads an input file with a letter character by character. Change the following characters:

    t (or T) changes to 1Y
    h (or H) changes to 1O
    j (or J) changes to 1X
    d (or D) changes to 1B
    a (or A) changes to 1S
    p (or P) changes to 1M
    i (or I) changes to 1Q

    The rest of the characters remain the same. Read the file character by character and write the character (if it stays the same) to the output file or write the changed version to the output file.

    Input File:
    "Dear Julia,
    You are the most beautiful girl that I have ever seen. I was wondering if
    you would like to come and visit me. My mother will make us pancakes with
    ice cream. My dog, Bella, just had three beautiful puppies. Mom says I may
    only keep one of them. I would,like you to help me choose one, because they
    are all so cute and adorable. And just because you are my special friend,
    you may also have one if you want.
    Your friend,
    Hector."


    Output File:
    eruQ,o r O oYeuQu QlOY Sevren S oBrn fo ol Qeooen QQ e yoOrQlSesScksQOc rS.yo,el,uYS Oe euQu uMe.o Ss S
    nyeMn fOm ol,Qeo oeMeOoen,eSs Oyr l ouen Brbe n uYeSs o r yMcS reB
    o S loSen fo SY
    orreB
    eYrr



    My Code:
    Code:
    int main()
    {
        char next, org_char, enc_char;  //declare variables of type char
        
        ifstream in_stream; //declaration for input stream
        ofstream out_stream; //declaration for output stream
        
        in_stream.open("letter.txt"); //open and check input file opening
        if (in_stream.fail())
        {
            cout  next)
        {
            in_stream.get(org_char);
            
                switch(org_char)
                {
                    case 'a':
                    case 'A':
                        enc_char = '1S';
                        break;
                    case 'd':
                    case 'D':
                        enc_char = '1B';
                        break;
                    case 'h':
                    case 'H':
                        enc_char = '1O';
                        break;
                    case 'i':
                    case 'I':
                        enc_char = '1Q';
                        break;
                    case 'j':
                    case 'J':
                        enc_char = '1X';
                        break;
                    case 'p':
                    case 'P':
                        enc_char = '1M';
                        break;
                    case 't':
                    case 'T':
                        enc_char = '1Y';
                        break;
                    default:
                        enc_char = org_char;
                        break;
                }
    
            out_stream.put(enc_char);
            next++;
        }
        in_stream.close();   //function to close in_stream 
        out_stream.close();  // function to close out_stream
        
        return 0;
    }
    Compiler Log:
    Compiler: Default compiler
    Executing g++.exe...
    g++.exe "C:\unisa\cos112v\Question4.cpp" -o "C:\unisa\cos112v\Question4.exe" -L"C:\unisa\mingw\lib"
    C:\unisa\cos112v\Question4.cpp:39:32: warning: multi-character character constant
    C:\unisa\cos112v\Question4.cpp:43:32: warning: multi-character character constant
    C:\unisa\cos112v\Question4.cpp:47:32: warning: multi-character character constant
    C:\unisa\cos112v\Question4.cpp:51:32: warning: multi-character character constant
    C:\unisa\cos112v\Question4.cpp:55:32: warning: multi-character character constant
    C:\unisa\cos112v\Question4.cpp:59:32: warning: multi-character character constant
    C:\unisa\cos112v\Question4.cpp:63:32: warning: multi-character character constant

    Execution terminated
    Compilation successful
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice