Thread: placing a clear or cls command at beginning of this program help needed

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    51

    placing a clear or cls command at beginning of this program help needed

    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);
    }

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    here is exactly what it says:

    Use the function call system("cls") or system("clear") to accomplish this. What happens when the output is redirected? is the screen cleared?

    My guess is the screen will be blank for a microsecond and then the program would ask me the same questions it does now.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > What happens when the output is redirected?
    Exactly, what will happen?
    Nothing good probably.

    If you really want something, try
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    All standard utilities which are designed to process some input file, and generate some output file seldom bother with a clear screen to begin with.

    Even interactive simulations like the one you're doing, don't really need it.

    It's like outputting a 'beep' whever the user makes a mistake - it's good "day-1" programming fun, but it pretty soon gets old.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    Your post does not help me with the current problem i am working in the book.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    So I'm assuming you didn't read the FAQ then.

    Nor why you never bothered to state your OS/Compiler.

    Nor why you persist in using "a terrible book"
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    win xp, studio latest version and yes i have read the faq. Why do you waste your time posting on this subject if you are not going to help.

    I dont usally post something this long without wanting some help , direction etc... all i got so far is a few posts from you complianing.

    So I know the rules of this forum and i have no choice at this point because i am beyone fraustration.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Think you have to restate your problem. In the FAQ there are 6 version to clear the screen. I take that as a perfect answer to your original question.
    Kurt

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    No one sees what the big deal is though. If you really want to clear the screen the FAQ should have answered your question.

    If you run your program in the DOS environment--that is, opening DOS and cd to the exe file to run it, clearing the screen will have more of a noticable effect, because it wipes out all the junk that was written on the screen before you ran your program, and then your program should continue writing to the screen happily.

  9. #9
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    i am doing all of this in visio studio .net. That is the problem. I can do a clear command in dos. I dont see anything happen in the visio studio .net envirnoment.

    It is a big deal to me. Obviously i need the command to put in visio studiio .net.

    thanks for the info

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Why do you waste your time posting on this subject if you are not going to help.
    Well there's bugger all else to do in the absense of a real question.
    All I've seen is "help", "I've read the FAQ" and "it doesn't work" without any evidence of
    - what you tried
    - what results you got
    - what error message(s) you got.

    > My guess is the screen will be blank for a microsecond and then the program would ask me the same questions it does now
    Maybe you should try it then.

    > Your post does not help me with the current problem i am working in the book.
    Well since I don't have that book, and you barely bothered to tell us what it says apart from YELLING some quote from the book, what are you expecting?

    http://www.catb.org/~esr/faqs/smart-questions.html
    Read this and then come back with a real question people can answer.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    The question is this:

    in visio studio where i am compiling my program. I have placed command after command in the main.c part of the program above and have yet to see anything happening.

    I asked at the begining the same question as now. What is the command i should be using to see a result or is this a trick question i am working with.

    Maybe my assumption is correct, it happens so fast i never see it.

    I searched thru the posts and it seems others have had this same problem.

    Maybe time would be spared if someone looks at the program and gives the command for clearing screen.

    I am trying to maintain my professionalism but , man i read your fact and it has a section on HOW TO ANSWER A QUESTION and man you have yet to attempt to answer it.

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    system("cls"); is a function in stdlib.h ... include the header and call it if you simply HAVE to

  13. #13
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    This has got to be the strangest thread I've seeen in a while... I
    may be totally missing the point here (and i may not be the only
    one either) because the question is a bit ... unclear, but here's my
    analysis.

    From the system details specified, I guess that Mshock is running
    his executable by double clicking on the icon produced, or using
    the compiler's run command. When opening a console using this
    method, i don't know whether it is equivalent to opening a
    command prompt, typing in the programs name, hitting enter, and
    clearing the screen all in one swoop, and closing said command
    prompt the moment the program exits, but i speculate that this
    doesn't matter
    - your console box will contain no text untill your
    program writes something to it, or if it is waiting for input and you
    enter some. Therefore, I propose that calling system ("cls") at
    the start of the program has no plausible effect - nevertheless,
    the screen IS cleared by it.

    I verfied this by creating a small program that called system ("cls")
    500 times using a loop - the program took 15 seconds or so to
    execute - presumably each call was actually clearing the screen,
    despite the fact that there was nothing to actually clear - ever.
    I then wrote a function that did nothing - literally. This was then
    executed 500 times and finished its work in a fraction of a
    second. Conclusion - system ("cls") clears the screen even when
    there is nothing to clear.

    This would therefore imply, that a call to system ("cls") also clears
    the screen, regardless of whether the output of the program
    is directed to the screen or somewhere else (a file, for example).

    An lastly, to point out what may be a misconception: It should be
    clear from my experiment that a book that asks a question as
    pointless as this appears to be is really rubbish - UNLESS that
    same book assumes that you are executing your program from
    the command line, and by that i mean you compile using some
    compiler, but when running it, you physically open a command
    prompt (which will diplay the current path), use the cd command
    to move to the directory your program is in, and you type its name
    to execute. IN THAT CASE, there will undoubtedly be text in the
    console from your action, and a call to system ("cls") from your
    program will clear this, whether your programs output is directed
    to the console or not.

    Well that's my analysis - I may be interpretting the question in
    a similar manner to the rest of you, and hopefully Mshock now
    sees why his question is "strange" and unclear. Lastly, i say
    get a new book if the current one is causing so much upset.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  14. #14
    Registered User
    Join Date
    Apr 2006
    Posts
    1
    I know this is an old post but I have been dealing with the same book as MShock. It does truly suck. My college requires it.

    For those having the same problem. the following is submitted as a function that will verify if your clear screen works.

    Once you have determined what clear screen function works on your type machine. It varies from one machine to the next.

    For my system system ("cls"); works to clear the screen. It does suggest that in the paragraph for exercise 5.

    read the link Salem submitted. It gave me the info I need to figure it out.
    http://faq.cprogramming.com/cgi-bin...0&id=1043284385

    To check if your clear screen is working write a simple function at the beginning of main such as the following:

    int main(void)
    {
    char ans;
    int no_of_plays;
    int ch;

    printf ("Press [Enter] to continue");
    while ((ch = getchar()) != '\n' && ch != EOF);

    /* your system clear screen function */
    ........

    What you should notice to happen is:

    "Press Enter to continue" will appear on your screen when you execute the program. When you press ENTER the phrase "Press Enter to continue" will disappear. And your game Heads or Tails will begin.

    If you comment out /*system("cls");*/ "Press Enter to Continue" remains on the screen. after you press enter to continue. Hence, clear screen at work.

    good luck

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    51
    lets correspond by email. [email protected]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program won't run properly, help needed asap
    By jlmac2001 in forum C Programming
    Replies: 2
    Last Post: 11-16-2002, 09:52 AM
  2. Program crashes...help Needed!!
    By butterfly in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2002, 11:22 AM
  3. redirection program help needed??
    By Unregistered in forum Linux Programming
    Replies: 0
    Last Post: 04-17-2002, 05:50 AM
  4. Program Ideas Needed
    By pkananen in forum C++ Programming
    Replies: 9
    Last Post: 02-24-2002, 10:08 PM
  5. Bug favour needed re: C++ Program
    By Nicole in forum C++ Programming
    Replies: 2
    Last Post: 12-05-2001, 07:13 AM