7 Replies - 10437 Views - Last Post: 08 October 2011 - 08:23 AM Rate Topic: -----

#1 landocammando  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-October 11

Trying to build a simple game with C++ for beginners by Mark Lee

Posted 06 October 2011 - 10:32 PM

Hey I bought the book C++ for the absolute beginner second edition by Mark Lee. I am in the first chapter trying to build the simple game. I am new at this and am lost. fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source? Don't know what this means.... Here is my program thanks so much for any help guys!!!!

 #include <iostream>
#include <string>

int main(void) //tells a pirate story
{
	using std::cout;
	using std::cin;
	using std::string;
	int buddies;
	int afterBattle;
	string exit;
	cout<< "you are a pirate and are walking"
		 << "along in the crime filled \n"
		 << "city of Havana (in 1789). "
		 << "How many of your pirate buddies \n"
		 << "do you bring along? (Any numbers between 11 and 115)\n"


//records the amount of friends you bring along
cin >> buddies;
//calculates the amount of pirates left after the battle.
after the battle = 1 + buddies - 10;


cout << "suddenly 10 musketeers jump out "
	  << "from the local tavern and \n"
	  << "draw their swords. "
	  << "10 musketeers and 10 pirates die in the \n"
	  << "battle. There are only "
	  << (buddies + 1 - 10)
	  << "pirates left. including you. \n\n";

cout << "the fallen drop a total of 107 gold coins.\n"
      << "the bounty is split evenly. which works out to "
	  << (107 / after battle) << "gold coins \n"
	  << "for each survivor.\n";
cout << "the last " << (107 % afterbattle) << "are fought over "
      << "in a big drunken brawl.\n ";
cout << "These last few coins are spent on more booze during the\n"
	  << "course of the brawl. Eventually everyone retires\n"
	  << "peacefully on the bar room floor.\n"
	  << "Another successful day as a pirate!\n"
return 0;
}


	 << (buddies + 1 - 10)
     << "pirates left, including you.\n\n";

cout << "the fallen drop a total of 107 fold coins.\n"
	  << "the loot is split evenly, which works out to "
	  << (107 / afterBattle) << "gold coins \n"
	  << "for each survivor. leaving ";
cout << (107 % afterBattle) << " unclaimed coins.\n";
	  << "how many of your pirate buddies \n"
      << "did you bring along? (Any number between 11 and 115)\n";
cout << "you and the others argue over who should get the extra \n"
	  << "coins, and soon a big drunken brawl breaks out!\n\n";
cout << "In the end, you are triumphant and "
	  << (107 / afterBattle) + (107 % afterBattle)
	  << " coins richer!\n\n";
return 0;
}


This post has been edited by Salem_c: 06 October 2011 - 10:44 PM
Reason for edit:: Fixed the [code][/code] tags


Is This A Good Question/Topic? 0
  • +

Replies To: Trying to build a simple game with C++ for beginners by Mark Lee

#2 Yatta!  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 122
  • Joined: 06-September 09

Re: Trying to build a simple game with C++ for beginners by Mark Lee

Posted 06 October 2011 - 10:53 PM

#include <iostream>
#include <string>
#include "stdafx.h"



May fix it. It might have something to do with the compiler you're using. Someone else could give a more indepth answer.
Was This Post Helpful? 0
  • +
  • -

#3 Karel-Lodewijk  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 449
  • View blog
  • Posts: 849
  • Joined: 17-March 11

Re: Trying to build a simple game with C++ for beginners by Mark Lee

Posted 07 October 2011 - 03:38 AM

I really found only minor issues ';'s missing, somehow you end your function twice. Are you certain you are compiling it as c++ code, stdafx.h shouldn't even be a c++ header ?

