Error message after declaring instance of class

I'm using Windows 7 Starter Edition and Code::Blocks. It gives me the error message at 'char text = "What is the meaning of...";'. I'm obviously doing something wrong, but I'm not sure what.

(FYI: this is just a prototype. I hope to be able to use this to quiz myself at some point- sort of like flashcards, except I learn a bit about programming while doing so and instead of writing them, I type them.)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <cstring>
using namespace std;


class question
{
    public:
    std::string text;
    std::string answer;
    std::string input;
    void show_question();
};

void question::show_question()
{
    cout << text;
    cin >> input;
    strcmp(input, answer)
    {
        cout << "That is incorrect. Try again?\n";
        cin >> input;
    }
    cout << "That is correct! Good job!\n\n";
}

int main()
{
    question history1;
    strcpy(history1.text, "What is the answer of Life, the Universe, and Everything?");
    strcpy(history1.answer, "42");
    history1.show_question();
    return 0;
}
Last edited on
If you want to access the 'text' member within history1, you need to use the dot operator, same goes for 'answer'.
1
2
3
4
5
question history1
{
    char text = "What is the answer of Life, the Universe, and Everything?";
    char answer = "42";
}


Yeah, that's interesting syntax though not C++. Also, you wouldn't be able to just assign "..." to text. You would be able to if text were a std::string.

This should do what you need:

1
2
3
question history1;
strcpy(history1.text, "What is the answer of Life, the Universe, and Everything?");
strcpy(history1.answer, "42");


I would also recommend limiting the use of public member variables.

Also, when you call show_question you need the brackets. Also also, if you're not returning a value from show_question() then you should declare it void
Last edited on
Okay, I did that. It's still not working though.

I need to declare the first few variables (text, answer) as strings then, don't I?
Last edited on
See my post above. ninja'd

This won't work either because it's pointer comparison (it would work if input and answer were std::strings):

while (input != answer)

You may use strcmp or strncmp to compare char arrays.

I need to declare the first few variables (text, answer) as strings then, don't I?

That'll make it easier though your problem is because you didn't use strcmp or strncmp for the above comparison.
Last edited on
Er, okay, but how do I do that?
strcmp(input, answer)

The above returns 0 if input and answer have the same contents (it stops comparing chars when it reaches the null character in one of the char arrays or if the chars differ). strcmp is in the <cstring> header.

Note that if answer will have spaces at any point then you'll have to use cin.getline
Last edited on
Source code's updated to its current state of affairs- but it still won't compile. The 'std::string' isn't highlighted, but it seems ok. The current error message is at strcmp(input, answer.
Now that you're using std::strings then you can proceed like you did before.

1
2
3
4
if(input != answer) //will work now
...
history1.text = "What is the answer of Life, the Universe, and Everything?"; //so will this
history1.answer = "42"; //and this 


BTW, you would have to do it like this for char arrays: if( strcmp(input, answer) )

The 'std::string' isn't highlighted, but it seems ok.

That doesn't get highlighted though you can probably tell your editor to do it.

Note you will have to use std::getline as opposed to cin>> if answer (as a string) will have spaces in it.
Last edited on
Awesome! It compiles!


...


But there's a horrible bug. I run it, it accepts one answer and says that's wrong, accepts another answer and says that's right and goodbye, regardless of what values I type into it. Any ideas?


And by std::getline do you mean this:
std::getline input;
?
That won't compile.
I run it, it accepts one answer and says that's wrong, accepts another answer and says that's right and goodbye, regardless of what values I type into it. Any ideas?

Can we see the updated code?

do you mean this: std::getline input; ?

No, I mean this:

1
2
3
std::string input;
std::cout << "Enter a string and add whitespace in it if you want." << std::endl;
std::getline(std::cin, input);
Last edited on
Topic archived. No new replies allowed.