读书人

将C字符串变换为整数

发布时间: 2012-11-03 10:57:43 作者: rapoo

将C字符串转换为整数

// c string to int.cpp : Defines the entry point for the console application./////功能::将C字符串转换为整数#include "stdafx.h"#include<iostream>#include<cstdlib> ////atoi()#define MAX  65535int 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;}


将C字符串变换为整数

读书人网 >编程

热点推荐