武器大师e技能:初学C++关于字符(串)录入的问题。

来源:百度文库 编辑:高校问答 时间:2024/04/20 03:06:04
VC6 win32 console程序,
我想让函数只返回接收键盘输入的double数值,对于其他类型数值都不接收,并给出提示。
尝试用cin.get()
或cin
或cin.getline()作成
我写的例程如下,现不知是否有更好的处理方法,或规范的处理方法:
#include "stdafx.h"
#include <iostream>
#include <cctype>
using namespace std;

int main(int argc, char* argv[])
{
const int ASize = 10;
double ave[ASize];
int datain[ASize];
//number input:
char ch;
char lch;
int tmp1 = 0;
double tmp2 =0.0;
int dotflg = 0;

for (int i=0; i<ASize; i++)
{
tmp1 = 0;
tmp2 = 0.0;
dotflg = 0;

cout << "Enter your " << i+1 <<"st number:\n";
cin.get(ch);

while(isdigit(ch) || ch=='.')
{
datain[i] = 1;
if (isdigit(ch) && dotflg == 0)
tmp1 = tmp1*10 + ((int)ch-48);
else if (ch=='.')
dotflg = 1;
else if (isdigit(ch) && dotflg == 1)
tmp2 = tmp2 + (double)((int)ch-48)*0.1;
else
break;
lch = ch;
cin.get(ch);
}
if ((isdigit(lch) || lch=='.')&&!(isdigit(ch) || ch=='.'))
ave[i] = (double)tmp1 + tmp2;
}
//number output:
double sum = 0.0;
int per = 0;
for (i=0; i<ASize; i++)
{
if(datain[i]==1)
{
sum = sum + ave[i];
per++;
}
// else
}

double average = sum/per;
cout <<"\nThe average of the number is:" << average <<endl;

for (i=0; i<ASize; i++)
{
if(datain[i]==1)
{
if(ave[i] > average)
cout <<"The ave[" << i <<"] is: " << ave[i] << "> the average.\n";
}
// else
}
return 0;
}
请大家多多指点批评,先都谢谢了!!!

“让函数只返回接收键盘输入的double数值,对于其他类型数值都不接收,并给出提示。”

我认为最好是写一个接受字符串参数、返回 double、并利用C++ 异常处理报错的函数。
我写的 strToDouble 就是这么一个函数。若参数通过所有检测,strToDouble 便利用 stringstream 类把参数转换成 double。

下面是完整的代码。代码并不复杂,而且有文档。
main( ) 里有调用该函数的例子。

#include<iostream>
#include<string>
#include<sstream>
#include<cctype> // isdigit( )

using namespace std;

/*
* Thrown by strToDouble.
*/
class NumberFormatException {
    // The name of this class is all that is needed.
};

/*
* Returns a double corresponding to the number represented by s if the
* number is in valid double format; else throws NumberFormatException.
*/
double strToDouble( const string& s ) throw( NumberFormatException ) {

    //
    // Validation.
    //
    string copy = s;

    // Erase the optional decimal point.
    size_t dotpos = copy.find( '.' );
    if( dotpos != string::npos )
        copy.erase( dotpos, 1 );

    // Erase the optional sign that is allowed only as the first char.
    if( copy[ 0 ] == '+' || copy[ 0 ] == '-' )
        copy.erase( 0, 1 );

    // At this point, copy should contain only digits.
    for( size_t i = 0; i < copy.size( ); ++i )
        if( ! isdigit( copy[ i ] ) )
            throw NumberFormatException( );

    // At least one digit is required.
    if( ! copy.size( ) ) throw NumberFormatException( );

    //
    // Validation completed. Do conversion.
    //
    stringstream ss( s );
    double d;
    ss >> d;

    return d;
}

int main( ) {

    cout << "Enter a valid number (sign and decimal point is allowed):\n";

    string s;
    double d;

    for(;;) {
        cin >> s;

        try {
            d = strToDouble( s );
            break;
        }
        catch ( NumberFormatException& ) {
            cout << s << " isn't a valid number. Try again.\n";
        }
    }

    cout << d << " is valid. Thank you!\n";

    return 0;
}

你可以这样:
cout<<"......:";
double next;
cin>>next;
bool more=true;
while(more)
{
if(cin.fail())
{cout<<"please input again:";
cin>>next;
}
else
more=false;
}//go on;

cin和cout可以直接处理double类型的数据