C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 18th, 2009, 01:21 PM
acting15 acting15 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 1 acting15 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 18 m 53 sec
Reputation Power: 0
General - C++ Hangman game tutorial I cant get to compile

I'm following a tutorial on the net and I understand everything in it but I'm getting fatal error C1083: Cannot open include file: 'fstream.h': No such file or directory. I'm using Visual c++ 2008 express

I added the header files.




#include "stdafx.h"
#include <iostream> // For input/output
#include <fstream.h> // For file input/output
#include <string.h> // For strcpy
#include <time.h> // For time
#include <stdlib.h> // For toupper and tolower

#define MAX_WORD_SIZE 15 // The max size for words
#define MAX_WORDS 255 // The max number of words


void Loadfile(); // Prototype for our functions
void RunGame();
void DrawGallows(int State);

typedef char String[MAX_WORD_SIZE]; // Make a char type with 15 chars
String Words[MAX_WORDS -1]; // This array will hold our words
int Count; // Word count

// The main function of our hangman game
void main()
{
char Continue = 'Y'; // End game if = to 'N'
cout << "Welcome to Hangman ... Don't lose your head!" << endl;

Loadfile(); // Load the data file

// Continue the game as long as the player wants
while(Continue == 'Y')
{
RunGame(); // Run the game
cout << endl<< "Do you want to play again (Yes or No)?" ;
cin << Continue;
Continue = toupper(Continue);
}

// Say good bye
cout << endl << "Thanks for playing ." << endl;
}

// This will load our file
void LoadFile()
{
char C; // Used to find EOF
ifstream DATfile; // The in file

Count=0; // Set count to 0

// Open the data file
DATfile.open("words.dat");

//Loop untill end of file
while((C=Datfile.peek()) != EOF)
// Get the next word and then increment Count
Datfile >> Words[Count++1];

// If we surpass the max exit and tell the user
if(Count > MAX_WORDS -1)
{
cout << endl << "Too many words in the file, stopping with "
<< MAX_WORDS << " << endl;
Count = MAX_WORDS;
break;
}

}

// We need to subtract one to get the correct number of words
Count--;

// Close the data file
Datfile.close();

}

// This function will run the game
void RunGame()
{
int Word; // This will hold the subscript of our word
int Size; // This will hold the length of our word
int State=1; // This holds the games state
int Subscript=0; // This will hold subscripts
char Guess[MAX_WORD_SIZE]; // This will hold the current word
char Copy[MAX_WORD_SIZE]; // This will hold a copy of the word
char Letter; // This will be their letter guess
int Correct=0; // This is a True/False value deciding if they got a good answer

// Seed and create a random number
srand((unsigned)time( NULL )); // Use time as a seed
Word = rand() % Count;

// Make a local copy of the word
strcpy(Copy,Words[Word]);

Size = strlen(Words[Word]); // Get the word's size

// Create a null terminated string to represent the word as the player knows it
for(; Subscript < Size; Subscript++)
{
Guess[Subscript] = '-';
}

// Insert the null character
Guess[Subscript] = '\0';

// Go untill the player is hung
while(State!=6)
{
DrawGallows(State); // Draw the Gallows
cout << Guess << endl; // Draw guess

cout << "Guess a letter :";
cin << Letter;

// We will use only lower case letter
Letter = tolower(Letter);

// Loop through the word
for(Subscript = 0; Subscript < Size; Subscript++)
{

// If the guess is good, tell the user and update Guess
if(Copy[Subscript] == Letter
{
Guess[Subscript] = Letter;
Correct = 1;
cout << endl << "Good Guess!";

// If guess equals the word, they won, so exit
if(strcmp(Words[Word],Guess == 0
{
cout << endl << "Yes, you survived and won!";
return;
}
}
}

// If they didn't get a letter correct, tell the user
if(Correct == 0)
{
cout << endl << "Sorry, bad guess!";
State++;
}

Correct = 0; // Reset Correct

}

DrawGallows(State); // Draw the Gallows at end of game. They lost if they are here so tell them the answer.
cout << "The word was : " << Words[Word] << endl << endl;

}

// This will Draw the Gallows according to the state
void DrawGallows(int State)
{
if(State==6)
{
// The \\ will translate as '\' because it is a special char
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" | O "<<endl
<<" | /|\\ "<<endl
<<" | / \\ "<<endl
<<" |Your Dead "<<endl
<<" ============"<<endl<<endl;
}
else if(State==5)
{
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" | O "<<endl
<<" | /|\\ "<<endl
<<" | \\ "<<endl
<<" | "<<endl
<<" ============"<<endl<<endl;
}
else if(State==4)
{
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" | O "<<endl
<<" | /|\\ "<<endl
<<" | "<<endl
<<" | "<<endl
<<" ============="<<endl<<endl;
}
else if(State==3)
{
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" | O "<<endl
<<" | /| "<<endl
<<" | "<<endl
<<" | "<<endl
<<" ============="<<endl<<endl;
}
else if(State==2)
{
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" | O "<<endl
<<" | | "<<endl
<<" | "<<endl
<<" | "<<endl
<<" ============="<<endl<<endl;
}
else if(State==1)
{
cout<<endl<<endl
<<" +----+ "<<endl
<<" | | "<<endl
<<" | "<<endl
<<" | "<<endl
<<" | "<<endl
<<" | "<<endl
<<" ============="<<endl<<endl;
}

}

Reply With Quote
  #2  
Old July 8th, 2009, 06:17 PM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 296 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 13 h 30 m 11 sec
Reputation Power: 6
May be you failed to include the correct path. It may be true that fstream.h is defined in a namespace and that you need not write .h after it.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > General - C++ Hangman game tutorial I cant get to compile


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
Create the Optimal Architecture for your Critical Applications
Warburton's the largest independently owned bakery in the UK faced a number of difficult challenges in providing the most robust yet efficient IT infrastructure for their organization's success. IBM's services combined with their xSeries servers created the perfect platform for their SAP environment with sufficient flexibility, and did so in very time effective fashion.

Request Your Free Technology Downloads!
 
Five Best Practices for Deploying a Successful Service-Oriented Architecture
This white paper describes the benefits you can expect with SOA, and how IBM can help take your business there.

Request Your Free Technology Downloads!
 
Gartner Magic Quadrant for Application Delivery Controllers
Gartner summarizes its view on Application Delivery Controllers, evaluates strengths and weaknesses of solutions, and provides Magic Quadrant reporting for a quick comparison across all vendors. Learn from Gartner how you can benefit from an all-in-one device like Citrix NetScaler that delivers the highest levels of availability, performance and security.

Request Your Free Technology Downloads!
 
Knowledge is Power
What you don't know can hurt you, and is likely costing you money and increasing your security risks during an era of scarce resources. This white paper proposes six key strategies that enterprise security managers can use to improve their network defense posture.

Request Your Free Technology Downloads!
 
Rationalizing the Multi-Tool Environment
The rationalized multi-tool approach is flexible, scalable and cost effective. It provides the necessary input to the IT service management business processes. It preserves prior investments in monitoring tools, empowers technologists to select the best tools with which to do their jobs, and enhances effective response to incidents.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2010 by Developer Shed. All rights reserved. DS Cluster 11 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek