LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-23-2004, 04:37 AM   #1
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 437

Rep: Reputation: 30
More arguments in printf()


Hey everyone

I'm redesigning the random number guessing game and want to haev a statement that goes on the amount of guesses it took... This is the code I have so far:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <ctype.h>
                                                                                                 
int main(int argc, char *argv[]) {
  int max_guess = 100, min_guess = 0, rand_num = 0;
  int guess = 0, num_of_guesses = 1;
  char temp[5];
                                                                                                 
  srand(time(NULL));
                                                                                                 
  if(argc > 1) {                /* Test for arguments */
    printf("%s: This program uses no arguments.\n", argv[0]);
    return 1;
  }
                                                                                                 
  rand_num = 1 + rand() % 100;
                                                                                                 
  do {
    printf("Please enter your guess: ");
    fgets(temp, sizeof(temp), stdin);
    sscanf(temp, "%d", &guess);
                                                                                                 
    if(guess < rand_num) {
       if(guess <= min_guess && min_guess != 0) {
         printf(":: Ignored: Your lowest is already %d ::\n", min_guess);
       } else {
         min_guess = guess;
         ++num_of_guesses;
         guess = 0;
       }
    } else if(guess > rand_num) {
      if(guess >= max_guess && max_guess != 0)
        printf(":: Ignored: Your highest is already %d ::\n", max_guess);
      else
        max_guess = guess;
        ++num_of_guesses;
        guess =0;
    }
 
    printf("Min: %d\tMax: %d\n", min_guess, max_guess);
  } while(guess != rand_num);
 
  printf("\n%s You got it in %d guess(s)\n", 
              num_of_guesses <= 3 ? "Great work." : "At least you finished.",
              num_of_guesses);
 
return 0;
}
What I wanna do is expand the amount of sayings: i.e. if the player guessed between 1 and 3 then have 1 saying, between 4 and 10 have another and another for over 10....

Is this possible?

Last edited by AMMullan; 02-23-2004 at 04:38 AM.
 
Old 02-23-2004, 06:08 AM   #2
chewysplace
Member
 
Registered: Sep 2003
Distribution: Slackware 10 w/ Kernel 2.6.8
Posts: 176

Rep: Reputation: 30
if(rand_num == (1 || 2 || 3))
{printf("BLAH!");}

and so on.
 
Old 02-23-2004, 12:38 PM   #3
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 437

Original Poster
Rep: Reputation: 30
Yeah I knew I could do that but wanted to know if it could be done the way I had it...

Thanks chewy
 
Old 02-23-2004, 03:29 PM   #4
chewysplace
Member
 
Registered: Sep 2003
Distribution: Slackware 10 w/ Kernel 2.6.8
Posts: 176

Rep: Reputation: 30
you can, just inside your current if statements:

if(guess<rand_num)
{
...
int insult = rnd(10);
talk_back(insult);
}

static void talk_back(int inslt)
{
switch(inslt)
{
case 1:
printf("MOTHER OF DOG YOUR STUPID!");
break;
case 2:
....
default:
printf("The coder is dumb for not haveing enough cases.");
break;
}
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
printf problem??? Mistro116@yahoo.com Programming 2 11-13-2005 09:45 PM
is a printf wrapper possible? Thinking Programming 2 10-21-2005 08:44 AM
Using 'printf' on a 'struct' skywalker27182 Programming 10 12-20-2003 01:06 PM
printf new_user10 Programming 3 09-10-2003 12:12 AM
bash printf anyone? gumby Programming 4 05-13-2003 12:51 PM


All times are GMT -5. The time now is 09:43 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration