簡體   English   中英

C++ 驗證 - 截斷設置為下一個輸入

[英]C++ Validation - Truncation set as next input

我一直在嘗試驗證用戶輸入時遇到這個問題,但我發現自己在整個程序中使用了大量的 ignores()。 由於它是學校計划,因此僅限iostreamcctypestudent.h庫。 問題是我需要確保用戶不會嘗試將字母字符輸入到我認為我用if(!(cin >> val))覆蓋的整數字段中,然后我使用cin.ignore(numeric_limits<streamsize>::max(), '\\n'); 忽略任何額外的內容(例如,如果他們嘗試在整數字段中輸入小數)。 盡管我無法輸入名稱字段,或者我將剩余的小數作為名稱(如果我輸入 123.6,名稱將為 0.6),但某些東西無法正常工作。 有人知道比使用質量忽略更好的驗證整數的方法嗎?

主要的

#include <iostream>
using namespace std;
#include "student.h"
//**************************************************************************
bool validInt(int&);
//**************************************************************************
int main()
{
    Student student;
    bool validInput;

    cout << "You have Chosen to Add a New Student." << endl;
    cout << "---------------------------------------" << endl;

    // Student ID Validation
    do
    {
        cout << "Enter Student ID (ex. 123): ";
        validInput = validInt(student.id);
    }while(!validInput);

    cout << "Enter Student Name (ex. John Doe): ";
    cin.getline(student.name, 50);

    cout << "Enter Student City and State (ex. St. Louis, Missouri): ";
    cin.getline(student.citystate, 50);

    cout << "\n\nStudent ID: " << student.id << endl;
    cout << "Student Name: " << student.name << endl;
    cout << "Student City / State: " << student.citystate << endl;

    return 0;
}
//**************************************************************************
bool validInt(int& val)
{
    bool valid = true;
    if(!(cin >> val))
    {
        cout << "\nERROR: Please enter a Positive Whole Number" << endl;
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        valid = false;
    }
    return valid;
}

學生頭

#ifndef STUDENT_H
#define STUDENT_H
//**************************************************************************
struct Student
{
    int id;
    char name[50];
    char citystate[50];

    friend ostream& operator<<(ostream& out, const Student& data);

    bool operator == (const Student &rhs) const;
    bool operator != (const Student &rhs) const;
    bool operator < (const Student &rhs) const;
    bool operator > (const Student &rhs) const;
    bool operator <= (const Student &rhs) const;
    bool operator >= (const Student &rhs) const;
};
//**************************************************************************
ostream& operator << (ostream& out, const Student& data)
{
    out << data.id << " " << data.name << endl;
    return out;
}
//**************************************************************************
bool Student::operator == (const Student &rhs) const
{
    return (this->id == rhs.id);
}
//**************************************************************************
bool Student::operator != (const Student &rhs) const
{
    return (this->id != rhs.id);
}
//**************************************************************************
bool Student::operator < (const Student &rhs) const
{
    return (this->id < rhs.id);
}
//**************************************************************************
bool Student::operator > (const Student &rhs) const
{
    return (this->id > rhs.id);
}
//**************************************************************************
bool Student::operator <= (const Student &rhs) const
{
    return (this->id <= rhs.id);
}
//**************************************************************************
bool Student::operator >= (const Student &rhs) const
{
    return (this->id >= rhs.id);
}
#endif

通常,您會使用std::getline(std::string&,std::istream&)進行面向行的輸入並使用istringstream解析數字。 但是,由於不允許您既不使用std::string也不使用std::stringstream您需要自己解析整數。

但首先是對你的錯誤的解釋。

解釋

雖然我無法輸入名稱字段,但某些功能無法正常工作

當您輸入正確的整數時,不會從字符串中提取換行符\\n ,這就是為什么cin.getline(student.name, 50); 不會提取任何內容: std::cin的下一個字符是\\n ,該行結束並且std::getline用提取行填充student.name ,該行是空的。

或者我將剩余的小數作為名稱(如果我輸入 123.6,名稱將為 0.6)。

同樣,這是相同的:您只提取整數,但不提取行的其余部分。

臨時解決方案

忽略validInt的其余行:

bool validInt(int& val)
{
    bool valid = true;
    if(!(cin >> val))
    {
        cout << "\nERROR: Please enter a Positive Whole Number" << endl;
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        valid = false;
    }
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    return valid;
}

正確的解決辦法

正確的解決方案要復雜一些,因為您必須編寫一些內容來提取單個字符,檢查它們是否是數字(或減號或加號),將它們保存為整數並忽略該行的其余部分。 別擔心,只有<iostream>才能做到這一點

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM