Need help with my command line quiz?

I have made a quiz:
1
2
3
4
5
6
7
8
9
10
11
12
13
// c++ quiz for beginners
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main ()
{
char string guess;
do {
cout << "What character is at the end of most lines, including declarations?";
cin >> guess;
if (guess = ";") puts ("Hurrah! Question 2:");
} while cout << ("Sorry! Try again.");

Resulting in many errors, many due to the string variable I declared. I have been reading c++ for dummies 6th edition and they mention a char string, but apparently codeblocks doesn't recognise it. What I think is happening is that code:blocks is declaring the variable string as type char, then does not recognise guess. Here are my bugs:
1
2
3
4
5
6
7
8
K:\CodeBlocks\Projects\C++ Quiz for beginners\main.cpp:8: error: expected initializer before 'guess'
K:\CodeBlocks\Projects\C++ Quiz for beginners\main.cpp:10: error: 'cout' was not declared in this scope
K:\CodeBlocks\Projects\C++ Quiz for beginners\main.cpp:11: error: 'cin' was not declared in this scope
K:\CodeBlocks\Projects\C++ Quiz for beginners\main.cpp:11: error: 'guess' was not declared in this scope
K:\CodeBlocks\Projects\C++ Quiz for beginners\main.cpp:13: error: expected '(' before 'cout'
K:\CodeBlocks\Projects\C++ Quiz for beginners\main.cpp:13: error: 'cout' was not declared in this scope
K:\CodeBlocks\Projects\C++ Quiz for beginners\main.cpp:13: error: expected ')' before ';' token
K:\CodeBlocks\Projects\C++ Quiz for beginners\main.cpp:13: error: expected '}' at end of input

Also, how would I go about having a second question?
Sorry for all the questions and seeming stupid, I only started c++(my first language) a few days ago.
You need to #include <iostream> in order to use cout, cin
They live in the std namespace, so you need to qualify them, like std::cout,
or send them to the global namespace with using std::cout or using namespace std;

As for char string guess; ¿what do you want there?
For just one character you use char guess; for a string std::string guess; (you'll need to #include <string>

The condition in the do-while must be surrounded by parenthesis.
while( condition )

To add a second question, you must first break from the first loop

Please indent your code, or better let the IDE do it for you.
closed account (o3hC5Di1)
Hi there,

That are indeed many errors, here's how to fix them:

- add #include <iostream> to the #include 's
- put using namespace std; under the #include 's
- change line 8 to char guess;
- change line 13 to while( expression );
- add a closing } to the end of the program

That will not make it work, but it will allow you to reassess how your program functions at least.

Hope that helps.

All the best,

NwN

Edit: Sorry for doublepost - need to learn to type faster :)
Last edited on
Thank-you sirs!
Now I have only one error :P
It returns an error, saying that the constant char(the guess) is a constant so cannot be changed. I havn't said that it was a constant though, as you can see from here:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// c++ quiz for beginners
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <string>

using namespace std;
int main ()
{
char guess1;
do {
cout << "What character is at the end of most lines, including declarations?";
cin >> guess1;
if (guess1 = ";") puts ("Hurrah! Question 2:");
} while (cout << ("Sorry! Try again."));
}

In response to ne555, I guess this answer can be just a char, but I will need a string for most of them.
Also, is it possible to have options(ie. a, b and c) shown and have the user choose from those(by entering a, b or c)?
closed account (o3hC5Di1)
1
2
//line 15
if (guess1 = ";")


A single "=" is used to assign values, so this would indeed try to change the value.
To compare data you need to use the equality operator "==":

 
if (guess1 == ';')


Hope that helps.

All the best,
NwN
Last edited on
Ahh! So many rules! :P
Thanks :)
I came up with an idea for having a b or c options, I'll try to write that now :)
Okay. Here is my revised code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

// c++ quiz for beginners
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <string>

using namespace std;
int main ()
{
char guess1;
do {
cout << "What character is at the end of most lines, including declarations?";
cout << "\na. ;\nb. :\nc. '\n";
cin >> guess1;
if (guess1 == ';' or 'a') puts ("Hurrah! ; is a character used at the end of most lines.\nQuestion 2:");
} while (cout << ("Sorry! Try again."));
}

Some bugs I have(not errors showing in CodeBlocks when building):
Any answer is accepted as right.
Once you have answered right, it informs you you are right. Then it informs you that you are wrong, and invites you to do the question again.
Sorry for all these, but I have no experience, and have branched off on my own little project outside of the book I am reading on c++, so it cannot guide me.
closed account (o3hC5Di1)
Hi there,

Think about the "while" condition for a minute, what you are telling the program is:

"While you print "sorry, try again", pose a question".

So the program is doing what you are asking it to do.

So, try and think of a better condition and let us know what you come up with.

Sorry for not just giving you the answer, but then you're not forced to think about your mistakes and find better ways.

All the best,
NwN
That is why I am reluctant to ask :P
Oh! I realised what you just said about what I am asking it to do.
I came up with else?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

// c++ quiz for beginners
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <string>

using namespace std;
int main ()
{
char guess1;
do {
cout << "What character is at the end of most lines, including declarations?";
cout << "\na. ;\nb. :\nc. '\n";
cin >> guess1;
if (guess1 == ';' or 'a') puts ("Hurrah! ; is a character used at the end of most lines.\nQuestion 2:");
else (cout << ("Sorry! Try again."));
} while (cout << (""));
}

