resource advice(book examples wont compile)
Posted: Fri Mar 30, 2012 7:47 am
hey i've been trying to learn c++ and have tried many books looking for one best suited to my needs. it seems that every one i try the examples wont compile including this one and i'm directly downloading the source code from the publishers website. its a huge problem because when your learning a language the biggest hangup, at least for me, is syntax. anyway here's an example so if anyone could lead me in the right direction tword some better resources or perhaps a different way of going about handling the faulty code that seems ubiquitous it would be greatly appriciated.
as always thank you fort your time and effort.
Code: Select all
// Word Jumble
// The classic word jumble game where the player can ask for a hint
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
enum fields {WORD, HINT, NUM_FIELDS};
const int NUM_WORDS = 5;
const string WORDS[NUM_WORDS][NUM_FIELDS] =
{
{"wall", "Do you feel you're banging your head against something?"},
{"glasses", "These might help you see the answer."},
{"labored", "Going slowly, is it?"},
{"persistent", "Keep at it."},
{"jumble", "It's what the game is all about."}
};
srand(static_cast<unsigned int>(time(0)));
int choice = (rand() % NUM_WORDS);
string theWord = WORDS[choice][WORD]; // word to guess
string theHint = WORDS[choice][HINT]; // hint for word
string jumble = theWord; // jumbled version of word
int length = jumble.size();
for (int i=0; i<length; ++i)
{
int index1 = (rand() % length);
int index2 = (rand() % length);
char temp = jumble[index1];
jumble[index1] = jumble[index2];
jumble[index2] = temp;
}
cout << "\t\t\tWelcome to Word Jumble!\n\n";
cout << "Unscramble the letters to make a word.\n";
cout << "Enter 'hint' for a hint.\n";
cout << "Enter 'quit' to quit the game.\n\n";
cout << "The jumble is: " << jumble;
string guess;
cout << "\n\nYour guess: ";
cin >> guess;
while ((guess != theWord) && (guess != "quit"))
{
if (guess == "hint")
{
cout << theHint;
}
else
{
cout << "Sorry, that's not it.";
}
cout <<"\n\nYour guess: ";
cin >> guess;
}
if (guess == theWord)
{
cout << "\nThat's it! You guessed it!\n";
}
cout << "\nThanks for playing.\n";
return 0;
}