Thread: Cant figure out how to end my program

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    17

    Cant figure out how to end my program

    I have this program and I am not really sure on how to get it to close at the end.

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int newTotal = 0;
    int input = 1;
    int maxTotal = 0;
    int week = 1;
    void compute()
    {
    newTotal = input + newTotal;
    }
    
    void main()
    {
    	int run;
    	printf ("Enter sales figures for the weeks.\nUse 0 to mark an end of a sales week and -1 to mark the end of all sales.\n");
    	for (run = 1; run!=0; )
    	{
    		scanf ("%d", &input);
    		if (input == 0)
    		{
    			printf("Week %d Sales: %d\n", week, newTotal);
    			if (newTotal > maxTotal)
    				maxTotal = newTotal;
    			week = week++;
    			newTotal = 0;
    		}
    		else if (input == -1)
    		{
    			printf ("Week %d Sales: %d\n", week, newTotal);
    			if (newTotal > maxTotal)
    			{
    				maxTotal = newTotal;
    				printf ("Max Week Sales: %d\n", maxTotal);
    					run = 0;
    			}
    			else
    				printf ("Max Week Sales: %d\n", maxTotal);
    			break;
    		}
    		else
    			compute();
    	}
    	printf ("\nPress any key to continue.\n");
    	getch();
    
    }
    Edit: The assignment forces me to use a for loop with a break. and we have to use void main()

    Any help? Thanks

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What exactly do you mean when you say you can't get it to close? Does the program do what you want it to, as far as calculations go? I'm not clear on what the question is.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    17
    yeah, it does everything i want it to. I just want to make it so at the end where i have them type any key to close it closes the window.


    Edit: woops, continue should be quit. Sorry
    Last edited by Summonerur; 10-11-2004 at 07:14 PM.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    17
    Hmm, must have had a bad runtime. It seems to be closing just fine when i run it now.

  5. #5
    Registered User
    Join Date
    Oct 2004
    Posts
    22

    At Times

    At times, compilers are very finicky about what can freeze a program until the user presses, say, Enter. A good alternative for you, which requires the use of the header file:

    Code:
    #include <stdlib.h>
    You could use this instead. What it reads on the screen depends on your compiler or your OS:

    Code:
    system("PAUSE");
    Just scratch off the getch function and place this. This works like a charm.

  6. #6
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Quote Originally Posted by BB18
    At times, compilers are very finicky about what can freeze a program until the user presses, say, Enter. A good alternative for you, which requires the use of the header file:

    Code:
    #include <stdlib.h>
    You could use this instead. What it reads on the screen depends on your compiler or your OS:

    Code:
    system("PAUSE");
    Just scratch off the getch function and place this. This works like a charm.
    system () is one of the least portable ansi functions... imo worse than getch... at least with getch if you put it onto a system without all that conio crap, it wont compile. with system ("pause"), it will.. funny to watch programs die when malicious programs called "PAUSE" so happen to be placed in opportune locations...

    Why do any of this at all? I'd be extremely annoying if a console program i used asked me to hit an extra key for no reason every time it finished... much like fflush (stdin) -- i've sent you data, why are you discarding it?
    hello, internet!

  7. #7
    Registered User
    Join Date
    Oct 2004
    Posts
    22

    Huh?

    Are there even more convenient ways of freezing the program until the user responds by entering a key? Isn't fflush(stdin) really bad usage of C code?

  8. #8
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Yup, that topic is covered extensively in the FAQ (honestly, until I read it, I've always used fflush(stdin) followed by a nice getch()....no more)

  9. #9
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Quote Originally Posted by BB18
    Are there even more convenient ways of freezing the program until the user responds by entering a key? Isn't fflush(stdin) really bad usage of C code?
    yes fflush(stdin) is bad for reasons other than the fact that i mentioned; that it doesn't typically make sense to "discard" user input.
    hello, internet!

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by BB18
    At times, compilers are very finicky about what can freeze a program until the user presses, say, Enter. A good alternative for you, which requires the use of the header file:

    Code:
    #include <stdlib.h>
    You could use this instead. What it reads on the screen depends on your compiler or your OS:

    Code:
    system("PAUSE");
    Just scratch off the getch function and place this. This works like a charm.
    I've never understood why people use getch. Why don't you save yourself some typing, and make your program portable by simply using getchar?

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't figure out why this program won't work
    By Northstar in forum C Programming
    Replies: 6
    Last Post: 11-26-2007, 11:31 AM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Another weird error
    By rwmarsh in forum Game Programming
    Replies: 4
    Last Post: 09-24-2006, 10:00 PM
  4. program not working...please look at this
    By JOlszewski in forum C Programming
    Replies: 3
    Last Post: 01-30-2006, 10:33 PM