[60分]2个简单问题!!

tudou614 2005-03-19 05:44:35
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来写!!
...全文
157 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
kobefly 2005-03-19
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

const int size = 10;

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;
while(n > size)
{
cout<<"error! Please input a correct number !"<<endl;
cin >> n;
}
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;



cout<<"the all number is: "<<total<<endl;


cin.get();
return 0;
}
wolfkain 2005-03-19
  • 打赏
  • 举报
回复
char * temp = (n > size)? ch:(char *)total;有问题;total见定义(int total),所以
(char *)total出错,去掉(char *)会有temp=total,也错;写的什么狗屁!!!!!
tudou614 2005-03-19
  • 打赏
  • 举报
回复
第2个要求:可以使用while,do-while,for + if..else来写!!
就是如果数组越界了,能够输出字符串"Error",否则输出数组中元素的和,现在分开我都能实现
就是和在一起的时候强制转型搞不好
xxxdg 2005-03-19
  • 打赏
  • 举报
回复
char * temp = (n > size)? ch:(char *)total;

----------------------->>>>>>>>>>>>>>>>>>>>>>>>>>>>>


char sTol[8];
sprintf(sTol,"%d",totoal);
char * temp =( n>size)?ch:sTol;


ltc_mouse 2005-03-19
  • 打赏
  • 举报
回复
char * temp = (n > size)? ch:(char *)total;
===================================================
total是一个数值,直接当作地址,是危险的,也得不到结果~
可以试试这个:
char strTotal[20];
sprintf(strTotal, "%d\0", total);
char *temp = (n>size) ? ch : strTotal;
kobefly 2005-03-19
  • 打赏
  • 举报
回复
第2个你说的似乎不太明白

我弄不懂
kobefly 2005-03-19
  • 打赏
  • 举报
回复
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{
string str= "this is just a test, and you will find some useful information";
const char *pch;
char ch;

pch = str.c_str();

while(ch = *pch)
{
if(ch == ',')
{
cout << "find!" << endl;
return 0;
}
pch++;
}

cout << "not find" << endl;

return 0;
}
ltc_mouse 2005-03-19
  • 打赏
  • 举报
回复
Sorry,看错了~
我还以为两个问题说明一样呢.........
ltc_mouse 2005-03-19
  • 打赏
  • 举报
回复
int temp = (n > size)? *(int*)ch:total; //这里不行
===========================================================
ch 是char类型的,能强制为int *吗? 改一下试试:
int temp = (n > size) ? (int)ch : total;
tudou614 2005-03-19
  • 打赏
  • 举报
回复
没人知道吗??

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