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.)
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
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
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.
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);