| | |
Guesing game
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2008
Posts: 13
Reputation:
Solved Threads: 0
hey guys I have a question on this guessing game. I want to make this game to repeat. when the guess is correct, i wanted the user to input 'y' or 'n' and if yes it should repeat. How can i do that? Do I have to put the whole program in to another while loop?
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { int g; int x; char a; srand (time(NULL)); g = rand() % 1000+1; printf("I have a number between 1 and 1000\nCan you guess my number\?\nPlease type your first guess\n"); scanf("%d", &x); while (g!=x){ if (g<x) printf("Too high. Try again\n"); else if (g>x) printf("your guess is less\n"); scanf("%d", &x); } printf("Excellent!you guess the number\nWould you like to play again (y or n)?\n"); return 0; }
What I would suggest is that you make a function of everything starting from "int g;" to the end of the while loop. Then in main() call the function inside another loop as
c Syntax (Toggle Plain Text)
do { function_here(); printf("Excellent!you guess the number\nWould you like to play again (y or n)?\n"); scanf("%c",&a); }while(a=='y'||a=='Y');
Last edited by hammerhead; Mar 10th, 2008 at 3:53 am.
There are 10 types of people in the world, those who understand binary and those who don't.
All generalizations are wrong. Even this one.
All generalizations are wrong. Even this one.
![]() |
Other Threads in the C Forum
- Previous Thread: trouble with linked list
- Next Thread: C Progamming
Views: 821 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C
#include * append array arrays asterisks bash binarysearch calculate changingto char character cm command copyimagefile creafecopyofanytypeoffileinc createprocess() database directory dynamic execv feet fgets file floatingpointvalidation fork forloop framework function functions getlogicaldrivestrin givemetehcodez global grade graphics gtkwinlinux hacking histogram homework ide include incrementoperators input intmain() iso kernel keyboard kilometer lazy license linked linkedlist linux list lists looping loopinsideloop. lowest matrix meter microsoft mqqueue number oddnumber odf openwebfoundation overwrite pause pdf pointer posix process program programming radix recursion recv recvblocked research reversing segmentationfault sequential single socket socketprogramming spoonfeeding standard strchr string student system testing threads turboc unix urboc user variable whythiscodecausesegmentationfault windowsapi






