How does this C++ program work internally?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: How does this C++ program work internally?

  1. #1
    Join Date
    Dec 2012
    Posts
    1

    How does this C++ program work internally?

    Hi, im new here. I was trying to teach myself C++ before I have to take it in the spring. I came across this example, and I don't understand how it works

    d_random.h is a random number generation program
    this first part is called, header.h


    #include <iostream>
    #include "d_random.h"

    using namespace std;

    class playHiLow
    {
    public:
    playHiLow()
    {
    randomNumber r;
    cout<<" Try to guess the right number between 1-1000"<<endl;;
    do {
    target = r.random();
    }
    while(target > 1000);
    count = 1;
    }
    int makeGuess() {
    if(count > 10) {
    cout<<" Sorry, you did not find the number ";
    cout << " The correct number is: " <<target<<endl;
    exit(1);
    }
    count++;
    cout<<" make a guess: ";
    int x;
    cin>>x;

    if(x == target)
    return 0;
    else

    if(x < target)
    return -1;
    else
    return 1;
    }

    void writeTarget() {
    cout<<"The number is "<<target;
    }
    int count;
    private:
    long target;
    };

    ______________________________________
    second part


    #include <iostream>
    #include "header.h"

    using namespace std;



    void main()
    {
    playHiLow game;
    while(1)
    {
    if (game.count <= 10)
    cout<<" Enter your guess # "<<game.count;

    int x = game.makeGuess();

    if(x == 0)

    {
    cout<<" Correct! ";

    return;
    }
    if(x == -1)

    {

    cout<<"Yout guess is too low..."<<endl ;

    if(game.count<10)
    cout<<"Try again." ;
    }

    if(x == 1)

    {
    cout<<"Yout guess is too high..." <<endl;

    if(game.count<10)
    cout<<"Try again.";

    }
    }
    }



    /*

    Try to guess the right number between 1-1000
    Enter your guess # 1 make a guess: 510
    Yout guess is too high...
    Try again. Enter your guess # 2 make a guess: 210
    Yout guess is too low...
    Try again. Enter your guess # 3 make a guess: 225
    Yout guess is too low...
    Try again. Enter your guess # 4 make a guess: 380
    Yout guess is too high...
    Try again. Enter your guess # 5 make a guess: 376
    Correct! Press any key to continue . . .

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,013

    Re: How does this C++ program work internally?

    You have to learn debugging... Then debug this program step-by step.
    Victor Nijegorodov

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center