C++中怎样识别输入的是空行??

来源:百度知道 编辑:UC知道 时间:2024/06/18 07:23:58
下面是有我编写的有问题的代码:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cctype>
const double M_sum = 10000;
using namespace std;
struct donation
{
char name[20];
double sum;
};

int _tmain(int argc, _TCHAR* argv[])
{
int n;
cout<<"Enter number of donation: ";
cin>>n;
donation *temp = new donation[n];
int i;
for (i=0; i<n; i++)
{
cout<<"Enter name of patrons:\n";
cin>>temp[i].name ;
if (strlen(temp[i].name)==0) //问题:我的主要目的是:如果输入为空,则执行以下代码
strcpy(temp[i].name, "none");
cout<<"Enter sum:\n";//但为什么程序接下来不运行此行?程序的意思是不希望我输入空行,那样就没有达到我预期的效果
cin>>temp[i].sum;
}
cout<<"Grand Patrons:\n";
for (int j = 0; j<i; j++)
{
if (temp[j].sum > M_sum)
cout<&l

cin>>temp[i].name ; 会自动滤过空格,
你可以用这种方式防止空格背过滤掉:
cin.unsetf( ios::skipws );
试试看哈,放在这个语句的前面。
也可以用getline来代替。

template<class CharType, class Traits, class Allocator>
basic_istream<CharType, Traits>& getline(
basic_istream<CharType, Traits>& _Istr,
basic_string<CharType, Traits, Allocator>& _Str
);