Looking for help on how to write a code...

Hello,
I am fairly new to programming and need help with a code.
I need to create a code that can add a suffix to a number.

Example:
Enter a value: 1

That would be the 1st.

Test another value? Yes

Enter a value: 11

I'd have to say the 11th


NOTE* - I am not asking that you write the whole code for me but how would I get started on this?

Your help is appreciated.
Thank you
Here is a starting point for a very simple way to go about this problem. Read in the input, and take the appropriate action for the input value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int main()
{
  int number = 0;
  cout << "Enter a value: ";
  cin >> number;

  if( number == 1)
  {
    cout << "1st" << endl;
  }
  // else if ( some other number)
  // so on and so forth for other numbers

  return 0;
}


This doesn't handle looping; but if you can handle a single run, you can then add the loop later.
Hello, I think you are lookinh for something like this, yes? -I hope I helped :)

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

using namespace std;


int main(){

cout << "Enter a number: ";
int enter;
cin >> enter;

if(enter==1){
	cout << enter << "st" << endl;
}
if(enter==2){
	cout << enter << "nd" << endl;
}
if(enter==3){
	cout << enter << "rd" << endl;
}
if(enter>3){
	cout << enter << "th" << endl;
}
else{
	cout << "Invalid enter!" << endl;
}



return 0;
}
@ MheonYaE

You didn't take into account that 21 is called 21st, 22 22nd, 23 23rd, 31 31st etc.

This should work correctly for every number:

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

int main(){
    bool running = true;
    while(running){
        int number, test, digits=0;
        double grej;
        cout << "Enter a number: "; cin >> number;
        test = number;
        while(test>=1){
            test/=10;
            digits++;
        }
        test = number;
        if(digits>2){
            grej = floor(test/pow(10,digits-2));
            test -= grej*pow(10,digits-2);
        }
        cout << number;
        if((number-3)%10==0 && test != 13)
            cout << "rd" << endl;
        else if((number-2)%10==0 && test != 12)
            cout << "nd" << endl;
        else if((number-1)%10==0 && test != 11)
            cout << "st" << endl;
        else
            cout << "th" << endl;
        cin.ignore();
        cin.get();
    }
}
Last edited on
Ye, but he said it had to say: 11th, thats why I did it like I did it... I know its grammatically wrong but thats what he requested.
This maybe is a little bit off topic, but 11th isn't grammatically incorrect, however 21th or 131th is, at least that is what I was taught.
Last edited on
Thank you so much for replying!

MheonYaE: Yes, what you have provides a very good outline of what I was looking for.

Btw: To make things a bit clear, there is a pattern for suffixes (suffices?)
Here:

1st 2nd 3rd 4th 5th ... 10th
11th 12th 13th 14th 15th ... 20th
21st 22nd 23rd 24th 25th ... 30th
31st 32nd 33rd 34th 35th ... 40th
...
101st 102nd 103rd 104th 105th ... 110th
111th 112th 113th 114th 115th ... 120th
121st 122nd 123rd 124th 125th ... 130th
...
201st 202nd 203rd 204th 205th ... 210th
211th 212th 213th 214th 215th ... 220th
221st 222nd 223rd 224th 225th ... 230th


I know it's a bit harder to view but that's the best way of displaying it.

It takes into account the last number and (in some cases) the second to last number.

After that it's just a matter of placing the 'st', 'nd', 'rd' and 'th' (for everything after).

Catch my drift?

Thanks for the help guys!
Topic archived. No new replies allowed.