• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Bear Bibeault
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • salvin francis
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Jj Roberts

a problem with programing a game in c language

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
#include <stdio.h>
#include <stdlib.h>
#include<math.h>
#include<time.h>

int main()
{

   int a =0,MAX=0, MIN=1 ;
   int mynumber=0 ,num=0 ,num1=0 ,num2=0;
   int person=0;
   char username1[30],username2[30];

   printf("*********** welcom to the game table ************\n\n");

   printf("choose a path..\n\n");
   printf(" 1.one person\n\n 2.tow person\n\n");
   printf("enter you choice:");
   scanf("%d",&person);
   printf("\n\n ******** Names of the players ********\n");
   switch(person)
   {
     case 1:
       person=1;
       printf("enter your name :\n\n");
       scanf("%s",&username1);
       printf("welcome %s\n\n",username1);
       break;
     case 2:
       person=2;
       printf(" enter you names :\n\n");
       scanf("\t %s\n",&username1);
       scanf("\t \n %s",&username2);
       printf("\n\n\twelcome %s *************** welcome %s\n\n",username1,username2);
       break;
     default:
       printf("sorry we can fond you name please try agin..\n\n");
       break;

   }
   clock_t start,finish;
   double duration;
   start=clock();

   if(person=1){

        printf("you want to play betwen:\n\n");
        printf("  +[1,100]\n  +[1,1000]\n  +[1,10000]\n\n");
        printf("enter your Maximun number please:\n\n");
        scanf("%d",&MAX);


     srand(time(NULL));
     mynumber= (rand()%MAX) + MIN ;


     do

      {

// We request the number
          printf("enter your number: \n\n");
          scanf("%d",&num);
// We compare between inputNumber and mysteriousNumber
          if (num > mynumber ){
             printf("Greater \n\n");
          }

         else if (num < mynumber )
           printf("leaser \n\n");
         else{
           printf ("Bravo, you have found the mysterious number !!!\n\n");
         }
    }
   while (num != mynumber);

   }
else if(person=2)
   {
    printf("you want to play bitwen:\n\n");
    printf(" +[1,100]\n +[1,1000]\n +[1,10000]\n\n");
    printf("enter your Maximun number please:\n");
    scanf("%d",&MAX);

    srand(time(NULL));
    mynumber= (rand()%MAX) + MIN ;


   do
  {
// We request the number
    printf("%s enter your number 1: \n\n",username1);
    scanf("%d",&num1);
    printf("%s enter your number 2 : \n\n",username2);
    scanf("%d",&num2);
// We compare between inputNumber and mysteriousNumber
    if ((num1 && num2) > mynumber )
       printf("%s , %s Greater \n\n",username1,username2);
    else if ((num1 && num2) < mynumber )
       printf("Lesser !\n\n");
    else if((num1 > mynumber)&&(num2 < mynumber))
       printf("%s greater ************** %s lesser\n\n",username1,username2);
    else if ((num1 < mynumber)&&(num2 > mynumber))
       printf("%s lesser ********** %s greater\n\n",username1,username2);
    else
       printf ("Bravo,%s ,%s you have found the mysterious number !!!\n\n",username1,username2);
    }
    while ((num1 && num2) != mynumber);

   }


   finish=clock();
   duration = (double)(finish - start)/ CLOCKS_PER_SEC;
   printf("the time that was teken is : %f\n\n",duration);

   printf ("****** do you want to play again? ********\n\n");
   int play=0;
      printf(" 1.yes\n\n 2.no \n\n enter your repanse her:..\n\n");
      scanf("%d",&play);

      switch(play){

  case 1:
   play=1;
   return main();
   break;
  default:
      printf("\n\nthank you ...bay\n\n");
   return 0;
   break;

      }


 
Sheriff
Posts: 15996
265
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

1. When posting code, please UseCodeTags (←that's a link to instructions on how to do that)
2. What's your question?
 
Rancher
Posts: 259
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Latifa - what's the problem?

Also, please can you format your code as code rather than leaving it as ordinary text. Just select all the code and press the 'Code' button - makes it easier for us to read/understand and help you with your problem(s).

Edit: sorry Junilu, you beat me to it
 
John Matthews
Rancher
Posts: 259
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One problem:
That should be the test for equality, not assignment:
Similarly for the check for 2 people.
 
Latifa Erradi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello
Thank you for your interest.
My problem is, it's in the game when the game is chosen for two people, the program doesn't work as it should.


 
John Matthews
Rancher
Posts: 259
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also:
Calling main() is unusual at best. I haven't looked at the code in enough detail to work out whether it will actually cause a 'problem', but I would highly recommend not doing that and using an outer loop which you exit if you don't want to play the game again.
 
John Matthews
Rancher
Posts: 259
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Latifa Erradi wrote:My problem is, it's in the game when the game is chosen for two people, the program doesn't work as it should.

I suspect that's the = which should be ==; try correcting those first.
 
Latifa Erradi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it is.
Thank you so much 🌹🌹
 
Latifa Erradi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there's something wrong with this section:


    if ((num1 && num2) > mynumber )
        printf("%s , %s Greater \n\n",username1,username2);
     else if ((num1 && num2) < mynumber )
        printf("Lesser !\n\n");
     else if((num1 > mynumber)&&(num2 < mynumber))
        printf("%s greater ************** %s lesser\n\n",username1,username2);
     else if ((num1 < mynumber)&&(num2 > mynumber))
        printf("%s lesser ********** %s greater\n\n",username1,username2);
     else
        printf ("Bravo,%s ,%s you have found the mysterious number !!!\n\n",username1,username2);
     }
     while ((num1 && num2) != mynumber);

 
John Matthews
Rancher
Posts: 259
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Latifa Erradi wrote:I think there's something wrong with this section:

Yes - it's not formatted as code like we asked:
Also:
You can't combine 2 tests like that; you need to do:
 
John Matthews
Rancher
Posts: 259
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
More detail:
Is equivalent to:
The result of logical expression:
is a logical Boolean, which in C has numerical value of 0 (false) or 1 (true). So the code is doing:
where b has value 0 or 1.
 
Latifa Erradi
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your help 🌹🌹
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic