My First CPP Code

留作记录。来自课程《Stanford CS106L》。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
int stringToInt(const string& s);
string s;
while(true) {
cout << "Type in an integer: ";
cin >> s;
int result = stringToInt(s);
cout << result << endl;
cout << result/2 << endl;
}
return 0;
}

int stringToInt(const string& s) {
istringstream iss(s);
int n;
iss >> n;
return n;
}