Linux教程網
- // c string to int.cpp : Defines the entry point for the console application.
- //
-
- ///功能::將C字符串轉換為整數
-
- #include "stdafx.h"
- #include<iostream>
- #include<cstdlib> ////atoi()
-
- #define MAX 65535
-
- int main(int argc, char* argv[])
- {
- using namespace std;
-
- char digit_string[MAX]; //保存字符'0'---'9'的數組
- char next;
- int index = 0;
- cout<<"Enter an integer and press return:";
- cin.get(next);
- while (next != '\n')
- {
- if( isdigit(next) && (index < MAX-1) ) ///丟棄不屬於'0'--'9'的字符
- {
- digit_string[index] = next;
- index++;
- }
- cin.get(next);
- }
- cout<<endl<<endl;
- cout<<"String converts to the integer :";
- cout<<atoi(digit_string); ///***********
- cout<<endl;
- return 0;
- }
Copyright ©
Linux教程網 All Rights Reserved