ที่นี่เราจะมาดูวิธีการแปลงสตริงตัวเลขเป็นข้อมูลประเภทจำนวนเต็ม เราสามารถแก้ปัญหานี้ได้โดยใช้ฟังก์ชัน atoi() ฟังก์ชันนี้ใช้สตริงเป็นอินพุตและแปลงเป็นข้อมูลจำนวนเต็ม
ฟังก์ชัน atoi() มีอยู่ในไลบรารี
Input: A number string “1234” Output: 1234
อัลกอริทึม
Step 1: Take a number string Step 2: Convert it to integer using atoi() function Step 3: Print the result. Step 4: End
โค้ดตัวอย่าง
#include<iostream>
#include<cstdlib>
using namespace std;
main() {
int n;
char num_string[20] = "1234";
n = atoi(num_string);
cout << n;
} ผลลัพธ์
1234