Page 1 of 1

resource advice(book examples wont compile)

Posted: Fri Mar 30, 2012 7:47 am
by vegiestirfrypizza
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.

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;
}
as always thank you fort your time and effort.

Re: resource advice(book examples wont compile)

Posted: Fri Mar 30, 2012 4:19 pm
by dandymcgee
vegiestirfrypizza wrote:it seems that every one i try the examples wont compile including this one
What compile errors are you getting?

Re: resource advice(book examples wont compile)

Posted: Fri Mar 30, 2012 10:58 pm
by vegiestirfrypizza
this is the error
error C2676: binary '[' : 'main::fields' does not define this operator or a conversion to a type acceptable to the predefined operator and it refers to this line of code.

Code: Select all

const string WORDS[NUM_WORDS][NUM_FIELDS] =
although there are other errors they all seem to stem from this one. i understand that you guys don't want to help me with every stupid problem i have, I'm not trying to be that guy, i guess i was looking for more in the way of general advice.

Re: resource advice(book examples wont compile)

Posted: Sat Mar 31, 2012 5:40 am
by tappatekie
I don't know much about C/C++ but I do know C# and at a guess, your NUM_FIELDS enum value needs to have a integer (numeric) value?

e.g

Code: Select all

enum fields { WORD, HINT, NUM_FIELDS=2 };
Not sure on this but check it out.. Because if I did similar code in C# it would bitch like crazy saying that the enum fields must have a value if I wanted to grab a integer value from it (being an array length).

At a guess, what you are doing is creating an array of [size][size] but the size cannot be defined since NUM_FIELDS has no value to get from.

Also you may have errors stemming from this one if your code is trying to get a numerical value from other enum fields in "fields". Or (at a guess), you may need to check out your

Code: Select all

string WORD
because you may be defining a "jagged array" but it is being defined to a string. At a guess use something like

Code: Select all

string[][] WORD

Re: resource advice(book examples wont compile)

Posted: Sat Mar 31, 2012 10:27 am
by dandymcgee
vegiestirfrypizza wrote:i understand that you guys don't want to help me with every stupid problem i have, I'm not trying to be that guy, i guess i was looking for more in the way of general advice.
Well the best general advice I can give is that experience is the only way to learn what the compiler errors mean and be able to fix them yourself. In order to do that, when you first come across a new error just paste it in to google (removing any unique information such as filenames or paths) and see how others have fixed it. Eventually you'll be familiar with most, if not all, of the common errors and you'll be able to fix it in a few seconds with no problem at all.

Re: resource advice(book examples wont compile)

Posted: Sat Mar 31, 2012 3:10 pm
by THe Floating Brain
dandymcgee wrote:
vegiestirfrypizza wrote:i understand that you guys don't want to help me with every stupid problem i have, I'm not trying to be that guy, i guess i was looking for more in the way of general advice.
Well the best general advice I can give is that experience is the only way to learn what the compiler errors mean and be able to fix them yourself. In order to do that, when you first come across a new error just paste it in to google (removing any unique information such as filenames or paths) and see how others have fixed it. Eventually you'll be familiar with most, if not all, of the common errors and you'll be able to fix it in a few seconds with no problem at all.
Although I believe that you should try to solve the problem on your own at first, I concur.
tappatekie wrote:I don't know much about C/C++ but I do know C# and at a guess, your NUM_FIELDS enum value needs to have a integer (numeric) value?
e.g

Code: Select all

enum fields { WORD, HINT, NUM_FIELDS=2 };
C++ compilers usually do not check this sort of thing.

---SPOILER--- (a possible solution, ONLY read if you absolutely can not fix the error, you will encounter many errors and if you can not learn to fix them then you wont be a competent programmer):
Array size's usually can only be defined with per-proccessor definitions, for example.

Code: Select all

#define ARR_SIZE 10
int arr[ARR_SIZE];

My advice to you is to NEVER TRUST EXAMPLES. They usually contain pseudo and or untested code.

Re: resource advice(book examples wont compile)

Posted: Sat Mar 31, 2012 8:48 pm
by wtetzner
It compiles fine for me with both Clang and g++. What compiler are you using?

Also, using an enum to define integer constants is ugly. It hides the intent of the programmer. Also, it seems to be what's confusing your compiler. It seems like the compiler can't tell that the enum value is an integer.

Re: resource advice(book examples wont compile)

Posted: Sun Apr 01, 2012 11:29 am
by vegiestirfrypizza
I'm using Microsoft visual c++ express(note:i understand its not the easiest compiler to go with but i need to get used to it because i may be getting a job soon that will demand it)
in responce to
My advice to you is to NEVER TRUST EXAMPLES. They usually contain pseudo and or untested code.
yeah i guess i really could have used that advice a while back

also: i understand that programming, is, by nature about seeing problems and solving them, it's about understanding the computer and being able to persevere. i will never come to this forum unless i have tried for quite a long time and not be able to come up with an answer, after which i google it and then i ask my programmer friend(he's a c programmer, and a novice at that, and so his help is limited) then if i really cant figure it out i will come here

or more likely: i have a theoretical question, not a specific one, on how to code not what to code, how to learn not to get you to do all my coding.

as always, my thanks to everyone for gracing me with their time and expertise.

Re: resource advice(book examples wont compile)

Posted: Mon Apr 02, 2012 3:02 pm
by THe Floating Brain
vegiestirfrypizza wrote:I'm using Microsoft visual c++ express(note:i understand its not the easiest compiler to go with but i need to get used to it because i may be getting a job soon that will demand it)
in responce to
My advice to you is to NEVER TRUST EXAMPLES. They usually contain pseudo and or untested code.
yeah i guess i really could have used that advice a while back
:-)
vegiestirfrypizza wrote: or more likely: i have a theoretical question, not a specific one, on how to code not what to code, how to learn not to get you to do all my coding.
Practice, practice, practice and try to get some constructive criticism.