fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. int main () {
  6. int yourPlace;
  7. int lastPlace;
  8. cout << "Your place: ";
  9. cin >> yourPlace;
  10. cout << endl << "Final place: ";
  11. cin >> lastPlace;
  12. cout << endl;
  13.  
  14. for (int i = 1; i <= lastPlace; i++) {
  15. if (i == yourPlace)
  16. cout << ""; // Had issues with break; but this works (not well)
  17. else if (i%100 == 11 || i%100 == 12 || i%100 == 13)
  18. cout << i << "th";
  19. else if (i%10 == 1)
  20. cout << i << "st";
  21. else if (i%10 == 2)
  22. cout << i << "nd";
  23. else if (i%10 == 3)
  24. cout << i << "rd";
  25. else
  26. cout << i << "th";
  27. if (i == yourPlace)
  28. cout << ""; // Had issues with break; but this works (not well)
  29. else if (i == lastPlace || (lastPlace-1 == i && yourPlace == lastPlace))
  30. cout << ".";
  31. else if (i == lastPlace - 1)
  32. cout << " and ";
  33. else
  34. cout << ", ";
  35. }
  36. }
Success #stdin #stdout 0s 3416KB
stdin
5
20
stdout
Your place: 
Final place: 
1st, 2nd, 3rd, 4th, 6th, 7th, 8th, 9th, 10th, 11th, 12th, 13th, 14th, 15th, 16th, 17th, 18th, 19th and 20th.