SilabGarza - RS Private Server  
Home Forum Play Register Store Market Highscores Mark Forums Read
Go Back   SilabGarza - RS Private Server > SilGar Media > Computers > Programming
Reload this Page code help


code help
Old
  (#1 (permalink))
D Kai V2 is Offline
Senior Member
image
 
D Kai V2's Avatar
 
Posts: 299
Cookies Given: 39
Cookies Received: 58
Time Online: 14 d 12 h 59 m
   
Default code help - 10-21-2013, 05:27 PM

trying to make a code in C that guess what number u r thinking
it starts with a 5 and if he number is higher u input a +, if its lower u di a -, if this igual u do a I
but for some reason it has multiple problems. halp

edit: the number should go between 10 and 1

Code:
#include <stdio.h>
#include <time.h>

main (){
	int x,y;
	char add;
	
	printf("5");
	scanf("%c\n",&add);
	while(add != 'I'){
	  if(add == '+'){
		  x=0;
		  srand(time(NULL));
		  x = (rand()%10)+y;
		  printf("%i\n",x);
		  y=x+1;
		  scanf("%c\n",&add);
	  }
	  if(add == '-'){
		  x=0;
		  srand(time(NULL));
		  x=(rand()%y)+1;
		  printf("%i\n",x);
		  y=x-1;
		  scanf("%c\n",&add);
      }	
    }
    printf(":D"); 
}


<3 Ervin is #1. <3


"New/updated content and small fixes do a lot for the server, even though they may seem small."
-Jokerjake72



Last edited by D Kai V2; 10-21-2013 at 05:36 PM.
  
Old
  (#2 (permalink))
Nater is Offline
Senior Member
image
 
Nater's Avatar
 
Posts: 376
Cookies Given: 17
Cookies Received: 107
Time Online: 14 d 18 h 2 m
Clan: White Knights
   
Default 10-21-2013, 06:31 PM

I don't play with C at all so ignore me if I give bad suggestions...

Make a method for guessing

bool for high or low
int for last guessed

so like

guess(bool high, int guess){

(code for random)

if high guess + random

if low guess - random

}


  
Old
  (#3 (permalink))
N0vlafye is Offline
Senior Member
image
 
N0vlafye's Avatar
 
Posts: 2,442
Cookies Given: 235
Cookies Received: 451
Time Online: 108 d 1 h 31 m
Clan: Imperium, WIN
   
Default 10-21-2013, 06:40 PM

Quote:
Originally Posted by WaterColor View Post
I don't play with C at all so ignore me if I give bad suggestions...

Make a method for guessing

bool for high or low
int for last guessed

so like

guess(bool high, int guess){

(code for random)

if high guess + random

if low guess - random

}
Probably should have just said that. Did out pseudocde because I can't read C either ;_;

Psuedocode:

Code:
int min=1,max10, guess = 5;//your upper and lower limits and your current guess
boolean correct=false;//whether the number is correct
char highlow;
Print "The current guess is: " + guess; //tells the user the current guess
while (!correct){
	do{
 		highlow = GetInput(If your number is higher, enter '+'.  If it is lower, enter '-'.  If the guess is correct, enter 'C') //gets whether the number is higher or lower
 	}while(highlow != '+'&&highlow!='-'&&highlow!='C')//keeps asking the user until he enters plus, minus, or C.
	if (highlow == '+'){
   	 min = guess+1; // the new minimum is the guess + 1, since the number is greater than the guess
	 guess = (min+max);//gets the new guess, the average of max and min 
	}
	else if (highlow == '-'){
		max = guess-1;//the number is less than the guess, so the new max is less than the guess
		guess = (min+max)/2;//gets the new guess, the average of the two numbers		
        }
	else if (highlow == 'C'){
		correct = true;//sets the boolean to true to stop the loop
		break; //breaks out of the loops immediately
        }
	if (max<min||min<0||max>10){
             Print (you failed.) //impossible conditions
 	}
	Print("the current guess is " + guess);
}
  
Old
  (#4 (permalink))
Nater is Offline
Senior Member
image
 
Nater's Avatar
 
Posts: 376
Cookies Given: 17
Cookies Received: 107
Time Online: 14 d 18 h 2 m
Clan: White Knights
   
Default 10-21-2013, 06:48 PM

Quote:
Originally Posted by N0vlafye View Post


Probably should have just said that. Did out pseudocde because I can't read C either ;_;

Psuedocode:

Code:
int min=1,max10, guess = 5;//your upper and lower limits and your current guess
boolean correct=false;//whether the number is correct
char highlow;
Print "The current guess is: " + guess; //tells the user the current guess
while (!correct){
	do{
 		highlow = GetInput(If your number is higher, enter '+'.  If it is lower, enter '-'.  If the guess is correct, enter 'C') //gets whether the number is higher or lower
 	}while(highlow != '+'&&highlow!='-'&&highlow!='C')//keeps asking the user until he enters plus, minus, or C.
	if (highlow == '+'){
   	 min = guess+1; // the new minimum is the guess + 1, since the number is greater than the guess
	 guess = (min+max);//gets the new guess, the average of max and min 
	}
	else if (highlow == '-'){
		max = guess-1;//the number is less than the guess, so the new max is less than the guess
		guess = (min+max)/2;//gets the new guess, the average of the two numbers		
        }
	else if (highlow == 'C'){
		correct = true;//sets the boolean to true to stop the loop
		break; //breaks out of the loops immediately
        }
	if (max<min||min<0||max>10){
             Print (you failed.) //impossible conditions
 	}
	Print("the current guess is " + guess);
}
if you're going to put descriptions use pastebin much easier to read


  
Old
  (#5 (permalink))
D Kai V2 is Offline
Senior Member
image
 
D Kai V2's Avatar
 
Posts: 299
Cookies Given: 39
Cookies Received: 58
Time Online: 14 d 12 h 59 m
   
Default 10-21-2013, 06:52 PM

I already did that way yesterday, i wanted this method to work but it has some problems, anyways thanks. lov ya


<3 Ervin is #1. <3


"New/updated content and small fixes do a lot for the server, even though they may seem small."
-Jokerjake72


  
Old
  (#6 (permalink))
Nater is Offline
Senior Member
image
 
Nater's Avatar
 
Posts: 376
Cookies Given: 17
Cookies Received: 107
Time Online: 14 d 18 h 2 m
Clan: White Knights
   
Default 10-21-2013, 06:54 PM

Having everything done in your main won't play nice.


  
Old
  (#7 (permalink))
D Kai V2 is Offline
Senior Member
image
 
D Kai V2's Avatar
 
Posts: 299
Cookies Given: 39
Cookies Received: 58
Time Online: 14 d 12 h 59 m
   
Default 10-21-2013, 07:17 PM

Quote:
Originally Posted by WaterColor View Post
Having everything done in your main won't play nice.
ye i know
thats why i didn't finish it :c
can't blame a man for trying doe


<3 Ervin is #1. <3


"New/updated content and small fixes do a lot for the server, even though they may seem small."
-Jokerjake72


  
Old
  (#8 (permalink))
Nater is Offline
Senior Member
image
 
Nater's Avatar
 
Posts: 376
Cookies Given: 17
Cookies Received: 107
Time Online: 14 d 18 h 2 m
Clan: White Knights
   
Default 10-21-2013, 07:18 PM

Quote:
Originally Posted by D Kai V2 View Post
ye i know
thats why i didn't finish it :c
can't blame a man for trying doe
I have a free bell tomorrow if you'd like me to make this in c# and you can translate it.. They're nearly the same..



Last edited by Nater; 10-21-2013 at 07:22 PM.
  
adsense code2.3
Closed Thread

Bookmarks

code help

Programming




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
adsense2.6
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 11:59 PM.





Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2017, vBulletin Solutions, Inc.