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

字符串输入输出有关问题

2012-03-24 
字符串输入输出问题此程序纯在这样的BUG,就是输入名字的时候,如果输入带空格的名字,就会出错如DKYANG.如何

字符串输入输出问题
此程序纯在这样的BUG,就是输入名字的时候,如果输入带空格的名字,就会出错
如DK   YANG.
如何修改出问题的部分呢?
程序如下:
#include   <iostream>
const   int   SIZE=20;
struct   donation
{
        char   name[SIZE];
double   money;

};

int   main()
{
using   namespace   std;
int   number;
cout < < "Enter   the   number   of   donators: ";
cin> > number;
        donation   *Dn=new   donation[number];
cout < < "Enter   all   of   the   information   of   donators: "
< < "include   names   and   money\n ";
for(int   i=0;i <number;i++)
{
cout < < "Name:\n ";
cin> > Dn[i].name;//问题在这里如何修改?
cout < < "Money:\n ";
cin> > Dn[i].money;
}
for(i=0;i <number;i++)
{
          if(Dn[i].money> 10000)
{
cout < < "Grand   Patron:   " < <Dn[i].name < < '\t ';
cout < < "Money:   " < <Dn[i].money < < "\t ";
cout < <endl;
}
else   if(Dn[i].money> 0&&Dn[i].money <=10000)
{
cout < < "Patron:   " < <Dn[i].name < < "\t ";
cout < < "Money:   " < <Dn[i].money < < "\t ";
cout < <endl;
}
else
cout < < "None!\n ";
}
delete   []   Dn;
return   0;
}

[解决办法]
解决了!
主要原因是回车符的处理!代码见下.
#include <iostream>
const int SIZE=20;
struct donation
{
char name[SIZE];
double money;

};

int main()
{
using namespace std;
int number;
cout < < "Enter the number of donators: ";
cin> > number;
donation *Dn=new donation[number];
cout < < "Enter all of the information of donators: "
< < "include names and money\n ";
char a;
cin.getline(&a, 1);
for(int i=0;i <number;i++)
{
cout < < "Name:\n ";
//cin> > Dn[i].name;//&Icirc;&Ecirc;&Igrave;&acirc;&Ocirc;&Uacute;&Otilde;&acirc;&Agrave;&iuml;&Egrave;&ccedil;&ordm;&Icirc;&ETH;&THORN;&cedil;&Auml;?
cin.getline(Dn[i].name, 20);
cout < < "Money:\n ";
cin> > Dn[i].money;
cin.getline(&a, 1);
}
for(i=0;i <number;i++)
{
if(Dn[i].money> 10000)
{
cout < < "Grand Patron: " < <Dn[i].name < < '\t ';
cout < < "Money: " < <Dn[i].money < < "\t ";
cout < <endl;
}
else if(Dn[i].money> 0&&Dn[i].money <=10000)
{
cout < < "Patron: " < <Dn[i].name < < "\t ";
cout < < "Money: " < <Dn[i].money < < "\t ";
cout < <endl;
}
else
cout < < "None!\n ";
}
delete [] Dn;
return 0;
}

热点排行