--- test2.cpp	2011-10-07 12:27:04.953773789 +0200
+++ test.cpp	2011-10-07 12:24:29.453773718 +0200
@@ -16 +16 @@
-		 << "do you bring along? (Any numbers between 11 and 115)\n"
+		 << "do you bring along? (Any numbers between 11 and 115)\n";
@@ -22 +22 @@
-after the battle = 1 + buddies - 10;
+afterBattle = 1 + buddies - 10;
@@ -35 +35 @@
-	  << (107 / after battle) << "gold coins \n"
+	  << (107 / afterBattle) << "gold coins \n"
@@ -37 +37 @@
-cout << "the last " << (107 % afterbattle) << "are fought over "
+cout << "the last " << (107 % afterBattle) << "are fought over "
@@ -43,4 +42,0 @@
-return 0;
-}
-
-
@@ -54 +50 @@
-cout << (107 % afterBattle) << " unclaimed coins.\n";
+cout << (107 % afterBattle) << " unclaimed coins.\n"


This post has been edited by Karel-Lodewijk: 07 October 2011 - 03:39 AM

Was This Post Helpful? 0
  • +
  • -

#4 landocammando  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 06-October 11

Re: Trying to build a simple game with C++ for beginners by Mark Lee

Posted 07 October 2011 - 11:49 AM

Im using visual studio c++ 2010.... Im just doin what the book says.
Was This Post Helpful? 0
  • +
  • -

#5 aaa111  Icon User is offline

  • D.I.C Regular

Reputation: 88
  • View blog
  • Posts: 284
  • Joined: 21-February 07

Re: Trying to build a simple game with C++ for beginners by Mark Lee

Posted 07 October 2011 - 12:09 PM

Look some of your uses of the variables:
after the battle = 1 + buddies - 10;
...
cout << "the fallen drop a total of 107 gold coins.\n"
      << "the bounty is split evenly. which works out to "
	  << (107 / after battle) << "gold coins \n"
	  << "for each survivor.\n";


Now look at the declaration of afterBattle:
int afterBattle;

You have to use the same name for the variable as your declaration.

This post has been edited by aaa111: 07 October 2011 - 12:10 PM

Was This Post Helpful? 0
  • +
  • -

#6 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: Trying to build a simple game with C++ for beginners by Mark Lee

Posted 08 October 2011 - 07:27 AM

While using Microsoft .Net framework or even the old Visual C++ 6 -- you must include microsoft's precompiled header stdafx.h for all of your programs. Instead, this is not required for other compilers, even if you use -- you will end up with an error.
Was This Post Helpful? 0
  • +
  • -

#7 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: Trying to build a simple game with C++ for beginners by Mark Lee

Posted 08 October 2011 - 07:39 AM

Did you got just 1 error? Really wondering!

10	    int afterBattle;
22	after the battle = 1 + buddies - 10;



If it would be afterthebattle -- maybe there would be some exceptional chance to have that variable -- perhaps with 0.01% chances or less. But this would never come true -> after the battle. It's totally against the variable declaration rules.

35	      << (107 / after battle) << "gold coins \n"


Same here!

16	         << "do you bring along? (Any numbers between 11 and 115)\n"
42	      << "Another successful day as a pirate!\n"



Don't wish to terminate them?

Perhaps you might want,

22	afterBattle = 1 + buddies - 10;

35	      << (107 / afterBattle) << "gold coins \n"

16	         << "do you bring along? (Any numbers between 11 and 115)\n";

42	      << "Another successful day as a pirate!\n";



Hope it Helpes!
Was This Post Helpful? 0
  • +
  • -

#8 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 856
  • View blog
  • Posts: 2,339
  • Joined: 20-August 07

Re: Trying to build a simple game with C++ for beginners by Mark Lee

Posted 08 October 2011 - 08:23 AM

As AKMafia001 hinted at, this error relates to a Microsoft Visual C++ feature which you can disable.

To disable it, right-click on your project (Not the solution) in the Visual C++ Solution Explorer window (That window will probably be along the left or right hand side of the Visual C++ layout depending on your default settings and/or whether you've customised the look to move things around)
Open the properties dialog, then navigate through the options tree to
Configuration Properties -> C/C++ -> Precompiled Headers. In the left-hand panel set your configuration to not using precompiled headers.

When you disable precompiled headers,the Visual C++ compiler will not look for stdafx.h

This post has been edited by Bench: 08 October 2011 - 08:29 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1