it's all good

This is a discussion on it's all good within the C++ Programming forums, part of the General Programming Boards category; what is the best way to create exit a program? For example: This program uses a while loop and then ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    11

    Cool it's all good

    what is the best way to create exit a program? For example:
    This program uses a while loop and then checks the inputed value before it enters it again. Is there a shorter better way to do this? Should I use a character or a number? This comes up a lot in my programs and I want to use the fastest, best method.

    Code:
    //grades
    #include <iostream.h>
    
    int main()
    {
    	int count = 0;
    	int total = 0;
    	int average;
    	int grades = 0;
    	cout << "Please enter a grade(enter -1 to quit): \n";
    	cin >> grades;
    	while(grades != -1)
    	{total += grades;
    	count++;
    	cout << "Please enter a grade(enter -1 to quit): \n";
    	cin >> grades;}
    	average = total / count;
    	cout << "Class average is: " << average << endl;
    	return 0;
    }
    Last edited by runtojesus; 11-06-2001 at 01:58 PM.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Huh?

    Huh?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User bljonk's Avatar
    Join Date
    Oct 2001
    Posts
    70

    You've got it Or~

    You've done it, that i can see but you only wnt to make you source code shorter use the 'exit()'function:

    as:
    if(grade == -1){
    exit(o);
    }

    the prototype is:

    #include <stdlib.h>
    void exit(int status)
    Ünicode¬>world = 10.0£

  4. #4
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    If you want shorter code then you could use a do..while loop, but it won't make much difference. It would save a bit of typing but make your initialisation a bit obscure -

    Code:
    #include <iostream.h>
    
    int main()
    {
    	int count = -1;
    	int total = 1;
    	int average;
    	int grades = 0;
    	do
    	{
    	cout << "Please enter a grade(enter -1 to quit): \n";
    	cin >> grades;
    	total += grades;
    	count++;
    	}while(grades != -1);
    
    	average = total / count;
    	cout << "Class average is: " << average << endl;
    	
    	return 0;
    }
    zen

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    That's strange...

    When I replied, it was a weird post. It IS modified, you see?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In a game Engine...
    By Shamino in forum Game Programming
    Replies: 28
    Last Post: 02-19-2006, 10:30 AM
  2. Good books for learning WIN32 API
    By Junior89 in forum Windows Programming
    Replies: 6
    Last Post: 01-05-2006, 04:38 PM
  3. Good resources for maths and electronics
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 12-22-2004, 03:23 PM
  4. what is good for gaphics in dos
    By datainjector in forum Game Programming
    Replies: 2
    Last Post: 07-15-2002, 03:48 PM
  5. i need links to good windows tuts...
    By Jackmar in forum Windows Programming
    Replies: 3
    Last Post: 05-18-2002, 11:16 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21