I need help on my program. How do I make it so that it prompts me again if I wan
ID: 3536338 • Letter: I
Question
I need help on my program. How do I make it so that it prompts me again if I want to do it again? It just ends.
Adding to my code would be much appreciated.
1 #include <iostream>
2 #include <string>
3 #include <cctype>
4 #include <vector>
5
6 using namespace std;
7 using std::cin;
8 using std::cout;
9 using std::endl;
10
11 bool palindrome(const string&);
12
13 int main( int argc, char *argv[])
14 {
15 cout << "Do you want to reverse a string, check if it's a palindrome or quit? ";
16 cout << "(Press 1 for reverse, 2 for palindrome, and anything else to quit) ";
17 int pickfunc;
18 cin >> pickfunc;
19 cin.ignore( 1, ' ' );
20
21 if (pickfunc == 2) {
22
23 string line;
24
25 cout <<" Enter word: ";
26 getline(cin, line);
27 cout<< line << " is ";
28
29 if (palindrome(line) == false) cout << "not ";
30 cout << "a palindrome." << endl;
31 return 0;
32 }
33
34 if (pickfunc == 1) {
35 string words;
36
37 cout << "Enter words : ";
38 getline( cin, words);
39
40 string reverse_string( words.rbegin() , words.rend() );
41 cout << " Reversed, it is: " << reverse_string << endl << endl;
42 }
43 }
44
45 bool palindrome( const string &s) {
46 bool pal = true;
47 const char *p = s.c_str();
48 const char *q = p + s.size() -1;
Explanation / Answer
1 #include <iostream>
2 #include <string>
3 #include <cctype>
4 #include <vector>
5
6 using namespace std;
7 using std::cin;
8 using std::cout;
9 using std::endl;
10
11 bool palindrome(const string&);
12
13 int main( int argc, char *argv[])
14 {
int pickfunc = 1;
while(pickfunc==1 || pickfunc ==2)
{
15 cout << "Do you want to reverse a string, check if it's a palindrome or quit? ";
16 cout << "(Press 1 for reverse, 2 for palindrome, and anything else to quit) ";
17
18 cin >> pickfunc;
19 cin.ignore( 1, ' ' );
20
21 if (pickfunc == 2) {
22
23 string line;
24
25 cout <<" Enter word: ";
26 getline(cin, line);
27 cout<< line << " is ";
28
29 if (palindrome(line) == false) cout << "not ";
30 cout << "a palindrome." << endl;
31 return 0;
32 }
33
34 if (pickfunc == 1) {
35 string words;
36
37 cout << "Enter words : ";
38 getline( cin, words);
39
40 string reverse_string( words.rbegin() , words.rend() );
41 cout << " Reversed, it is: " << reverse_string << endl << endl;
42 }
}
43 }
44
45 bool palindrome( const string &s) {
46 bool pal = true;
47 const char *p = s.c_str();
48 const char *q = p + s.size() -1;
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.