-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIntToString.cpp
More file actions
30 lines (22 loc) · 764 Bytes
/
IntToString.cpp
File metadata and controls
30 lines (22 loc) · 764 Bytes
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>
using namespace std;
string IntToString(int x)
{
bool check_if_negative; // create a variable to store the sign of the number.
if (x < 0) { check_if_negative = true; cout << check_if_negative << endl; } // check is the number is positive or negative
string s = "aa"; // create variables to store the results and digit by digit slicing of the number
do {
s += '0' + abs(x % 10); // convert each digit in the number to a character
x /= 10;
cout << x << endl;
} while (x); // keep looping until the division becomes zero
return {s.rbegin(),s.rend()}; // revert string
}
int main()
{
int test_num = 1234;
cout << IntToString(test_num) << endl;
cout << '0' + 2 << endl;
return 0;
}