Thread: Quick Question, Basic Programing

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    35

    Quick Question, Basic Programing

    hi all,

    since i am new to these forums i hope this is the right place to post this question. i am starting to teach myself how to program using c++ and the program i was trying to make did not complete all the required tasks.

    in the below program it will only give the second out come for the second task: What where you thinking. You now have a virus. How to i change this program so it will say the first out come for odd numbers and the second for even.


    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
    char name;
    
    cout<<"What is the first letter of your operating system? ";
    cin>>name;
    cin.ignore();
    cout<<"The first letter of your operating system is "<< name <<"\n";
    
    int number;
    
    cout<<"Please input a number 1-10: ";
    cin>>number;
    cin.ignore();
    if ( number == ( 1, 3, 5, 7, 9 ) ) {
        cout<<"You read my mind!\n";
    }
    else if ( number != (1, 3, 5, 7, 9 ) ) {
        cout<<"What were you thinking. You now have a virus.\n";
    }
    cin.get();
    
    
    }

    any help is appreciated. thank you in advance.

  2. #2
    Registered User
    Join Date
    Nov 2008
    Posts
    41
    This:
    Code:
    number == ( 1, 3, 5, 7, 9 )
    does not work, you can only compare one number at a time. So you would have to use the || operator like this:
    Code:
    number == 1 || number == 3 || number == 5 ....
    But for this purpose it is easier to use the mod operator %.
    For odd numbers, n%2 = 1 and for even numbers, n%2 = 0.
    So it would be
    Code:
    if ( number%2 == 1 ) {
        cout<<"You read my mind!\n";
    }
    else if ( number%2 == 0 ) {
        cout<<"What were you thinking. You now have a virus.\n";
    }

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    35
    Thanks alot. That really helped me out. I couldnt figure it out but know it makes sense why it didnt work before.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. basic question about global variables
    By radeberger in forum C++ Programming
    Replies: 0
    Last Post: 04-06-2009, 12:54 AM
  2. Quick question about types...
    By Elysia in forum C++ Programming
    Replies: 32
    Last Post: 12-07-2008, 05:39 AM
  3. Questions on basic Quick Sort
    By Weng in forum C++ Programming
    Replies: 4
    Last Post: 12-16-2003, 10:06 AM
  4. Quick question: exit();
    By Cheeze-It in forum C Programming
    Replies: 6
    Last Post: 08-15-2001, 05:46 PM