Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

fun fun C++

Name: Anonymous 2009-09-04 20:46

#include <stdlib.h>
#include <iostream>

using namespace std;

int main() { int number=rand()%100;
int guess=-1;
int trycount=0;
while(guess!=number && trycount<8) {
    cout<<"Please enter a guess: ";
    cin>>guess;
    if(guess<number) cout<<"Too low"<<endl;
    if(guess>number) cout<<"Too high"<<endl;
    trycount++;
    }
if(guess==number) cout<<"You guessed the number";
else cout<<"Sorry, the number was: "<<number;
return 0;
}

Name: Anonymous 2009-09-05 1:15

SUPERIOR


module Main where
import IO
import Monad
import Random

maxTries :: Int
maxTries = 8

loop :: Int -> Int -> IO Bool
loop number try = do
    if try < maxTries
        then do putStr "Enter a guess: "
                guess <- getLine >>= (return . read)
                when (guess < number) (putStrLn "Too low.")
                when (guess > number) (putStrLn "Too high.")
                if guess == number
                    then return True
                    else loop number (try + 1)
        else return False

main :: IO ()
main = do
    hSetBuffering stdout NoBuffering
    number <- randomRIO (1, 100)
    won <- loop number 0
    if won
        then putStrLn "You win!"
        else putStrLn "Sorry, please play again."

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List