Shortening code

Hello may I ask how to call out functions instead of copy-pasting, specifically this rating system below?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  if (rate == 1 || rate == 2) {

	cout << "Ooh. That's too bad. Can you give us some feedback that could help this program?\nAnswer: ";
	cin >> feedback;
	cout << "Thank you so much!\n";

}
	else if (rate == 3 || rate == 4 || rate == 5) {
	
cout << "Wow! That's great. Can you give us some feedback that could help this program?\nAnswer: ";
cin >> feedback;
cout << "Thank you so much!\n";

						}
else if (rate > 5) {

	cout << "Oh! You're too kind! Can you give us some feedback that could help this program?\nAnswer: ";
        cin >> feedback;
	cout << "Thank you so much!\n";
						}


Thanks in advance
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
#include <iostream>
#include <string>

const std::string prompt = "Can you give us some feedback that could help this program?\nAnswer: ";

const std::string specifics[]{{"Ooh. That's too bad. "}, {"Wow! That's great. "}, {"Oh! You're too kind! "}};
int main()
{
    int rate{};

    std::cout << "Enter rate \n";
    std::cin >> rate;//usual input validation checks, etc

    if (rate <= 2)
    {
        std::cout << specifics[0] + prompt;
    }
    else if (rate <= 5)
    {
        std::cout << specifics[1] + prompt;
    }
    else if (rate > 5)
    {
        std::cout << specifics [2] + prompt;
    }//could have a default option to catch incorrect entries and a loop to run until correct entry
    int feedback{};
    std::cin >> feedback;
    std::cout << "Thank you so much!\n";

}
Last edited on
Topic archived. No new replies allowed.