resource advice(book examples wont compile)

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
vegiestirfrypizza
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 5
Joined: Wed Nov 30, 2011 12:27 pm
Current Project: learning basics of c++
Favorite Gaming Platforms: famicom
Programming Language of Choice: C++
Location: iowa city

resource advice(book examples wont compile)

Post 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.
the morning is good for algorithms and tough thinking, the night is for things that don't require such forethought.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: resource advice(book examples wont compile)

Post by dandymcgee »

vegiestirfrypizza wrote:it seems that every one i try the examples wont compile including this one
What compile errors are you getting?
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
vegiestirfrypizza
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 5
Joined: Wed Nov 30, 2011 12:27 pm
Current Project: learning basics of c++
Favorite Gaming Platforms: famicom
Programming Language of Choice: C++
Location: iowa city

Re: resource advice(book examples wont compile)

Post 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.
the morning is good for algorithms and tough thinking, the night is for things that don't require such forethought.
tappatekie
Chaos Rift Junior
Chaos Rift Junior
Posts: 204
Joined: Mon Nov 21, 2011 3:01 pm
Current Project: Web browser from scratch
Favorite Gaming Platforms: SNES, PSP, PS1 and 3
Programming Language of Choice: C#
Location: A house near me
Contact:

Re: resource advice(book examples wont compile)

Post 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
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: resource advice(book examples wont compile)

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
THe Floating Brain
Chaos Rift Junior
Chaos Rift Junior
Posts: 284
Joined: Tue Dec 28, 2010 7:22 pm
Current Project: RTS possible Third Person shooter engine.
Favorite Gaming Platforms: PC, Wii, Xbox 360, GAME CUBE!!!!!!!!!!!!!!!!!!!!!!
Programming Language of Choice: C/C++, Python 3, C#
Location: U.S

Re: resource advice(book examples wont compile)

Post 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.
"Why did we say we were going to say we were going to change the world tomorrow yesterday? Maybe you can." - Myself

ImageImage
User avatar
wtetzner
Chaos Rift Regular
Chaos Rift Regular
Posts: 159
Joined: Wed Feb 18, 2009 6:43 pm
Current Project: waterbear, GBA game + editor
Favorite Gaming Platforms: Game Boy Advance
Programming Language of Choice: OCaml
Location: TX
Contact:

Re: resource advice(book examples wont compile)

Post 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.
The novice realizes that the difference between code and data is trivial. The expert realizes that all code is data. And the true master realizes that all data is code.
User avatar
vegiestirfrypizza
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 5
Joined: Wed Nov 30, 2011 12:27 pm
Current Project: learning basics of c++
Favorite Gaming Platforms: famicom
Programming Language of Choice: C++
Location: iowa city

Re: resource advice(book examples wont compile)

Post 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.
the morning is good for algorithms and tough thinking, the night is for things that don't require such forethought.
User avatar
THe Floating Brain
Chaos Rift Junior
Chaos Rift Junior
Posts: 284
Joined: Tue Dec 28, 2010 7:22 pm
Current Project: RTS possible Third Person shooter engine.
Favorite Gaming Platforms: PC, Wii, Xbox 360, GAME CUBE!!!!!!!!!!!!!!!!!!!!!!
Programming Language of Choice: C/C++, Python 3, C#
Location: U.S

Re: resource advice(book examples wont compile)

Post 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.
"Why did we say we were going to say we were going to change the world tomorrow yesterday? Maybe you can." - Myself

ImageImage
Post Reply