The empty while is just because I kept getting errors when trying to compile that while was needed.
I have tried to come up with a solution to the always-being-right program but have concluded with no fix. :/
Okay. So after else and the cout do I need some kind of command to go to the start of the question?
And after getting the question right(line 17) is there a command to go to another part of the code(ie, the next question)?
closed account (o3hC5Di1)
Hi there,

getting there! :)

What you could do for the do/while loop is just leave it out (but if you're on windows your console will close) or you could declare a variable like so:

1
2
3
4
5
6
7
8
9
10
11
12
int main ()
{
char guess1;
bool endgame = false;
do {
cout << "What character is at the end of most lines, including declarations?";
cout << "\na. ;\nb. :\nc. '\n";
cin >> guess1;
if (guess1 == ';' or 'a') puts ("Hurrah! ; is a character used at the end of most lines.\nQuestion 2:");
else {cout << ("Sorry! Try again."));}
} while (endgame == false);
}



When you want the game to end, you just do endgame = true;.
That's just an example off course, there are other ways.

Hope that helps,
All the best,
NwN
Okay. I have done some more.
But yest again I have reached an error I cannot comprehend :/

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
// c++ quiz for beginners
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <string>

using namespace std;
int main ()
{
char guess1, guess2;
bool endgame = false, a1 = false, a2 = false;
// a1 meaning if question 1 has been answered correctly.
do {
cout << "What character is at the end of most lines, including declarations?";
cout << "\na. ;\nb. :\nc. '\n";
cin >> guess1;
if (guess1 == ';' or 'a') puts ("Hurrah! ; is a character used at the end of most lines.\nQuestion 2:") and (a1 = true);
else (cout << ("Sorry! Try again."));
} while (endgame = false);
}

do {
if (a1 == false)
else (cout << "What does int stand for?\na. Introducing \nb. Internal \nc. Integer")
else (cin >> guess2)
do {
if (guess2 = "c" or "Integer") puts(cout << "Hurrah! int does stand for integer, and it is the term\nyou use when declaring one.")
} 


Here is my error:
K:\CodeBlocks\Projects\C++ Quiz for beginners\main.cpp:23: error: expected unqualified-id before 'do'
Last edited on
closed account (o3hC5Di1)
Hi there,

You have taken the bottom do/while loop out of your main() function by closing the main() function with a '}' on line 21.

Any code outside of any function (main or otherwise) or structure is considered to be "in limbo" by the compiler - it doesn't know when to call it and what to do with it.

I think you are misunderstanding the use of do/while here.
We are using do{} while (endgame == false) in order for your game to keep running without exiting on a wrong answer.

What you could do is nesting the do/while loops for every question, as such:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
do {
    //question one
    do {
        cout << "question 1"; 
        cin << guess;
    } while (guess != 'answer');

    //question two
    do {
        cout << "question 2"; 
        cin << guess;
    } while (guess != 'answer');

    endgame = true;
} while (endgame == false);


In a looping structure, like do/while, the program will continue in the loop until the while expression is fulfilled, after which it just continues to the next instruction in your program. So in my example, the first do/while keeps going until endgame == true, after which there is no more instruction and the program exits. The second do/while loop will keep asking the question until the right answer is given, once it is given, it continues to the next instruction, here another do/while loop.

Hope that makes sense to you.

Small kindly remark, I noticed that you posted another topic on this, please be patient with us as we are with you.

All the best,
NwN
But still, whatever is answered, it asks the question again :/
closed account (o3hC5Di1)
Could you please post your full current code again?

Thanks,

NwN
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
 // c++ quiz for beginners
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <string>

using namespace std;
int main ()
{
char guess;
bool endgame = false, a1 = false, a2 = false;
do {
    //question one
    do {
        cout << "Select the format which is NOT a c++ source format.\na. .yml\nb. .cpp\nc. .h\n";
        cin >> guess;
        if (guess != 'a' or '.yml');
       } while (endgame == false);

    //question two
    do {
        cout << "question 2";
        cin >> guess;
    } while (guess != 'answer');

    endgame = true;
} while (endgame == false);
}

I'm rubbish at this :/
Sorry I'm late :3
closed account (o3hC5Di1)
Hi there,

1
2
3
4
5
6
7
//line 14
//question one
    do {
        cout << "Select the format which is NOT a c++ source format.\na. .yml\nb. .cpp\nc. .h\n";
        cin >> guess;
        if (guess != 'a' or '.yml');
       } while (endgame == false);


You have an if statement there if (guess != 'a' or || '.yml'); , but you haven't told the program what to do if the condition is true. It's like telling the program: "If the answer is a or .yml, nothing happens".

You'll want to do something like this:

1
2
3
4
5
6
do {
        cout << "Select the format which is NOT a c++ source format.\na. .yml\nb. .cpp\nc. .h\n";
        cin >> guess;
        if (guess != 'a' || '.yml') a1 = true; //<-- telling what to do
       } while (a1 == false);
 //question 1, so use a1, or the game ends after the first question. 


Hope that helps.

All the best,
NwN
Last edited on
Topic archived. No new replies allowed.