首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

将C字符串变换为整数

2012-11-03 
将C字符串转换为整数// c string to int.cpp : Defines the entry point for the console application.///

将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字符串变换为整数

热点排行