need help

Discussion in 'Programming' started by ikusung, Mar 12, 2010.

  1. ikusung

    ikusung New Member

    Joined:
    Mar 12, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    #include <iostream>
    #include <time.h>
    using namespace std;
    void main() {
    srand(time(0));
    while (true) {
    int brian;
    int happy;
    int r;
    r = rand () %101;
    cout <<rand () %101 << "\n";
    cout << "enter number \n" ;
    cin >> brian;
    if (brian == r) {
    cout << "Go you, you win\n";
    }
    else if (r<brian){
    cout << "Lower\n";
    }
    else {
    cout << "Higher\n";
    }
    }
    }
    Could anyone point out what's wrong with my code .
    It's meant to be a higher or lower game but it keeps generating a new number everytime?
     
    Last edited by a moderator: Mar 12, 2010
  2. virxen

    virxen Active Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    28
    check the changes and ask what you do not understand.

    Code:
    #include <iostream>
    #include <time.h>
    using namespace std;
    
    [COLOR=Red]int[/COLOR] main() {
    [COLOR=Red]int brian=-1;
    int happy;
    int r=0;[/COLOR]
    srand(time([COLOR=Red]NULL[/COLOR]));
    [COLOR=Red]r = rand () %101; //let it outside the loop in order to get 1 value only for each run of the program
    cout <<r << "\n";[/COLOR]
    while ([COLOR=Red]brian!=r[/COLOR]) {
    
    cout << "enter number:" ;
    cin >> brian;
    if (brian == r) {
        cout << "Go you, you win\n";
    }
    else if (r<brian){
        cout << "[COLOR=Red]number x is[/COLOR] Lower\n";
    }
    else {
        cout << "[COLOR=Red]number x is [/COLOR]Higher\n";
    }
    
    }
    getchar();getchar();
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice