multiple choice program

what is the initializer i need? the error is in the line on top of the "while(1)" line. I looked online and it looked like all they did was add the int in front of QuestionNumber.

I also have a link to my code, there were 2 other errors i had. i'm new to c++
http://cpp.sh/5e6gf
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
35
36
37
38
39
40
41
42
43
44
  
#include <iostream>
#include <string>

int main()
{
   std::string Responce;
   std::string A;
   std::string B;
   std::string C;
   std::string D;
   std::string QuestionNumber;
   std::string Name
      int QuestionNumber = 1;
While (1)
  while (QuestionNumber=1){
                std::Cout <<"what element is your favorite?" ;
std::cout << "A. Fire" "\N" ;
std::cout <<"B Ice \N" ;
std::cout << "C. Water" << "\N" ;
std::cout << "D.  Wind" "\N" ;
getline(std::cin,Response);
                
while(Response="A"){
std::Cout << "So, you're a fire weilder then? huh, took you for more a wind one. >> oh how rude of me, what was your name again?" ;
}
while(Response="B"){
std::cout <<  "Ice? really? huh, i guess you deserve the cold shoulder then. Kidding! what is your name anyways?" ;
}
while(Response= "C"){
std::cout << "water? what are you waiting for, tell me your name" ;
}
while(Response= "D"){
std::cout << "wind? you must be the boring one then. no? okay, fine, what is your name then?" ;
}
 }
return 0
  };
  
  
  
  
  
Last edited on
I edited my code, now my error is at "int main"

what is this initializer i need? I made question number, and the a, b, c, d global vareables, so they could be reused throughout the program thats why they're before int main.

http://cpp.sh/4bcge

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
35
36
37
38
#include <iostream>
#include <string>

std::string Responce;
   std::string A;
   std::string B;
   std::string C;
   std::string D;
   std::string QuestionNumber;
   std::string Name
       

int main(){
   
 while (1)
 QuestionNumber = 1;
  while (QuestionNumber=1){
                std::cout <<"what element is your favorite?" ;
std::cout << "A. Fire"  ;
std::cout <<"B Ice" ;
std::cout << "C. Water"  ;
std::cout << "D.  Wind"  ;
getline(std::cin,Response);
                
while(Response="A"){
std::cout << "So, you're a fire weilder then? huh, took you for more a wind one. >> oh how rude of me, what was your name again?" ;
}
while(Response="B"){
std::cout <<  "Ice? really? huh, i guess you deserve the cold shoulder then. Kidding! what is your name anyways?" ;
}
while(Response= "C"){
std::cout << "water? what are you waiting for, tell me your name" ;
}
while(Response= "D"){
std::cout << "wind? you must be the boring one then. no? okay, fine, what is your name then?" ;
}
 }
  };






You are missing a semicolon after the declaration std::string Name

Possess yourself of patience in awaiting replies: the variable definitions were better in the first program.

None of your while () statements will work as you think:
= means assignment
== is a test for equality (which is what you need).
There should be a semicolon at the end of the declaration on line 10.
std::string Name ;

To do something once if x is equal to y, use an if statement with == instead of a while with =
1
2
// while(Response="A"){
if( Response == "A" ) {
etc.

Try this:
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
35
36
37
38
39
40
41
42
#include <iostream>

int main() {

    bool keep_looping = true ;
    while(keep_looping) {

         std::cout << "\n\nwhat element is your favourite?\n"
                   << "A. Fire\nB. Ice\nC. Water\nD. Wind\nQ. Quit\n? "  ;
         char response ;
         std::cin >> response ;

         switch(response) {

             case 'A': case 'a':
                 std::cout << "So, you're a fire wielder then? huh, took you for more a "
                              "wind one. >> oh how rude of me, what was your name again?\n" ;
                 break ;

             case 'B': case 'b':
                 std::cout <<  "Ice? really? huh, i guess you deserve the cold shoulder "
                               "then. Kidding! what is your name anyway?\n" ;
                 break ;

             case 'C': case 'c':
                 std::cout << "water? what are you waiting for, tell me your name\n" ;
                 break ;

             case 'D': case 'd':
                 std::cout << "wind? you must be the boring one then. no? "
                              "okay, fine, what is your name then?\n" ;
                 break ;

             case 'Q' : case 'q':
                keep_looping = false ;
                break ;

             default:
                std::cout << "invalid input. try again\n" ;
         }
    }
}


what is case? and whats Q?
Case is a keyword used in a switch statement it means a case

lets say if you use an if statement

1
2
3
4
 if(a == b){
  // do something ..

}


lets say that both are and b are the same number then this if statement gets executed,this is the same in a switch statement except you use the keyword case,so lets say you pass 2 to the switch statement,you can use different cases instead of creating multiple and messy looking if or else if statements,

Q is just the test case like a or b in the if statement

hope that helps not sure if that answered it.

Last edited on
Try this:

there is a function that outputs the question and receives the answer (void question)
there is a function that determines the results (void results)
and a function that outputs if the question number is not one(void error)

the parameters for void question() and void results() were the answer that the user chose, the ampersand is pass by reference and it allows the character to be modified in the function

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <string>

void question(char& ans);
void results(char& ans);
void error();

int main()
{
    char answer;
    int questionNum = 1;

    questionNum == 1 ? question(answer) : error();
}

void question(char& ans)
{
    std::cout << "What element is your favorite?\n";
    std::cout << "A. Fire\n"
              << "B. Ice \n"
              << "C. Water \n"
              << "D.  Wind\n";
    std::cin >> ans;
    results(ans);
}
void results(char& ans)
{
    switch (ans) {
    case 'A':
    case 'a':
        std::cout << "So, you're a fire wielder then? huh, took you for more a "
                     "wind one. oh how rude of me, what was your name again?\n";
        break;

    case 'B':
    case 'b':
        std::cout << "Ice? really? huh, i guess you deserve the cold shoulder "
                     "then. Kidding! what is your name anyway?\n";
        break;

    case 'C':
    case 'c':
        std::cout << "water? what are you waiting for, tell me your name\n";
        break;

    case 'D':
    case 'd':
        std::cout << "wind? you must be the boring one then. no? okay, fine, what is your name then?\n";
        break;
    }
}
void error()
{
    std::cout << "Question number is not 1";
}
Topic archived. No new replies allowed.