Hello Tallow,
The problem with using the "cin.ignore()" is that without a prompt no one knows it is waiting for you to press enter.
It may appear a bit strange or different, but there is only 2 parts to worry about. "seconds" means the number in () is a whole number of seconds. If you use "milliseconds" the the number in () starts @ 1000 for 1 second and any number above or below that is part of a second. I have found that any number close to or < 250 is not enough time, but 1250 milliseconds may make a little difference.
These days I do not write the line. I have it set up in my IDE to just paste it in then put a number in the ().
The (0) in return 0; means the the program ended and there was no problem. "EXIT_SUCCESS" and "EXIT_FAILURE" are defined in the "<cstdlib>" header file. Although "cppreference",
https://en.cppreference.com/w/c/program/EXIT_status , says that the value of constants is "implementation defined" I have found that "SUCESS" is usually (0) zero and "FAILURE" is (1), but do not count on this. You will need to check the way your IDE.compiler header files define these variables. Usually I just (0)zero at the end of "main" and 1 or greater anywhere else. Actually any non (0)zero value will work to mean that there is a problem or that you made an exit from somewhere other than the end of "main". Personally I like using positive numbers, but negative numbers work. Its your choice.
I have found that when a return is not at the end of "main" it helps to make the returns 1, 2, 3 etc. This helps to know or track down where the program ended.
Last night after running the program several times and reading your last post I have some suggestions that you might want address.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
Yusuf Presents:
Part 1 of Adventures in Vetteres
If you have item numbers enter it, if not enter 0: 0
You walk through a laboratory. You see glasses filled with unknown substances.
1 = Inspect the glasses,
2 = Read the papers lying on the tables,
3 = Open the door on the door on the far side of the room.
Enter your Choice: 1
You inspect the tubes. You see several chemicals.
1= Open the glass tubes,
2= Put the glass tubes back: 1
You opened the glass tubes.
Oops you died!
The substances contained inside were poison.
THE END
|
On line 5 you could enter 1, 2, 12 or 21. The 1 or 2 may be easy to figure out, but the 12 or 21 is a problem with some instruction to use 1 of these numbers at the first prompt.
The menu is lacking a choice 4 to check the inventory. and maybe a 5 to exit.
Line 18 is fine, but when you define and initialize the variable you should make it a constant:
const string END_TEXT_FUNCT{ "Oops you died!" };
since you want to make sure that you do not accidentally change it.
Line 21. Adding the blank line and indenting is just something I like to do. It is not required, IMHO I think it looks better. I use the same idea with error messages to catch the users attention.
As for the "question == 4" first I would put the sleep line I showed you after the "cout" to pause long enough to read the message. Also I would put most of "main" in a do/while loop because after this prints you need to start over not end the program just to check your inventory. Also the do while loop would allow you to continue the program starting over instead of exiting and rerunning the program. Something to consider.
In this output:
Yusuf Presents:
Part 1 of Adventures in Vetteres
If you have item numbers enter it, if not enter 0: 0
You walk through a laboratory. You see glasses filled with unknown substances.
1 = Inspect the glasses,
2 = Read the papers lying on the tables,
3 = Open the door on the door on the far side of the room.
Enter your Choice: 2
You read the papers on the desk. Hmm...some classified information.
Would you like to continue reading?
1 = Yes,
2 = No: 1
There seems to be parts blanked out on the paper.
PAPER: The F.r..la is almost ready.
I still can't beleive I made a f.r..la for eternal y..t. - Dr C. Tallow.
1 = Try to guess the blanked out words,
2 = Read the paper attached to it: 1
You try to guess the blanked words, this is what you come up with.
F.r..la = Formula, y..t. = youth.
1 = Read the attached paper,
2 = Go home: 1
You read the attached paper. It seems like it is just a random string of letters.
1 = Keep the paper,
2 = Throw the paper: 1
You put the attached paper in your bag.
Congratulations! You sucessfully completed part one of Adventures in Vetteres!
The attached papers item number is 1,
enter it in the start of the game to have the attached paper in your inventory.
|
With your original code some of the lines displayed did not fit in mu console window and may not fit a lap top screen without wrapping when it reaches the right. When you write your code you need to think about what is displayed on the screen. If nothing else write a small test program that you can use to test your "cout" statements and see how they look on the screen B4 you put them in your 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
|
// <--- Most comon includes.
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
#include <chrono>
#include <thread>
int main()
{
std::cout << "\n\n Your Original \"cout\"\n\n";
std::cout << "Correct! The door clicks open. And within is a treasure chest. 1= Open the chest, 2= Get out of the laboratory immediately: ";
std::cout << "\n\n Corrected \"cout\"\n\n";
std::cout <<
"Correct! The door clicks open. And within is a treasure chest.\n"
" 1 = Open the chest,\n"
" 2 = Get out of the laboratory immediately: ";
std::cout << "\n\n Revised \"cout\"\n\n";
std::cout <<
"Correct! The door clicks open. And within is a treasure chest.\n"
" 1 = Open the chest,\n"
" 2 = Get out of the laboratory immediately!\n"
" Enter choice: ";
// <--- Keeps console window open when running in debug mode on Visual Studio. Or a good way to pause the program.
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue: ";
std::cin.get();
return 0; // <--- Not required, but makes a good break point.
}
|
You would not save this with all the "cout" statements, but this does give the output of:
Your Original "cout"
Correct! The door clicks open. And within is a treasure chest. 1= Open the chest, 2= Get out of the laboratory immediate
ly:
Corrected "cout"
Correct! The door clicks open. And within is a treasure chest.
1 = Open the chest,
2 = Get out of the laboratory immediately:
Revised "cout"
Correct! The door clicks open. And within is a treasure chest.
1 = Open the chest,
2 = Get out of the laboratory immediately!
Enter choice:
Press Enter to continue:
|
In the 1st "cout" that is the way it looks in my console window. Not the easiest line to read. The 2nd output shortens the lines and makes a menu that looks better. In the 3rd output the layout of the "cout" in the program made it very easy to add the last line and test how it looks. Notice that the (" Enter choice: ";) has no "\n" or "endl". This puts a following "cin" on the same line. The spaces at the beginning of the string are not required, but the indentation looks nicer.
Andy