Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;清除流和测试输入-多个问题 我现在正在学习C++,我在验证方面有3个问题。我的教授要求我不要使用stringstream来验证输入。我遇到了一个问题,例如,如果用户输入两个字符'rr',错误消息会显示两次(这就是为什么我最初使用stringstream来测试第一个不正确的字符)。第二个问题是它们是否输入了两个值,例如-45-56。不知何故,第二个值56在循环之后变为正值,并且程序正在使用它运行,即使在我清除流之后也是如此。 最后,如果用户在int中输入一个float,它会将其截断——但如何防止这种情况发生呢?例如,我只想输入一个整数。我试了很多次,但都没有成功。请注意,在下面发布的代码中,我没有截断问题,因为用户输入是双精度的,但我认为应该有人理解我所要求的理论_C++_Validation_Input_Stream - Fatal编程技术网

C++;清除流和测试输入-多个问题 我现在正在学习C++,我在验证方面有3个问题。我的教授要求我不要使用stringstream来验证输入。我遇到了一个问题,例如,如果用户输入两个字符'rr',错误消息会显示两次(这就是为什么我最初使用stringstream来测试第一个不正确的字符)。第二个问题是它们是否输入了两个值,例如-45-56。不知何故,第二个值56在循环之后变为正值,并且程序正在使用它运行,即使在我清除流之后也是如此。 最后,如果用户在int中输入一个float,它会将其截断——但如何防止这种情况发生呢?例如,我只想输入一个整数。我试了很多次,但都没有成功。请注意,在下面发布的代码中,我没有截断问题,因为用户输入是双精度的,但我认为应该有人理解我所要求的理论

C++;清除流和测试输入-多个问题 我现在正在学习C++,我在验证方面有3个问题。我的教授要求我不要使用stringstream来验证输入。我遇到了一个问题,例如,如果用户输入两个字符'rr',错误消息会显示两次(这就是为什么我最初使用stringstream来测试第一个不正确的字符)。第二个问题是它们是否输入了两个值,例如-45-56。不知何故,第二个值56在循环之后变为正值,并且程序正在使用它运行,即使在我清除流之后也是如此。 最后,如果用户在int中输入一个float,它会将其截断——但如何防止这种情况发生呢?例如,我只想输入一个整数。我试了很多次,但都没有成功。请注意,在下面发布的代码中,我没有截断问题,因为用户输入是双精度的,但我认为应该有人理解我所要求的理论,c++,validation,input,stream,C++,Validation,Input,Stream,请让我知道,如果这里的格式不正确,我有一些问题,粘贴到块,但它看起来可能是好的 谢谢大家! 这是我的密码: #include <iostream> #include <iomanip> using namespace std; int main() { const double EARTH_G = 1, SPACE_G = 0, EARTH_MOON_G = .17, VENUS_G = .90, MARS_G = .38, MERCURY_G = .38

请让我知道,如果这里的格式不正确,我有一些问题,粘贴到块,但它看起来可能是好的

谢谢大家!

这是我的密码:

#include <iostream>
#include <iomanip>

using namespace std;


int main()

