Suffix Project Help

Im tryng to make a print the suffix that goes with the input. When i enter 12 or 11, It prints out the number with the correct suffix but then it will print two more lines with an additional suffix based on the last number. Im not sure what im doing wrong.

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


using namespace std;



int main() {

	
	int input;
	string suff = "";
	string x = "That would be the ";
	cout << "Enter a value" << endl;
	cin >> input;

	 if (input % 100 == 11 || input % 100 == 12 || input % 100 == 13) {
		suff += "th";
		cout << x << "" << input << suff << endl;
	}

	if (input % 10 == 1) {
		suff += "st";
		cout << x << "" << input<<suff << endl;
	}
		
	else if (input % 10 == 2) {
		suff += "nd";
		cout << x << "" << input<<suff << endl;
	}
		
	else if (input % 10 == 3) {
		suff += "rd";
		cout << x << "" << input<<suff << endl;
	}

	
	else
		suff += "th";
	cout << x << "" << input<<suff << endl; 
	
	system("pause");
	return 0;
}
Line 24: change if to else if

The final else should have curly braces around the following two lines. Otherwise, only line 41 is considered part of the else, and line 42 is always executed regardless.
For all n such that n mod 100 = 11, n mod 10 = 1. Although there's only one correct suffix, both if statements will run. You need an else on line 24.

Adding "th" as a suffix is required for every integer whose magnitude is greater than 3, as well as zero. Maybe you could do something like
1
2
3
4
5
int magn = std::abs(input);
if (magn == 1) suff = "st"; 
if (magn == 2) suff = "nd";
if (magn == 3) suff = "rd"; 
else suff = "th";
That worked. Thanks
Topic archived. No new replies allowed.