| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1168 人关注过本帖
标题:向大神求助cin.clear和cin.sync的问题,新手第一次求教
只看楼主 加入收藏
alhbbc
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-10-10
收藏
 问题点数:0 回复次数:1 
向大神求助cin.clear和cin.sync的问题,新手第一次求教
#include <iostream>
#include <string>
using namespace std;

 

int main ( )

{    //Welcome students to this program
    cout << "Welcome to Student Information System, follow these steps and be satisfied with your result!" << endl<< endl<< endl;

    //creat varibles as student number and percentage grade
    int studentNumber = 0;
    double percentageGrade = 0.0;
    string letterGrade;

   
    do
    {               
        //Ask for student Number
            cout <<"Enter your student number here: ";
            cin >> studentNumber;
        
        //Enter value -1 to termine the program
            while(studentNumber == -1)
        {    return 0;
        }
        
        //Check if the user dosen't type correctly
            while(((studentNumber<100000000)||(studentNumber>999999999))&&(studentNumber != -1))
            {    cin.clear();
                cin.sync();
                cout << "Please enter correctly,invalid numbers are not allowed."<< endl<< endl;
                cout <<"Enter your student number here: ";
                cin >> studentNumber;        
                while(studentNumber == -1)
                {    return 0;
                }
            }   
        
        
        //Ask student user to enter his/her percentage grade
            cout <<"Enter your percentage grade here: ";
            cin >> percentageGrade;

        //Check if the user dosen't type correctly        
            while((percentageGrade<0.0)||(percentageGrade>100.0))
            {    cin.clear();
                cin.sync();
                cout << "Please enter correctly,invalid numbers are not allowed."<< endl<< endl;
                cout <<"Enter your percentage grade here: ";
                cin >> percentageGrade;        
            }

            


        //Check out the letter grade
            if ((percentageGrade>=0.0)&&(percentageGrade<=100.0))
            {    if (percentageGrade>85.5)
                {    letterGrade = "an A.";
                }
                else if(percentageGrade>73.5)
                {    letterGrade = "a B.";
                }
                else if(percentageGrade>61.5)
                {    letterGrade = "a C.";
                }
                else if(percentageGrade>49.5)
                {    letterGrade = "a D.";
                }
                else
                {    letterGrade = "a F.";
                }
            }
        //Give the final comment
            cout <<"Student number: "<<studentNumber<<", percentage grade: "<<percentageGrade<<"%."<<endl<<"This student will receive"<<letterGrade<<endl<< endl<< endl;
        
    }
    while(cin.fail());
}


以上是我的程序,作业是要求做一个学生成绩查询系统,根据学分百分比给出ABCDF的级别
要求是学生可以不停的查询(所以我用do while,应该while也可以),直到学生在要求输入学生号的时候输入-1来退出系统
我现在遇到的问题是,studentNumber和percentageGrade都是数字形式的变量,一旦cin的时候输入字母或符号就会无限死循环,所以我用cin.clear()和cin.sync()来解决这个死循环,
但问题就来了,我放了两个cin.clear()和cin.sync()分别给studentNumber和percentageGrade,但是只有studentNumber那部分会退出死循环,percentageGrade那部分如果输入字母或者符号,还是继续死循环,难道cin.clear()和cin.sync()只能用一次吗?
搜索更多相关主题的帖子: number double include percentage Welcome 
2011-10-10 07:51
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9024
专家分:54030
注 册:2011-1-18
收藏
得分:0 
随便举个例子吧

程序代码:
//Ask for student Number
int student_number = 0;
for( ; ; )
{
    cout << "Enter your student number here: ";
    cin >> student_number;
    if( !cin ) // 如果输入错误,比如输入了字母等等导致cin进入错误状态
    {
        cin.clear(); // 那就要 清楚错误状态
        cin.sync();  // 并把那些错误的输入也清除掉
    }
    else if( student_number == -1 ) // Enter value -1 to termine the program
    {
        return 0;
    }
    else if( student_number>=100000000 && student_number<=999999999 )
    {
        break;
    }
    cout << "Please enter correctly,invalid numbers are not allowed.\n" << endl;
}

2011-10-10 08:45
快速回复:向大神求助cin.clear和cin.sync的问题,新手第一次求教
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018118 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved