1. Head to the 33rd Modeling Contest Poll and drink to your heart's desire.
    Dismiss Notice
  2. Choose your means of doom in the 17th Mini Mapping Contest Poll.
    Dismiss Notice
  3. A slave to two rhythms, the 22nd Terraining Contest is here.
    Dismiss Notice
  4. The heavens smile on the old faithful. The 16th Techtree Contest has begun.
    Dismiss Notice
  5. The die is cast - the 6th Melee Mapping Contest results have been announced. Onward to the Hive Cup!
    Dismiss Notice
  6. The glory of the 20th Icon Contest is yours for the taking!
    Dismiss Notice
  7. Check out the Staff job openings thread.
    Dismiss Notice
Dismiss Notice
60,000 passwords have been reset on July 8, 2019. If you cannot login, read this.

(C++) Problem

Discussion in 'Programming' started by GywGod133, Sep 19, 2014.

  1. GywGod133

    GywGod133

    Joined:
    Jul 16, 2012
    Messages:
    630
    Resources:
    3
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    Hello Hivers :)

    I have a problem...

    Look this....

    Code (vJASS):


    #include<conio.h>
    #include<iostream.h>

    main()
    {
        cout<<"Enter your number: "; /* Only 1, 2, 3 */
        cin>>o;

        int counter = 1, o;

        if( o == 0){
          counter = 0;
        }else if( o == 1){
          q1++;
          t = t + c1;
        }else if( o == 2){
          q2++;
          t = t + c2;
        }else if( o == 3){
          q3++;
          t = t + c3;
        }else if( o != 1 && o != 2 && o != 3){
          cout<<"Invalid: "<<o;
        }else
          cout<<"Invalid: "<<o; /*Non-numbers */
        getch();
        return 0;
    }
     


    So...
    I want to get the 1, 2, 3
    If the cin get letters or symbols

    must be show Invalid but does not work :(
     
  2. GhostWolf

    GhostWolf

    Joined:
    Jul 29, 2007
    Messages:
    4,952
    Resources:
    2
    Tools:
    1
    Tutorials:
    1
    Resources:
    2
    Please. Learn. English.

    You are missing
    using std;
    .
    Your
    main
    doesn't return int in the declaration.
    You are reading into a variable called
    o
    before you declare it.
    You are using variables called
    q1
    ,
    q2
    ,
    q3
    ,
    c1
    ,
    c2
    ,
    c3
    and
    t
    , without ever declaring any of them.
    You are assuming number characters match the actual numbers, which is obviously not right. If you want to compare a number against the ASCII identifier of that number, use id literals. For example,
    o == '1'
    .
    Your 5th condition isn't necessary. If you got to it, you already know that
    o
    isn't 1, 2, or 3.
    Your last condition will never be reached, it's pointless.
     
  3. edo494

    edo494

    Joined:
    Apr 16, 2012
    Messages:
    3,846
    Resources:
    5
    Spells:
    1
    JASS:
    4
    Resources:
    5
  4. Dr Super Good

    Dr Super Good

    Spell Reviewer

    Joined:
    Jan 18, 2005
    Messages:
    26,104
    Resources:
    3
    Maps:
    1
    Spells:
    2
    Resources:
    3
    You should probably be using a switch instead of conditional selection. They generally compile better (better than O(n) decision making) at the restriction that only 1 variable may be tested.
     
  5. GywGod133

    GywGod133

    Joined:
    Jul 16, 2012
    Messages:
    630
    Resources:
    3
    Maps:
    1
    Spells:
    1
    Tutorials:
    1
    Resources:
    3
    @GhostWolf
    ? Im sorry for my bad english -_-

    btw, this is only a demo.

    yep, if I use char just like this
    Code (vJASS):

    if( o == '1'){
       bla bla
    }else if( o == '2'){
       bla bla
       }
     

    but how if o is 100?
    Thats why I need help...

    @edo
    what is a problem in
    Code (vJASS):
    #include<iostream.h>
    ?

    ??

    @DSG
    switch is better...

    Thanks...
     
  6. GhostWolf

    GhostWolf

    Joined:
    Jul 29, 2007
    Messages:
    4,952
    Resources:
    2
    Tools:
    1
    Tutorials:
    1
    Resources:
    2
    What's a "demo", your code shouldn't even compile.

    If a char variable is equal to 100, it's equal to the ASCII ID 'd'.
    If your purpose is not to read a character, but rather a number, then read a number and not a character.
    Code (C++):
    int number;
    cin >> number;

    This will automatically convert whatever input the user gives into a number, assuming the input is indeed a number.

    If you are using functions from the std namespace, you either need to prepend them with std:: to scope them, or you need to add using namespace.
    Code (C++):
    // No using, have to scope them explicitly
    std::cout << ...;
    std::cin >> ...;

    // Using std, the scopes are implicit
    using namespace std;
    cout << ...;
    cin >> ...;

    // Using only cout, so cout can be implicit, but cin must be explicit
    using std::cout;
    cout << ...;
    std::cin >> ...;
     

    The same rules apply to any namespace, obviously.

    As to <iostream>, edo494 linked to a page that explains why it should be used...
     
    Last edited: Sep 20, 2014
  7. edo494

    edo494

    Joined:
    Apr 16, 2012
    Messages:
    3,846
    Resources:
    5
    Spells:
    1
    JASS:
    4
    Resources:
    5
    you sure about that? :D
     
  8. GhostWolf

    GhostWolf

    Joined:
    Jul 29, 2007
    Messages:
    4,952
    Resources:
    2
    Tools:
    1
    Tutorials:
    1
    Resources:
    2
    Erm, edo*