I need help with this C++ console code error. I have been working on these error
ID: 3848266 • Letter: I
Question
I need help with this C++ console code error. I have been working on these errors for a week now. I have posted this code 3 times now in here and would appreciate a solution to these errors it continues to throw. I have left the errors in the comments after I try to use the suggested "answered code" after it debugs, but no one ever offers to answer the error comment question:: **This is the instructions given to fill in the blanks of this assignment: Currently, there is a test program that calls a player’s class. The program asks the player for a name and then gives options on what happens next. The only option available is to quit. Add an additional option and supporting code in the player header that will allow the player to input a preferred console type. After the player inputs a preferred console type, be sure to redisplay it.
>>CPP Files<<
//Main TextAdventure.cpp
// Chapter7-TextAdventure.cpp : Defines the entry point for the console application.
#include "Player.h"
#include "GameLoop.h"
#include <tchar.h>//Header for TCHAR
int _tmain(int argc, _TCHAR* argv[])
{
Game game;
game.RunGame();
return 0;
}
>>GameLoop.cpp<<
#include "GameLoop.h"
#include <iostream>
using namespace std;
void Game::WelcomePlayer()
{
cout << "Welcome to Text Adventure!" << endl << endl;
cout << "What is your name?" << endl << endl;
string name;
cin >> name;
m_player.SetName(name);
cout << endl << "Hello " << m_player.GetName() << endl;
}
void Game::GivePlayerOptions() const
{
cout << "What would you like to do? (Enter a corresponding number)" << endl << endl;
cout << "1: Quit" << endl << endl;
}
void Game::GetPlayerInput(string& playerInput) const
{
cin >> playerInput;
}
PlayerOptions Game::EvaluateInput(string& playerInput) const
{
PlayerOptions chosenOption = PlayerOptions::None;
if (playerInput.compare("1") == 0)
{
cout << "You have chosen to Quit!" << endl << endl;
chosenOption = PlayerOptions::Quit;
}
else
{
cout << "I do not recognise that option, try again!" << endl << endl;
}
return chosenOption;
}
void Game::RunGame()
{
WelcomePlayer();
bool shouldEnd = false;
while (shouldEnd == false)
{
GivePlayerOptions();
string playerInput;
GetPlayerInput(playerInput);
shouldEnd = EvaluateInput(playerInput) == PlayerOptions::Quit;
}
}
>>stdafx.cpp<<
// stdafx.cpp : source file that includes just the standard includes
// Chapter7_TextAdventure.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information
#include "stdafx.h"
// TODO: reference any additional headers you need in STDAFX.H
// and not in this file
>>Header Files<<
GameLoop.h
#pragma once
#include "Player.h"
#include "PlayerOptions.h"
class Game
{
private:
Player m_player;
void WelcomePlayer();
void GivePlayerOptions() const;
void GetPlayerInput(std::string& playerInput) const;
PlayerOptions EvaluateInput(std::string& playerInput) const;
public:
void RunGame();
};
Player.H
#pragma once
#include <string>
class Player
{
private:
std::string m_name;
public:
Player() {}
void SetName(const std::string& name)
{
m_name = name;
}
const std::string& GetName() const
{
return m_name;
}
};
PlayerOptions.H
#pragma once
//Class
enum class PlayerOptions
{
//Enumerator
Quit, None
};
stdafx.h
#pragma once
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
targetver.h
#include <SDKDDKVer.h>
>>This is the debug error I continue to get.<< It states :
Unable to start program 'C:#########Chapter7_TextAdventure.exe'.
The system cannot find the file specified.
This is the debug errors:
Severity Code Description Project File Line Suppression State
Error C1010 unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source? Chapter7_TextAdventure c:####lchapter7_textadventurechapter7_textadventuregameloop.cpp 49
Error C1010 unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source? Chapter7_TextAdventure c:####chapter7_textadventurechapter7_textadventurechapter7_textadventure.cpp 14
Explanation / Answer
The error message tells to include stdafx.h to your source. Go ahead and #include<stdafx.h> in your Chapter7_TextAdventure.cpp file.
If that does not seem to work, you may try turning off precompiled headers.
For example, in VS2010, go to Common Properties->Configuration Properties->C/C++->Precompiled Headers, then set Precompiled Header to "Not Using Precomiled Header".
However, bear in mind that turning off precompiled headers will result in making your program take longer to compile, as the huge header files will also be compiled along with your program.