{
const double EARTH_G = 1, SPACE_G = 0, EARTH_MOON_G = .17, VENUS_G = .90, MARS_G = .38,
    MERCURY_G = .38, JUPITER_G = 2.36, SATURN_G = .92, URANUS_G = .89,
    NEPTUNE_G = 1.14, PLUTO_G = .07;

double userWeight;

enum planets {Earth = 1, Space, Moon, Venus, Mars, Mercury, Jupiter, Saturn, Uranus,
Neptune, Pluto};


/*Validates input by making sure checking for only numbers greater than 0*/
{
    bool flag = 0;
    while (flag == 0)
    {
        cout << "What is your earth weight in lbs?" << endl;
        cin >> userWeight;
        if (!static_cast<double>(userWeight) || userWeight < 0)
        {
            cout << "Invalid Input.\nPlease enter only positive numbers." << endl;
            cin.clear();
            cin.ignore();


        }
        else
        {
            flag = 1;
        }

        }
    }
{
    //Displays menu with choices, and allows produces output based on choice.
    //Loops while input is invalid.
    bool flag = 0;
    int choice;
    double newWeight;
    while (flag == 0)
    {
        cout << "Select a place in space with a number to see your weight there!\n\n";
        cout << "1  - Earth\n\n2  - Space\n\n3  - Earth's Moon\n\n4  - Venus\n\n5  - Mars\n\n"
             << "6  - Mercury\n\n7  - Jupiter\n\n8  - Saturn\n\n9  - Uranus\n\n10 - Neptune\n\n11 - Pluto\n\n";
        cout << "Select 1 - 11:\n";
        cin >> choice;

        switch (choice)
        {
        case(Earth):
        {
            newWeight = (userWeight * EARTH_G);
            cout << "Your weight on earth is " << fixed << setprecision(1)
                 << newWeight << endl;
            flag = 1;
            break;
        }
        case(Space):
        {
            newWeight = (userWeight * SPACE_G);
            cout << "Your weight in space is " << fixed << setprecision(1)
                << newWeight << endl;
            flag = 1;
            break;
        }
        case(Moon):
        {
            newWeight = (userWeight * EARTH_MOON_G);
            cout << "Your weight on the Earth's moon is " << fixed << setprecision(1)
                 << newWeight << endl;
            flag = 1;
            break;
        }
        case(Venus):
        {
            newWeight = (userWeight * VENUS_G);
            cout << "Your weight on Venus is " << fixed << setprecision(1) << newWeight << endl;
            flag = 1;
            break;
        }
        case(Mars):
        {
            newWeight = (userWeight * MARS_G);
            cout << "Your weight on Mars is " << fixed << setprecision(1) << newWeight << endl;
            flag = 1;
            break;
        }
        case(Mercury):
        {
            newWeight = (userWeight * MERCURY_G);
            cout << "Your weight on Mercury is " << fixed << setprecision(1) << newWeight << endl;
            flag = 1;
            break;
        }
        case(Jupiter):
        {
            newWeight = (userWeight * JUPITER_G);
            cout << "Your weight on Jupiter is " << fixed << setprecision(1) << newWeight << endl;
            flag = 1;
            break;
        }
        case(Saturn):
        {
            newWeight = (userWeight * SATURN_G);
            cout << "Your weight on Saturn is " << fixed << setprecision(1) << newWeight << endl;
            flag = 1;
            break;
        }
        case(Uranus):
        {
            newWeight = (userWeight * URANUS_G);
            cout << "Your weight on Uranus is " << fixed << setprecision(1) << newWeight << endl;
            flag = 1;
            break;
        }
        case(Neptune):
        {
            newWeight = (userWeight * NEPTUNE_G);
            cout << "Your weight on Neptune is " << fixed << setprecision(1) << newWeight << endl;
            flag = 1;
            break;
        }
        case(Pluto):
        {
            newWeight = (userWeight * PLUTO_G);
            cout << "Your weight on Pluto is " << fixed << setprecision(1) << newWeight << endl;
            flag = 1;
            break;
        }
        default:
        {
            cout << "Invalid Input." << endl;
            cin.clear();
            cin.ignore();
        }

        }
    }
}
}
#包括
#包括
使用名称空间std;
int main()
{
常数双地球=1,空间=0,地月=0.17,金星=0.90,火星=0.38,
水星=0.38,木星=2.36,土星=0.92,天王星=0.89,
海王星=1.14,冥王星=0.07;
双重用户权重;
枚举行星{地球=1,太空,月球,金星,火星,水星,木星,土星,天王星,
海王星,冥王星};
/*通过确保只检查大于0的数字来验证输入*/
{
布尔标志=0;
while(标志==0)
{
cout用户权重;
如果(!static_cast(userWeight)| userWeight<0)
{
cout根据,基本的无参数忽略只从输入流中删除一个字符。根据上述文档链接中提供的示例,建议使用

input.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
input.ignore(std::numeric_limits::max(),'\n');

使用整行。请注意获取
numeric_limits::max

为什么要进行否决票所需的附加include语句?我想如果您有足够的知识进行否决票,您可以提供一个快速答案?解决此类问题的正确工具是您的调试器。在询问之前,您应该逐行检查代码关于堆栈溢出。有关更多帮助,请阅读。至少,您应该[编辑]您的问题包括一个重现您的问题的示例,以及您在调试器中所做的观察。对不起,我确实运行了调试器,我在问题中发布的结果就是我所看到的。我是要构建一个变量表并发布它吗?还是只重写一个代码示例?发布的代码是最少的我需要看看我的问题,除了代码中没有明确说明的截断问题,但他的理论很容易理解。对不起,在电话上,无法滚动编辑。我已经做了很多研究,调试,我所有的研究都一直说使用stringstream,但我的教授说不要使用。因此,我在这里询问万不得已。谢谢!我完全忽略了这一点。