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.
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.
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.
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.
// Time Periods.cpp : main project file.
#include <iostream>
usingnamespace 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;
}
#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
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.