Hello IanTG,
If you put every waking hour into the program you might just make it.
In addition to what
dhayden has said I will cover some other points:
Your include statements:
1 2 3 4 5 6 7 8 9 10 11
|
#include<iostream>
#include<string>
#include <cctype> // <--- For "std::tolower() and std::toupper()" + others.
#include<stdlib.h> // <--- C header file.
#include<cstdlib> // <--- C++ header file. This is the 1 to use.
#include<fstream>
using namespace std; // <--- Best not to use.
int UnknownFunc();
|
Lines 4 and 5 are the same with the exception that "cstdlib" is designed to work with C++ programs. You only need "cstdlib" not both.
The int UnknownFunc();" function is to use the while loop that follows "main". As is it just causes an error. I used this to avoid the error and because I do not know if it should be on "main" or a function.
Then in "main":
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
int main()
{
string saveload;
cout <<
" Welcome to the program!\n"
" ---------------------------\n"
" | save - Save to a file |\n"
" | load - Load from a file |\n"
" ---------------------------\n\n";
UnknownFunc(); // <--- Used for testing.
getline(cin, saveload);
if (saveload == "load")
|
You do not need a "cout" and "endl" for every line. And prefer to use the new line, (\n), over the function "endl".
Each line of a quoted string may look separate, but it is considered just 1 big string, so just ` "cout" to start and 1 (;) to end. This looks more like what you will see on the screen and it is easier to change, i.e., add to or delete from. Such as the last line should be something like " Enter choice: ".
The function is just for testing the while loop that followed "main".
Line 3 defines a "std::string", which will work.
Line 16 tests for "load", but what about "L", :LOad", "LOAD" and other combinations. This if statement could get very large.
It would work better to define "saveload" as a "char" and deal with just 1 character instead of a string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
int main()
{
char saveload{}; // <--- ALWAYS initialize all your variables.
cout <<
" Welcome to the program!\n"
" ---------------------------\n"
" | save - Save to a file |\n"
" | load - Load from a file |\n"
" ---------------------------\n"
" Enter choice: "; // <--- Added.
UnknownFunc(); // <--- Used for testing.
cin >> saveload;
saveload = static_cast<char>(std::toupper(static_cast<unsigned char>(saveload))); // <--- Added.
if (saveload == 'L')
|
This way you only have 1 character to check. The opening message would have to be changed.
For the while loop I did this:
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
|
int UnknownFunc()
{
while (1)
{
string name;
cout <<
"Hello welcome to the camping game simulator! I am so glad you are here,\n"
"if you want to start this game type in S,\n"
"if you want to exit this game type in E\n";
string exit;
//cin >> exit;
//if (exit == "E" || exit == "e")
// return 0;
//string start;
//if (start == "S" || start == "s")
// cin >> start;
cout <<
"\n"
"Okay let's begin our journey of camping!\n\n" // <--- The space at the end is unnecessary.
"The place where you are camping is somewhere in the woods. You pack everything\n"
"you need to enjoy your day outside in the fresh air and with nature. Once you\n"
"start to get into your car, clouds start to form around each other and it starts\n"
"to get darker,but you continue on to your camping site.\n\n";
cout <<
"\n(#1) You finally arrive at your camping grounds and it starts to rain down,\n"
"then you hear thunder and lightning, what do you do?\n\n"
"(1) Stay in car until rain ends\n"
"(2) Start pitching your tent\n"
"(3) Unpack all your equipment\n"
"(4) Make a fire\n"
" Eneter choice: "; // Added.
|
On line 5 you define the variable:
string name;
, but never use it. I am thinking this should be defined in "main" and passed to any function that would need it.
On line 12 you define "exit" as a string, but it should be a "char" based on the menu choices. Also it should have a better name like "choice" or something else.
Line 19 defines "start", but you do not need this.
Line 16 compares "exit" to either case of "e", line 21 should be comparing "exit" to either case of "S".
You are asking for the same thing twice and putting the answer into 2 different variables. Not a good idea. You just need 1 variable to use with the 2 if statements.
I have changed the first "cout" statements. Your original code would look something like this on my screen:
Okay let's begin our journey of camping! The place where you are camping is somewhere in the
woods. You pack everything you need to enjoy your day outside in the fresh air and with natur
e. Once you start to get into your car, clouds start to form around each other and it starts
to get darker, but you continue on to your camping site.
(#1) You finally arrive at your camping grounds and it starts to rain down, then you hear thu
nder and lightning, what do you do?
(1) Stay in car until rain ends
(2) Start pitching your tent
(3) Unpack all your equipment
(4) Make a fire
|
This may look fine on a wider screen, but do not count on that happening.
Notice on the 2nd line how the "e" is wrapped to the next line. Then 4 lines later notice how the word "thunder" is broke up. Not easy to read. You need to think about what this program will be run on and the size of the screen.
A better choice is what I have done. The output looks like:
Okay let's begin our journey of camping!
The place where you are camping is somewhere in the woods. You pack everything
you need to enjoy your day outside in the fresh air and with nature. Once you
start to get into your car, clouds start to form around each other and it starts
to get darker,but you continue on to your camping site.
(#1) You finally arrive at your camping grounds and it starts to rain down,
then you hear thunder and lightning, what do you do?
(1) Stay in car until rain ends
(2) Start pitching your tent
(3) Unpack all your equipment
(4) Make a fire
Eneter choice:
|
This makes the lines shorter and quicker to read. Plus it considers being displayed on a smaller screen width.
Your program will work with what you have. You just need to clean some parts up and deal with reading and saving a file.
Andy