[60分]2个简单问题!!
1.我有个string str= "this is just a test, and you will find some useful information";
如何通过定义的:char ch;把string中的字符
一个一个读入到ch中,
每次要做测试if(ch != ',')我要检测这个“,”号,想找个好点的办法!(有示范代码或函数最好)
2.三目运算符“(测试条件)? first :second ”要求first和second是同类型
如下:
假设我有一个数组:
要求输入的数值n>size时能输出"Error!",否则输出“总数”(total)
const int size = 10;
int array[] = {.........}; //小于10个
..........
int n;
cout<<"please input a int number n:"<<endl;
cin>>n;
.....
int total=0;
int temp = (n > size)? *(int*)ch:total; //这里不行
如下:
#include <iostream>
const int size = 10;
using namespace std;
int main()
{
int arr[] = {1, 2, 4, 8, 16, 32, 64, 128};
int n;
cout<<"Input the num of the arr[]: "<<endl;
cin>>n;
int total = 0;
if(n > size)
{
cout<<"error! Please input a correct number !"<<endl;
for(int i=0; i<n; i++)
{
total += arr[i];
}
cout<<"the address of the arr[]= "<<arr<<endl;
cout<<"the size of the arr[]= "<<sizeof arr<<endl;
char * ch ="error!";
char * temp = (n > size)? ch:(char *)total;
cout<<"the all number is: "<<temp<<endl;
}
cin.get();
return 0;
}
可以使用:while,do-while,for + if..else来写!!