My First If Code, Question? :P

This is a discussion on My First If Code, Question? :P within the C++ Programming forums, part of the General Programming Boards category; Well I was goofing around in C++ today and I made my first IF code. Code: #include <iostream> int main() ...

  1. #1
    Programmer
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    33

    My First If Code, Question? :P

    Well I was goofing around in C++ today and I made my first IF code.

    Code:
    #include <iostream>
    
    int main()
    {
        std::cout << "Please Enter 1 or 2: ";
        
        int a;
        std::cin >> a;
        std::cin.ignore();
        
        if (a == 1)
        {
              std::cout << "You Typed: 1";
        }
        
        if (a == 2)
        {
              std::cout << "You Typed: 2";
        }
        
        if (a > 2)
        {
            std::cout << "You Didn't Type 1 or 2!";
        }
        
        if (a < 1)
        {
            std::cout << "You Didn't Type 1 or 2!";
        }
        
        std::cin.get();
        return 0;
    }
    What it's supposed to do is see if you typed 1 or 2 and if you didn't it would tell you so. I wan'ted to know how to restart it all from the start if they didn't type 1 or 2? Can anyone give me some info on how I would go about doing that?

    In Visual Basic the IF things where a lot easier for me,


    (Sub)
    If Blah = Blah Then
    (Statement)
    End If
    (Or ElseIf or Else)
    End Sub

    In someways the C++ seems faster to me, but really its all the same in code so far for me, since in VB the sub parts are typed for you. Just need to get use to the "{" "}" and the "( )" things in C++.

    Thanks!
    (Expert Visual Basic Programmer)
    (Newbie C/C++ Programmer)

  2. #2
    ^ Read Backwards^
    Join Date
    Sep 2005
    Location
    Earth
    Posts
    282

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    yeah, you could put everything into an infinite loop, i do this often. take a look:
    Code:
    #include <iostream>
    
    using namespace std;  //so you dont haave to always type std::(cout/cin)
    
    int main()
    {
        while(1)    //This is a while() loop, it runs what is in the {} 
                    //until its condition () is false or it is terminated, 
                    //while(1) is always true, we call this an infinite loop.
        {
            cout << "Please Enter 1 or 2(Type 0 to Exit): ";
            
            int a;
            cin >> a;
            cin.ignore();
            
            if (a == 1)
            {
                cout << "You Typed: 1";
                cin.ignore();      //pauses
                continue;          //this will continue the loop
            }
            
            if (a == 2)
            {
                cout << "You Typed: 2";
                cin.ignore();
                continue;
            }
            
            //i added the exit option in, a while loop like this will go on forever
            if(a==0)
            {
                cout<<"Press Enter to Exit...";
                cin.ignore();
                break;     //break does as it implies, breaks a loop
            }
                
            if (a > 2)
            {
                cout << "You Didn't Type 1 or 2!";
                cin.ignore();
                continue;
            }
            
            if (a < 1)
            {
                cout << "You Didn't Type 1 or 2!";
                cin.ignore();
                continue;
            }
            
        }    
        cin.get();
        return 0;
    }
    Good Luck!

    PS-Try to type in 1.5 or 1.2 What happens? Nothing! You could do a couple things here. Heres what i would do, replace:
    Code:
    ...
            if (a > 2)
            {
                cout << "You Didn't Type 1 or 2!";
                cin.ignore();
                continue;
            }
            
            if (a < 1)
            {
                cout << "You Didn't Type 1 or 2!";
                cin.ignore();
                continue;
            }
    ...
    With:
    Code:
    ...
    if(a!=1 || a!=2)
    {
           cout<<"You didnt type 1 or 2!"
           cin.ignore();
           continue;
    }
    ...
    The != means doesnt equal, and the || means or.

    Good Luck!

    Check out the link, there are a couple other kinds of loops.
    Last edited by Junior89; 11-29-2005 at 06:47 PM.

  4. #4
    Programmer
    Join Date
    Nov 2005
    Location
    Canada
    Posts
    33
    Alright, thanks your all such a big help!
    (Expert Visual Basic Programmer)
    (Newbie C/C++ Programmer)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Hi all i have a question about a code
    By iweapons in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2007, 11:05 AM
  2. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  3. End of Code Loop Question
    By JuanSverige in forum C++ Programming
    Replies: 1
    Last Post: 04-08-2003, 10:35 AM
  4. EXE to code? Decompile question.
    By RoD in forum Game Programming
    Replies: 4
    Last Post: 10-01-2002, 05:28 PM
  5. question about my code
    By killerasp in forum C++ Programming
    Replies: 7
    Last Post: 02-18-2002, 07:05 PM

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