Thread: help with programs

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    22

    help with programs

    plz can i get help with that:
    Write a C++ program with a case structure.
    1.
    The program reads in a character from the user, then translates it into a different character according to the following rules:
    characters
    1. A, B, C will be translated to characters z, y, x respectively;
    characters
    2. 6, 7, 8, 9 will all be translated to character 5;
    3. the space character ' ' will be translated to underscore '_'
    4. all other characters remain unchanged.
    The program then displays the translated character
    2
    Write a C++ program that reads from keyboard 3 words, with proper input prompt, and then display them in the order of the word length (shortest word first).
    3
    Write a C++ program with a loop structure that reads in 5 integers one by one, calculate the sum of the even values that are read and display the final result.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    please read this and this before posting to the board. noone is going to do your homework for you, but specific questions are certainly welcome. a good start would be to post the code you have thus far and show where you are stuck.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  3. #3
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    The trick to ANY program is to write it a little at a time!

    Start-out with a little Hello World program, and add one or two lines at a time, test-compiling and test-running each time. This takes a little practice, you have to skip-around a bit... For example, if you just enter the first-half of a program, it won't compile....

    Sometimes it's helpful to put in some extra temporary cout statements, so you can "see' what your program is doing. For example, in your program you might have a temporary cout statement that says "User has input A, B or C... need to translate".

    Note - Remember this technique when you start learning functions. Start-out with an empty do-nothing function. (You might have to make the function return a "fake" value.) When the program compiles and runs, make the function actually do something.

    When As you gain experience, you can write bigger chunks of code between testing. But, your programs will get bigger too. So, you'll almost never write an entire program before testing!



    In your first program, you can replace "Hello World" with "Enter a character".

    When that works, write the code to get a character from the user, and the code at the end of the program to display it (or to display the translated character).

    Next, you can write (and test) the if-statement to check for A, B, or C.

    Etc., etc., etc.



    You should also plan-out your program with a flow-chart, or psudo-code, etc.

    I usually "outline" the program in comments before I get started, like this:

    // Read a character from the user ;

    // Is the Character ABC?;

    // If so, translate it to x, y,z;

    // Is the character 6, 7, 8. or 9?;

    // If so, translate to 5;

    // Is it a space?

    // If so, translate it to underscore;

    // Display the result.

    //END



    After I have the comments, I start filling-in and testing the actual code. Usually the comments need editing too.... There's usually some difference between saying what the program is supposed to do, and proper comments explaining what the program is actually doing.

    FYI - That's an informal technique that works for me, but it's not the kind of formal design and documentation that's tought in computer science or required in some formal organizations.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    22
    i tried the part one and i still got problems doing it:
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        char ch;
        cout << " enter a character:";
        cin >>ch;
        if (ch =='6'||ch == '7'||ch == '8'|| ch == '9')
        {
        ch= '5';
        cout << "character converted to :" << ch; 
    }
    if ( ch == 'A')
    
     ch='z';
    
     else if (ch == 'B')
    
     ch ='y';
    
     else if (ch == 'C')
    
     ch ='x' ;
     else if ( ch == 'space')
      ch = '_';
     cout << "character converted to:" <<ch;  
     else 
     cout << " the character is:"<<ch;
    }
    system("PAUSE");
        return 0;
    }
    that's what i've done can i get your help

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> i tried the part one and i still got problems doing it:

    remember: informative posts usually get better responses: 'I keep getting a compilation error X on line Y' or 'when I print debug statements the logic/runtime error seems to occur somewhere around line Z' rather than 'It doesn't work'.

    Code:
     else if ( ch == 'space')
      ch = '_';
     cout << "character converted to:" <<ch;
    - 'space' is not a character, assuming you meant ' '.
    - the statements following your else if clause must be grouped together either with brackets or by separating them with commas - otherwise, only the first statement gets associated with it
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    22
    i tried to tape ' ' insread of 'space' but i got an error message saying typing empty value constant how do i solve this problem
    plz i need help as soon as possible

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Put a space between the single quotes and it won't be an empty value constant.

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    22
    i did do that but i still got the same message

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    22
    but i tried this :
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        char ch;
        cout << " enter a character:";
        cin >>ch;
        if (ch =='6'||ch == '7'||ch == '8'|| ch == '9')
        {
        ch= '5';
        cout << "character converted to :" << ch; 
    }
    if ( ch == 'A')
    ch='z';
    else if (ch == 'B')
    ch ='y';
    else if (ch == 'C')
    ch ='x' ;
     cout << "character converted to:"<< ch; 
     if ( ch= ' ')
     {
        ch = '_';
          cout <<"character converted to:";
           ch=cin.get();
           }   
    system("PAUSE");
        return 0;
    }
    but when i type ' ' int the output nothing happen

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> if ( ch= ' ')
    That should be ==.

    Also, you cannot read in a space with cin >>, so ch will never be ' ' in that code. You need to read in with cin.get to be able to read in a space.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    22
    can i get any hints in doing this
    Code:
    Write a C++ program that reads from keyboard 3 words, with proper input prompt, and then display them in the order of the word length (shortest word first).

  12. #12
    Registered User
    Join Date
    Apr 2006
    Posts
    22
    thank you for your help daved , Sebastiani and
    DougDbug

  13. #13
    char main() RoshanX's Avatar
    Join Date
    Mar 2003
    Posts
    68
    just a tip, it's always a good idea to reverse the comparion

    ex. if ('5'==ch), this would prevent errors like , if (ch='5')
    First there was God. He was quite lonely, so he created Dennis.
    Dennis was unimpressed with God.
    So, . . . God created Brian..........Khan Klatt
    http://www.clifford.at/fun/GOD

  14. #14
    Registered User
    Join Date
    Apr 2006
    Posts
    22
    i need a help how to create a loop to make the sum of even number

  15. #15
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    the sum of even number
    Pray tell, what does that mean? The sum of an even number of numbers? To sum to an even number? To even out an uneven number?
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recommend upgrade path for C programs
    By emanresu in forum C Programming
    Replies: 3
    Last Post: 11-22-2007, 07:32 AM
  2. I never Finish my programs :(
    By epidemic in forum C++ Programming
    Replies: 11
    Last Post: 04-02-2007, 12:35 PM
  3. POSIX/DOS programs?
    By nickname_changed in forum C++ Programming
    Replies: 1
    Last Post: 02-28-2003, 05:42 AM
  4. executing c++ programs on the web
    By gulti01 in forum C++ Programming
    Replies: 4
    Last Post: 08-12-2002, 03:12 AM
  5. Release MFC Programs & Dynamic MFC DLL :: MFC
    By kuphryn in forum Windows Programming
    Replies: 2
    Last Post: 05-18-2002, 06:42 PM