I am working on one of my excercises in a terrible book. C by disection by al kelly / ira pohl. For a beginning c programing book it is poorly illustrated with examples. The excercises dont match the stuff taught in the chapters. I know you dont want to here complaining.
To the point. This example wants me to take the example program in the book and add a CLS or clear command at the beginning of the program. ONLY THING IS THERE IS NOTHING ON THE CLEAR COMMAND IN THE BOOK.
can someone give me the command and suggest where to drop it in the program.
Here is the program:
main.c
Code:
#include "heads_or_tails.h"
int main(void)
{
char ans;
int no_of_plays;
cls
printf("\n"
"THE GAME OF HEADS OR TAILS\n"
"\n"
"Do you want instructions? ");
scanf(" %c", &ans);
putchar('\n');
if (ans == 'y' || ans == 'Y')
prn_instructions();
printf("How many times do you want to play? ");
scanf("%d", & no_of_plays);
putchar('\n');
play( no_of_plays);
return 0;
}
in get.c
Code:
#include "heads_or_tails.h"
int get_call_from_user(void)
{
int guess; /* 0 = heads, 1 = tails */
do {
printf("Call it: ");
if (scanf("%d", &guess) != 1) {
printf("\nSORRY: Severe input error - bye!\n\n");
exit(1);
}
if (guess != 0 && guess != 1) {
printf("\n%s\n\n",
"ERROR: Type 0 for heads, 1 for tails.");
}
} while (guess != 0 && guess != 1);
return guess;
}
in heads_or_tails.h
Code:
#include <stdio.h>
#include <stdlib.h>
#define MAXWORD 100
int get_call_from_user(void);
void play(int how_many);
void prn_final_report(int win, int lose, int how_many);
void prn_instructions(void);
void report_a_win(int coin);
void report_a_loss(int coin);
int toss(void);
in play.c
Code:
#include "heads_or_tails.h"
void play(int how_many) /* machine tosses, user calls */
{
int coin, i, lose = 0, win = 0;
for (i = 0; i < how_many; ++i) {
coin = toss();
if (get_call_from_user() == coin) {
++win;
report_a_win(coin);
}
else {
++lose;
report_a_loss(coin);
}
}
prn_final_report(win, lose, how_many);
}
int toss(void)
{
return (rand() % 2); /* 0 = heads, 1 = tails */
}
in prn1.c
Code:
#include "heads_or_tails.h"
void prn_instructions(void)
{
printf("%s\n",
"This is the game of calling heads or tails.\n"
"I will flip a coin; you call it. If you\n"
"call it correctly, you win; otherwise,\n"
"I win.\n"
"\n"
"As I toss the (simulated) coin, I will\n"
"tell you to \"call it.\" To call heads,\n"
"type 0; to call tails, type 1.\n"
"\n");
}
void prn_final_report(int win, int lose, int how_many)
{
printf("\n%s\n%s%3d\n%s%3d\n%s%3d\n\n",
"FINAL REPORT:",
" Number of games that you won: ", win,
" Number of games that you lost: ", lose,
" Total number of games: ", how_many);
}