how to prompt the user to try again

i want the user to instead to exit it loops back to try again when asked either p/f when he pressed the wrong option

#include <iostream>
using namespace std;

int main (){
string n;
double pr,hr,ot,grspy,ans,ott;
char t;

cout << "Enter your Name:";
getline(cin,n);
cout << "Are you a Part-time or Fulltime employee? [P/F]";
cin >> t;

if (t == 'P' || t == 'p'){
cout << "---Part-Time---\n";
cout << "Enter your Rate Per Hour:";
cin >> pr;
cout << "Enter the Hours you've worked:";
cin >> hr;
cout << "Enter your hours of overtime:";
cin >> ot;
cout <<""<< endl;

ans = pr * hr;
ott = ot * pr * 1.25;
grspy = ans + ott;

cout << "Employee Name:" << n << endl;
cout << "Basic Pay:" << ans << endl;
cout << "Overtime Pay:" << ott << endl;
cout << "--------------------------------" << endl;
cout << "Gross Pay:" << grspy << endl;
}else if (t == 'F' || t == 'f'){
cout << "---Fulltime---\n";
cout << "Enter your basic pay:";
cin >> grspy;
cout <<""<<endl;
cout <<""<<endl;
cout << "Employee Name:" << n << endl;
cout << "Basic Pay:" << grspy << endl;
cout << "--------------------------------" << endl;
cout << "Gross Pay:" << grspy << endl;
}else{
cout << "Data Invalid! Please try again.";

}
}








Last edited on
Have you had functions yet? They make it easier to keep the program in "neat pieces".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// work() asks details and reports the pays
void work( const string& name, char type );

int main ()
{
  string n;
  cout << "Enter your Name:";
  getline(cin,n);

  while ( true ) {
    cout << "Are you a Part-time or Fulltime employee? [P/F]";
    char t;
    cin >> t;
    work( n, t );
  }
}

Note though that this program repeats forever, which probably is not a good thing.
typically you validate user input asap after getting it, rather than do a bunch of stuff and then circle back and start over (this will annoy the user!).

roughly, then, consider (pseudocode)

do
cout enter p/f
cin t
t = toupper(t);
while(t != 'F' && t != 'P')



When posting code, please use code tags so that the code is readable!


[code]
formatted code goes here
[/code]


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
#include <iostream>
using namespace std;

int main() {
	string n;
	double pr, hr, ot, grspy, ans, ott;
	char t;

	cout << "Enter your Name:";
	getline(cin, n);
	cout << "Are you a Part-time or Fulltime employee? [P/F]";
	cin >> t;

	if (t == 'P' || t == 'p') {
		cout << "---Part-Time---\n";
		cout << "Enter your Rate Per Hour:";
		cin >> pr;
		cout << "Enter the Hours you've worked:";
		cin >> hr;
		cout << "Enter your hours of overtime:";
		cin >> ot;
		cout << "" << endl;

		ans = pr * hr;
		ott = ot * pr * 1.25;
		grspy = ans + ott;

		cout << "Employee Name:" << n << endl;
		cout << "Basic Pay:" << ans << endl;
		cout << "Overtime Pay:" << ott << endl;
		cout << "--------------------------------" << endl;
		cout << "Gross Pay:" << grspy << endl;
	} else if (t == 'F' || t == 'f') {
		cout << "---Fulltime---\n";
		cout << "Enter your basic pay:";
		cin >> grspy;
		cout << "" << endl;
		cout << "" << endl;
		cout << "Employee Name:" << n << endl;
		cout << "Basic Pay:" << grspy << endl;
		cout << "--------------------------------" << endl;
		cout << "Gross Pay:" << grspy << endl;
	} else {
		cout << "Data Invalid! Please try again.";
	}
}

Possibly something like:

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

int main() {
	std::string n;
	char t {};
	bool good {};

	std::cout << "Enter your Name:";
	std::getline(std::cin, n);

	while (!good) {
		good = true;
		std::cout << "Are you a Part-time or Fulltime employee? [P/F] ";
		std::cin >> t;

		if (t == 'P' || t == 'p') {
			double pr {}, hr {}, ot {};

			std::cout << "---Part-Time---\n";
			std::cout << "Enter your Rate Per Hour:";
			std::cin >> pr;
			std::cout << "Enter the Hours you've worked:";
			std::cin >> hr;
			std::cout << "Enter your hours of overtime:";
			std::cin >> ot;
			std::cout << '\n';

			const auto ans {pr * hr};
			const auto ott {ot * pr * 1.25};
			const auto grspy {ans + ott};

			std::cout << "Employee Name:" << n << '\n';
			std::cout << "Basic Pay:" << ans << '\n';
			std::cout << "Overtime Pay:" << ott << '\n';
			std::cout << "--------------------------------\n";
			std::cout << "Gross Pay:" << grspy << '\n';
		} else if (t == 'F' || t == 'f') {
			double grspy {};

			std::cout << "---Fulltime---\n";
			std::cout << "Enter your basic pay:";
			std::cin >> grspy;
			std::cout << "\n\nEmployee Name:" << n << '\n';
			std::cout << "Basic Pay:" << grspy << '\n';
			std::cout << "--------------------------------\n";
			std::cout << "Gross Pay:" << grspy << '\n';
		} else {
			std::cout << "Data Invalid! Please try again.\n";
			good = false;
		}
	}
}

Topic archived. No new replies allowed.