How do I put in a yes or no input statement?

How do I put in a yes or no input statement? Yes for the program to run and no for the program to stop. I also need a yes or no input at the end of the program as well but I still to be getting errors such as "Expected instalizers before 'swtich' on the switch statement. Basically I am trying to prompt the user two times, first before the program executes the main code and after the first iteration of code asking the user if he would like to run the program again.
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int LettertoNumber (char);

int main()
{
cout <<"-------------------------------------------------------" <<endl;
cout <<"Ani Biswas CSC133-01 Lab02.cpp 09/27/16" <<endl;
cout <<"-------------------------------------------------------" <<endl;

//Declaration of variables
char PhoneNumber[11];
//Beginning of main program
cout<<"This program converts letters to their corresponding telephone digits" <<endl;
{
string input;
cout<<"Input y to start the program and input n to end the program" <<endl;
cin>> input;
if(input.compare("y"))
{
cout<<"Enter a phone number: " <<endl;
cin.getline( PhoneNumber, 11, '/n' ); //Similar to void. cin.getline or the getline member function. Here I am trying to get the compiler to input integers on the line and then remove the terminating characters

cout<<"Phone Number before conversion: " << endl;
cout << PhoneNumber << endl;

for(int i = 0; i < 11; i++)
{
PhoneNumber[i] = LettertoNumber(PhoneNumber[i]);
}

cout<<"Phone Number after conversion: "<< endl;
cout << PhoneNumber << endl;
cout<<"****************************************" <<endl;
}
else if(input.compare("n"))
{
cout<<"End of program"<<endl;
return 0;
}

int LettertoNumber(char letter)

switch (letter)
{
case 'A':
case 'B':
case 'C':
case 'a':
case 'b':
case 'c':
return '2';
break;
case 'D':
case 'E':
case 'F':
case 'd':
case 'e':
case 'f':
return '3';
break;
case 'G':
case 'H':
case 'I':
case 'g':
case 'h':
case 'i':
return '4';
break;
case 'J':
case 'K':
case 'L':
case 'j':
case 'k':
case 'l':
return '5';
break;
case 'M':
case 'N':
case 'O':
case 'm':
case 'n':
case 'o':
return '6';
break;
case 'P':
case 'Q':
case 'R':
case 'S':
case 'p':
case 'q':
case 'r':
case 's':
return '7';
break;
case 'T':
case 'U':
case 'V':
case 't':
case 'u':
case 'v':
return '8';
break;
case 'W':
case 'X':
case 'Y':
case 'Z':
case 'w':
case 'x':
case 'y':
case 'z':
return '9';
break;
default:
return letter; // in case it is not a letter or is already a number
break;
}

}
}
You have not explained well I think , or I am not understanding you maybe.
Do you mean :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
char x = 'n' ;

while(x == 'n') 
{
      .
      .
      .
      .

      cout << "Do you want to finish program (y or n):";
      cin >> x;
      cout << endl;

      if(x == 'y') {
        x == 'y';
      }

}
Last edited on
Asking yes or no before and after iterations:

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
#include <iostream>
#include <cctype>

char prompt()
{
        std::cout << "Do you want to run program again (y or n): " << std::flush;

        char prog_opt;

        // error checking

        while ( !(std::cin >> prog_opt) ||
                (std::tolower(prog_opt) != 'y' && std::tolower(prog_opt) != 'n') )
        {
                std::cin.clear();
                std::cin.ignore(1000000, '\n');
                std::cout << "y or n: " << std::flush;
        }

        return prog_opt;
}

int main()
{
        char prog_opt = prompt();

        while (prog_opt != 'n')
        {
                // code here

                prog_opt = prompt();
        }

        return 0;
}
Last edited on
Topic archived. No new replies allowed.