How to terminate program- Question

Let me start off by saying that this forum has been extremely helpful since I have been taking an online C++ course, thank you all for that. Unlike some other posts that I have come across, I am not trying to get someone else to do my assignments for me, I just need some guidance.

My question is regarding the termination of a program. How can I terminate a program if I am reading a data type int for the user input, but the only method to end the program is to enter a char data type- 'Q', for example.

Here is my program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>

using namespace std;

//Function Prototypes

void displayPeriod(int);


int main ()
{
	//Local Variables
	int millionYears; 
	
	//Display inital message and instructions
	cout << "TIME PERIOD NAMES!" << endl;
	cout << "Please enter the number of years (in millions) to display the names of the time periods" << endl;
	cout << "Example:  '150' means:  ";
	cout << "150,000,000 years ago" << endl;
	cout << "When finished, enter 'Q' to exit the program" << endl << endl;

	//Begin Main Loop
	do 
	{
		cout << "Enter a time period range" << endl;
		cin >> millionYears; //Input is type int, how do I read a type char in order to terminate program?

		cout << "The time periods include:  " << endl;

		displayPeriod(millionYears);
		

	}while (millionYears != 0); 

	
	
	if (millionYears = 'Q' || 'q') //Not sure how to terminate the program if millionYears is type int, and 'Q' is type char
		return 1;
	
		cin.get();
		cin.get();
		return 0;
}
//Function Descriptions
void displayPeriod(int millionYears)
{
if (millionYears >= 23 && millionYears <= 64)
			cout << "Neogene" << endl;
		else if (millionYears >= 65 && millionYears <= 135)
			cout << "Neogene" << endl << "Paleogene" << endl;
		else if (millionYears >= 136 && millionYears <= 191)
			cout << "Neogene" << endl << "Paleogene" << endl << "Cretaceous" << endl;
		else if (millionYears >= 192 && millionYears <= 224)
			cout << "Neogene" << endl << "Paleogene" << endl << "Cretaceous" << endl << "Jurassic" << endl;
		else if (millionYears >= 225 && millionYears <= 279)
			cout << "Neogene" << endl << "Paleogene" << endl << "Cretaceous" << endl<< "Jurassic" << endl << "Triassic" << endl;
		else if (millionYears >= 280 && millionYears <= 344)
			cout << "Neogene" << endl << "Paleogene" << endl << "Cretaceous" << endl<< "Jurassic" << endl<< "Triassic" << endl << "Permian" << endl;
		else if (millionYears >= 345 && millionYears <= 394)
			cout << "Neogene" << endl << "Paleogene" << endl << "Cretaceous" << endl<< "Jurassic" << endl<< "Triassic" << endl<< "Permian" << endl << "Carboniferous" << endl;
		else if (millionYears >= 395 && millionYears <= 434)
			cout << "Neogene" << endl << "Paleogene" << endl << "Cretaceous" << endl<< "Jurassic" << endl<< "Triassic" << endl<< "Permian" << endl << "Carboniferous" << endl << "Devonian" << endl;
		else if (millionYears >= 435 && millionYears <= 499)
			cout << "Neogene" << endl << "Paleogene" << endl << "Cretaceous" << endl<< "Jurassic" << endl<< "Triassic" << endl<< "Permian" << endl << "Carboniferous" << endl << "Devonian" << endl << "Silurian" << endl;
		else if (millionYears >= 500 && millionYears <= 569)
			cout << "Neogene" << endl << "Paleogene" << endl << "Cretaceous" << endl<< "Jurassic" << endl<< "Triassic" << endl<< "Permian" << endl << "Carboniferous" << endl << "Devonian" << endl << "Silurian" << endl << "Ordovician" << endl;
		else if (millionYears >= 570 && millionYears <= 4499)
			cout << "Neogene" << endl << "Paleogene" << endl << "Cretaceous" << endl<< "Jurassic" << endl<< "Triassic" << endl<< "Permian" << endl << "Carboniferous" << endl << "Devonian" << endl << "Silurian" << endl << "Ordovician" << endl << "Cambrian" << endl;
		else if (millionYears >= 4500)
			cout << "Neogene" << endl << "Paleogene" << endl << "Cretaceous" << endl<< "Jurassic" << endl<< "Triassic" << endl<< "Permian" << endl << "Carboniferous" << endl << "Devonian" << endl << "Silurian" << endl << "Ordovician" << endl << "Cambrian" << endl << "Precambrian" << endl;
		else 
			cout << "Please enter a year range greater than 22 Million" << endl;
		
}


Thanks



closed account (3CXz8vqX)
var exit_test
exit_test = userinput
if exit_test == q
then return 0.

Theres the pseudocode for it ^_^

edit

cin >> millionYears

