C++

Discussion in 'Computer Science & Culture' started by BiteMe, Feb 19, 2003.

Thread Status:
Not open for further replies.
  1. BiteMe Registered Member

    Messages:
    5
    Hi all... I need to write a C++ program saying
    "Are you single or married? (s/m)?"
    It needs to accept the 's' or 'm' in it's capital or lowercase form, using an if-else statement...and if a different letter is entered, such as a 'q', the output must read "invalid input, please re-enter." I can't do it, please post some help, ASAP. THanks.
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. pragmathen 0001 1111 Registered Senior Member

    Messages:
    452
    What about this?

    <b>void</b> main(&nbsp; )
    {
    &nbsp;&nbsp;&nbsp;<b>char</b> var;
    &nbsp;&nbsp;&nbsp;cout << "Are you single or married (s/m)?" << endl;
    &nbsp;&nbsp;&nbsp;cin >> var;
    &nbsp;&nbsp;&nbsp;var = tolower(var);
    &nbsp;&nbsp;&nbsp;<b>while</b> (var != 'm' && var != 's')
    &nbsp;&nbsp;&nbsp;{
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout << "Invalid input, please re-enter." << endl;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin >> var;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var = tolower(var);
    &nbsp;&nbsp;&nbsp;}
    &nbsp;&nbsp;&nbsp;<b>if</b> (var == 'm')
    &nbsp;&nbsp;&nbsp;{
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout << "You are married." << endl;
    &nbsp;&nbsp;&nbsp;}
    &nbsp;&nbsp;&nbsp;<b>else if</b> (var == 's')
    &nbsp;&nbsp;&nbsp;{
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout << "You are single." << endl;
    &nbsp;&nbsp;&nbsp;}
    &nbsp;&nbsp;&nbsp;getch();
    }
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. dudespin Registered Member

    Messages:
    6
    ummm, I don't think we should be doing each other's homework here... shouldn't this be used more for specific questions like how the hardware talks to the software and other computer-related stuff?

    I've programmed a tile based game engine in C++ so seriously, you gotta do your own work so it's 2nd nature by the time you do something big
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. ConsequentAtheist Registered Senior Member

    Messages:
    1,579
    As someone who's been programming since the mid-70s, I tend to agree. If you have a problem, the very least you should do is post the code (segment) for feedback.

    btw - interesting coding question:
    • Given byte variables x and y,
    • swap their contents without using a 3rd (temporary) variable.

    Please Register or Log in to view the hidden image!

     
    Last edited: Feb 26, 2003
  8. Ender Registered Senior Member

    Messages:
    294
    Given byte variables x and y,
    swap their contents without using a 3rd (temporary) variable

    Couldn't you make a pointer to them and make anytime thay are accessed make them hit the pointer and go to the other one?

    It doesn't swap but it has the same effect!
     
  9. dudespin Registered Member

    Messages:
    6
    #include <iostream.h>

    void swap(int *a, int *b)
    {
    int *x = &a, *y = &b;
    *a = y;
    *b = x;

    }
    void main(void)
    {
    int a = 1, b = 2;

    swab(&a, &b);

    cout << a << " " << b << endl;
    }

    /* darnit, it seems simple until I do it. Then it seems impossible. I used 4 variables in the function... I seriously think there's a way to do this using memory addresses but have yet to figure that out. I will keep my brain occupied with this one in the near future.
    */

    I could be cheap and use
    void swap(int a, int b)
    {
    a = b;
    b = ::a;
    }
    // but I don't count that as being very effective. Unless maybe there's a dynamic way to use externs with the same name, heh. (with the same name makes it kinda undynamic). Also ::a can count as a 3rd variable.
     
  10. AntonK Technomage Registered Senior Member

    Messages:
    1,083
    Swapping vars...

    only for integers.

    A ^= B;
    B ^= A;
    A ^= B;

    or

    A = A ^ B;
    B = B ^ A;
    A = A ^ B;


    Example:
    A 1101
    B 0011

    A = A ^ B;

    A 1110
    B 0011

    B = B ^ A;

    A 1110
    B 1101

    A = A ^ B;

    A 0011
    B 1101

    -AntonK
     
  11. ConsequentAtheist Registered Senior Member

    Messages:
    1,579
    Re: Swapping vars...

    Outstanding!
     
  12. Blindman Valued Senior Member

    Messages:
    1,425
    void swap(int *a,int *b)
    {
    int c; // temp var
    c = *a;
    *a = *b;
    *b = c;
    }
     
Thread Status:
Not open for further replies.

Share This Page