Jump to content
iana

Tolong Bley...

Recommended Posts

kenapa looping ni tak jadi?tak faham la..

#include<iostream>

using std::cout;

using std::cin;

int func_play();

int func_guess();

int num_1, num_2;

void main(){

cout<<"I have a number between 1 and 1000 \n";

cout<<"Can you guess my number? \n";

cout<<"Your first guess:";

cin>>num_1;

// num_2 = 28;

func_guess();

}

int func_guess(){

num_2 = 28;

while(num_1>=1&&num_1<=1000){

if(num_1==num_2)

{

int choice;

cout<<"Excellent! You guess the number!\n";

cout<<"Would you like to play again?\n\n";

cout<<"1. Play again\n";

cout<<"2. Out of the game\n";

cout<<"Your choice is: ";

cin>>choice;

if(choice==1)

{

func_play();

}

if(choice==2)

{

cout<<"You're out of the game!\n";

}

}

else if(num_1<num_2)

{

cout<<"Too low. Try again\n";

func_play();

}

else

{

cout<<"Too high. Try again\n";

func_play();

}

}

return 0;

}

int func_play()

{

int num_1;

cout<<"Enter your next guess: ";

cin>>num_1;

func_guess();

return 0;

}

Share this post


Link to post
Share on other sites

http://www.hirc.org/submit/view.php?id=122

srry, sini dah kul 3 pagi. tapi aku ada buat code mcm tu. i'll take a look at u're code tomorrow (ngantuk sgt now). hope that helps.

edit: actually..

while(num_1>=1&&num_1<=1000)

try and change it to.

while (num_1 != num_2)

also u're not passing a value into func_guess();

Edited by da^hype

Share this post


Link to post
Share on other sites

oohh well made some tea. and fixed u're code.

#include <iostream>
using namespace std;

void func_play(void);
int func_guess();
int num_1, num_2;
int choice;

int func_guess(int num1)
{
	num_2 = 28;
	while(num1 != num_2)
	{
  if(num1<num_2)
  {
  	cout<<"Too low. Try again\n";
  	func_play();
  }
  else
  {
  	cout<<"Too high. Try again\n";
  	func_play();
  }
	}
	cout<<"Excellent! You guess the number!\n";
	cout<<"Would you like to play again?\n\n";
	cout<<"1. Play again\n";
	cout<<"2. Out of the game\n";

	cout<<"Your choice is: ";
	cin>>choice;

	if(choice==1)
  func_play();
	if(choice==2)
	{
  cout<<"You're out of the game!\n";
  return 0;
	}
	return 0;
}

void func_play(void)
{
	cout<<"Enter your next guess: ";
	cin>>num_1;
	func_guess(num_1);
}

int main()
{
	cout<<"I have a number between 1 and 1000 \n";
	cout<<"Can you guess my number? \n";

	cout<<"Your first guess:";
	cin>>num_1;

	func_guess(num_1);
	return 0;
}

u have problems with u're function not getting values. and logic error in

func_guess(). func_play() tu takyah return value, pakai void. everything else looks great.

read up on functions http://www.cplusplus.com/doc/tutorial/tut2-2.html

good luck biggrin.gif

Edited by da^hype

Share this post


Link to post
Share on other sites

coding untuk problem yang sama tapi aku tambah random number generator dan detect brapa kali user try untuk guess number tu. biggrin.gif

#include <iostream>
#include <ctime>

using namespace std;

void play(void);
void play_again(int, int);
void check_value(int , int);

int main(void)
{
    play();
    return 0;
}

void play()
{
    int num1, num2;

    srand((unsigned int) time(NULL)); // random seed
    num2 = rand() % 1000 + 1;        // nombor random [julat 1 -> 1000]

    cout << endl;
    cout << "I have a number between 1 and 1000 \n";
    cout << "Can you guess my number? \n";

    cout << "Your first guess: ";
    cin >> num1;

    check_value(num1, num2);
    play_again(num1, num2);
}

void play_again(int num1, int num2)
{
    int counter = 0;	// store brapa kali user try
    int choice = 1;

    while(num1 != num2)
    {
        cout << "Enter your next guess: ";
        cin >> num1;
        check_value(num1, num2);
        counter++;
    }

    cout << endl;
    cout << "Excellent! You guess the number in " << counter + 1 << " tries!\n";
    cout << "Would you like to play again?\n\n";
    cout << "1. Play again\n";
    cout << "2. Out of the game\n";

    cout << "Your choice is: ";
    cin >> choice;

    if(choice == 1)
        play();
    if(choice == 2)
    {
        cout << endl;
        cout << "You're out of the game!\n";
    }
}

void check_value(int num1, int num2)
{
    if(num1 < num2)
        cout << "Too low. Try again\n";
    if(num1 > num2)
        cout << "Too high. Try again\n";
}

sila beri tunjuk ajar... laugh.gif

Share this post


Link to post
Share on other sites

ni aku lak nk mintak tlg...

cam ner nak membangunkan aturcara penghimpun M68K m'gunakan c++?

tau tak..? tak pun kat mana aku leh dapatkan contoh dier??

http://www.monroeccc.edu/ckelly/EASy68K.htm dgn menggunakan http://www.google.com/search?hl=en&q=68000+Assembler

kesimpulannya: alaaahai

Edited by zeph

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...