-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathconvert_string_to_int.cpp
More file actions
64 lines (45 loc) · 1.63 KB
/
convert_string_to_int.cpp
File metadata and controls
64 lines (45 loc) · 1.63 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*******************************************************************************
*
* Program: Convert A String To An Int
*
* Description: Two approaches to convert a string to an int in C++.
*
* YouTube Lesson: https://www.youtube.com/watch?v=a43R5WBs2zQ
*
* Author: Kevin Browne @ https://portfoliocourses.com
*
*******************************************************************************/
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
// Approach #1: Use stoi()
// Create a test string.
string number1 = "42";
// The stoi() function from the string library will accept a string as an
// argument and convert it to an int, returning the int value, which we then
// store into n1.
int n1 = stoi(number1);
// Output n1 to check that the conversion was successful.
cout << n1 << endl;
// The stoi() function is only available in C++11 onwards, so the above
// approach will not work in older versions of C++.
// Approach #2: Use a stringstream object
// Declare the stringstream object, we can use the stream insertion and
// extraction operator with this stream in a similar way we do with
// other streams like cin and cout.
stringstream convert;
// Create a test string.
string number2 = "4";
// Use the stream insertion operator to put the string onto the stream.
convert << number2;
// Declare an int variable to store the converted into value.
int n2;
// Use the stream extraction operator to extract the converted int value.
convert >> n2;
// Output n2 to confirm the conversion was successful.
cout << n2 << endl;
return 0;
}