Problem with my little program.

This is a discussion on Problem with my little program. within the C++ Programming forums, part of the General Programming Boards category; Hi Could you guys help me out with the following code: Code: #include <iostream> using namespace std; int main() { ...

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    9

    Problem with my little program.

    Hi

    Could you guys help me out with the following code:



    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
      char thisisacharacter;
      int X;
      int Y;
      
      cout<<"Are you X  or Y ? ";
      cin>> thisisacharacter;
      cin.ignore();
      if ( thisisacharacter == Y ) {
                           cout<<"You are a male!\n";
                           }
      if (thisisacharacter == X ) {                
           cout<<"you are a female!\n";
           }
      cin.get();
    }
    i really cant understand what is wrong, ive tryed many things to fix.

  2. #2
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    What is your error message(s) from your compiler?
    And what compiler are you using?

  3. #3
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Code:
    if ( thisisacharacter == Y )
    You might want to compare thisisacharacter to a character like 'X' or 'Y' not X or Y which are integers that you never initialized.
    Don't quote me on that... ...seriously

  4. #4
    Registered User
    Join Date
    Jun 2007
    Posts
    24
    Bra0407 is right you have to say what x and y equal

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    9
    i can compile it but it doesn't work the way i want.
    i wanted to make it so that when the program asks "Are you X or Y ? "
    and i type in Y it will say "You are a male" and when i type X " You are a female".
    so i thought when i make it like this it will work
    Code:
    if ( thisisacharacter ==  X) // it will say that about the female and
     if ( thisisacharacter ==  Y) it will say that about male
    but when i type any character or number it will cout only the first sentence.

    i tried it like this now:
    Code:
     if ( thisisacharacter == "X"
    but i didn't work
    Last edited by RainbowStar; 07-08-2007 at 10:09 AM.

  6. #6
    Internet Superhero
    Join Date
    Sep 2006
    Location
    Denmark
    Posts
    964
    Quote Originally Posted by RainbowStar View Post
    i can compile it but it doesn't work the way i want.
    i wanted to make it so that when the program asks "Are you X or Y ? "
    and i type in Y it will say "You are a male" and when i type X " You are a female".
    so i thought when i make it like this it will work
    Code:
    if ( thisisacharacter ==  X) // it will say that about the female and
     if ( thisisacharacter ==  Y) it will say that about male
    but when i type any character or number it will cout only the first sentence.

    i tried it like this now:
    Code:
     if ( thisisacharacter == "X"
    but i didn't work
    In the code you posted, the user is entering a value into a char, and you then compare the char to 2 uninitialized integers. That doesn't make sense, what you want to do is compare the char, to a constant like "X" or "Y".

    So instead of doing this:

    Code:
    int X, Y;
    char input;
    
    std::cin >> input;
    if(input == X)
    std::cout << "Male";
    
    if(input == Y)
    std::cout << "Female";
    You should be doing something like this:

    Code:
    char input;
    
    std::cin >> input;
    if(input == 'X')
    std::cout << "Male";
    
    if(input == 'Y')
    std::cout << "Female";

  7. #7
    Registered User
    Join Date
    Jul 2007
    Posts
    9
    thanks it worked ^^.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi Thread Program Problem
    By ZNez in forum C Programming
    Replies: 1
    Last Post: 01-03-2009, 11:10 AM
  2. Program Termination Problem
    By dacbo in forum C Programming
    Replies: 3
    Last Post: 01-23-2006, 02:34 AM
  3. Inheritance and Dynamic Memory Program Problem
    By goron350 in forum C++ Programming
    Replies: 1
    Last Post: 07-02-2005, 03:38 PM
  4. Replies: 20
    Last Post: 06-13-2005, 12:53 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21