HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

C++ game with splash screen and menus

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
splashmenuswithgameandscreen

Problem

As of now this game only consists of:

  • Splash Screen



  • MainMenu



  • SubMenus



Did I apply bad practices, or did I do something well? Is there a better way to do it?

`#include
#include ///filestream lib-- needed for file i/o
#include ///Needed for alt clear screen function
#include ///setw-- for indentation on title screen or headers in game
#include
#include ///used for char -> int input error
#include /Used for pause screen--Windows Specific-- MUST FIND PORTABLE SOLUTION, possibly ifdef/
//#include
#include
#include ///used for initializer for now

//
///used to clear screen cross platform
///#ifdef _WIN32 //checks to see if windows OS
///#define CLEAR "cls"
///#else //In any other OS
///#define CLEAR "clear"
///#endif
/*/
int inputError();///custom error message at top of screen
void Clear();///Safer way to clear screen non system()
void SplashScreen();///Production logo
void LoadTextFile(std::string);
void Initialize(int, char**);///Loading game
void MainMenu();
void Help();
void Game();
/****/
int main(int arg, char** argv)
{
SplashScreen();///display Splash screen
Initialize( arg, argv);///Pretend to be loading game
Clear();///clear Splash and other text

MainMenu();///Pretty Much the start of the main Game loop
}
/*/
void Clear()
{
COORD topLeft = { 0, 0 };
HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);///get a handle to the console
CONSOLE_SCREEN_BUFFER_INFO screen;
DWORD written;///num of characters written

GetConsoleScreenBufferInfo(console, &screen);

FillConsoleOutputCharacterA(
console, ' ', screen.dwSize.X * screen.dwSize.Y, topLeft, &written
);
///Overwrite console with whitespace
FillConsoleOutputAttribute(
console, FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_BLUE,

Solution

This is wrong in every language:

while(!myFile.eof())///checks to see if End Of File... Loops unil end
    {


This is because the last successful read read "up to" but not past the EOF. Thus there is no data left but the EOF flag is not set. So condition will allow the loop to be entered evn though the next read is going to fail.

So this line:

std::getline(myFile, tempString);
        tempString += "\n";
        Lines += tempString;


Will fail (after all the other lines have been read) and you are not checking for it. Thus Lines has an extra '\n' on the end at the very least. If you had written:

if (std::getline(myFile, tempString))
        {
            tempString += "\n";
            Lines += tempString;
        }


Then Line would have at least bee correct.

But the better way to write this loop is:

std::string tempString;
    while(std::getline(myFile, tempString))
    {
        Lines += (tempString + '\n')
    }
    std::cout << Lines << std::endl << std::endl;


Your comments are terrible:

SplashScreen();///display Splash screen


I can guess from the name of the function that the splashScreen is going to be displayed. I don't need the extra clutter of this comment. Comments should be reserved for telling my why something is happening. I can easily read in the code how it is happening (unless there is some big magic formula you may want to explain what that is doing).

Comments that repeat what the code is saying are actually WORSE than no comments. This is because over time the comments will fall out of step with the code. You need to take extra care to maintain the comments. Comments that are out of sync with the code are a real problem as the maintainer will come along and see that the code and comments do not match and will have to spend time trying to work out which are correct before fixing one or the other.

Prefer '\n' over std::endl

std::cout <<"Previous selection was invalid, try another from the list: " << std::endl;

// Just write
std::cout <<"Previous selection was invalid, try another from the list: \n";


The difference is an extra flush. The flush will not help you but it can definitely make the performance of your code much worse.

Code Snippets

while(!myFile.eof())///checks to see if End Of File... Loops unil end
    {
std::getline(myFile, tempString);
        tempString += "\n";
        Lines += tempString;
if (std::getline(myFile, tempString))
        {
            tempString += "\n";
            Lines += tempString;
        }
std::string tempString;
    while(std::getline(myFile, tempString))
    {
        Lines += (tempString + '\n')
    }
    std::cout << Lines << std::endl << std::endl;
SplashScreen();///display Splash screen

Context

StackExchange Code Review Q#74380, answer score: 4

Revisions (0)

No revisions yet.