'should' terminate the program but your if statement needs to be in the while loop =D. (edit: Okay, didn't notice it was an int, but normally there's a "user continue test". Which is what I thought you had done on the first read through. Whitenite1 is quite right.

You 'could' to be more...correct. Terminate the loop by using a break statement...then allowing the program to run to return 0 on it's own.
Last edited on
Here's a small insight ;)

In your displayPeriod() function, instead of doing all those if's and else if's, you can just use all if's.

1
2
3
4
5
6
if (millionYears >= 23 && millionYears <= 64)
     std::cout << "Neogene" << endl;
if ( ...)
     std::cout << "Paleogene" << endl;

etc.


The only way I think you can implement this code, is by catchhing the input with a char array/string. Then you ask the user for input. If the input is numeric (can be checked by using the isdigit() on the char array.string), you can go on with the function. You can change the char array into an int by using a stringstream.

If the input was not numeric, you can check it... if the input == "q", exit the program, if not, the input was invalid... that should do the job.

EDIT: whitenite1's way is also correct, but what if the input is genuinely 81 or 113, you'll unrightfully get the wrong response out of the program. It'll shut down instead of saying that you have to pick a later year.
Last edited on
@Omeyer

A capital 'Q''s value is 81 and a small 'q' has a value of 113. Both within the values for the DisplayPeriod. Might be best to just check for a zero, as the terminating input. Also, your displayPeriod function is overly done. Try it this way.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Time Periods.cpp : main project file.
#include <iostream>

using namespace std;

//Function Prototypes

void displayPeriod(int);


int main ()
{
	//Local Variables
	int millionYears; 
	
	//Display inital message and instructions
	cout << "TIME PERIOD NAMES!" << endl;
	cout << "Please enter the number of years (in millions) to display the names of the time periods" << endl;
	cout << "Example:  '150' means:  ";
	cout << "150,000,000 years ago" << endl;
	cout << "When finished, enter '0' to exit the program" << endl << endl;

	//Begin Main Loop
	do 
	{
		cout << "Enter a time period range" << endl;
		cin >> millionYears; //Input is type int, how do I read a type char in order to terminate program?
				
		if(millionYears != 0) // Display periods if NOT a zero
		{
			cout << "The time periods include:  " << endl;
			displayPeriod(millionYears);
		}

	}while (millionYears != 0); // End program with a 0 input

	return 0;
}
//Function Descriptions
void displayPeriod(int millionYears)
{
	if (millionYears < 23)
			cout << "Please enter a year range greater than 22 Million" << endl;
	if (millionYears >= 23)
			cout << "Neogene" << endl;
	if (millionYears >= 65)
			cout << "Paleogene" << endl;
	if (millionYears >= 136)
			cout << "Cretaceous" << endl;
	if (millionYears >= 192)
			cout << "Jurassic" << endl;
	if (millionYears >= 225)
			cout << "Triassic" << endl;
	if (millionYears >=280)
			cout << "Permian" << endl;
	if (millionYears >= 345)
			cout << "Carboniferous" << endl;
	if (millionYears >= 395)
			cout << "Devonian" << endl;
	if (millionYears >= 435)
			cout << "Silurian" << endl;
	if (millionYears >= 500)
			cout << "Ordovician" << endl;
	if (millionYears >= 570)
			cout << "Cambrian" << endl;
	if (millionYears>= 4500)
			cout << "Precambrian" << endl;
}
Try using the library function : exit(0);
coupled with an if else statement after accepting input.

Try to find out more about the library functions, pretty useful sometimes :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <limits>

// ...

    do 
    {
        cout << "Enter a time period range" << endl;

        if ( !(cin >> millionYears) )                                   // not a number entered?
        {
            cin.clear() ;                                               // clear error state.
            if ( cin.peek() == 'Q' || cin.peek() == 'q' )               // check to see if sentinel has been entered
                millionYears = 0 ;                                      // It has, signal end of input.
            else                                                        // sentinel hasn't been encountered
            {
                cout << "Invalid input.\n" ;                            // tell user input is invalid
                millionYears = 1 ;                                      // make sure millionYears is a reasonable value
                cin.ignore(numeric_limits<streamsize>::max(), '\n' ) ;  // remove offending input from the stream
            }
        }

        if(millionYears != 0) // Display periods if NOT a zero
        {
            cout << "The time periods include:  " << endl;
            displayPeriod(millionYears);
        }

    }while (millionYears != 0); // End program with a 0 input 
Last edited on
A capital 'Q''s value is 81 and a small 'q' has a value of 113. Both within the values for the DisplayPeriod. Might be best to just check for a zero, as the terminating input. Also, your displayPeriod function is overly done

Honestly, I was wondering about that. This was covered earlier in the chapter, about integer values of characters. I assumed that was why when Q or q was entered, the program went into an infinite loop. I think I will go with the zero as the terminating input. I knew there had to be an easier way of doing that function.

I started another version of this program using enum data types for the period names, but doing it this way seemed so much easier.

@ cire- Thank you for your suggestion, but we havent covered some of that stuff in the course yet, I think its best if I keep pace with what we have covered so far.

Guys, thanks a ton. My instructor still hasn't emailed me back yet, so this forum is much more efficient.
Topic archived. No new replies allowed.