【问题标题】:My program keeps looping in the first do...while loop even when condition isn't met我的程序在第一个 do...while 循环中不断循环,即使条件不满足
【发布时间】:2019-03-03 04:06:21
【问题描述】:

我 3 天前开始学习 C++,在对循环和向量进行了一些试验后,我决定用它做一些真正有用的事情:客户经理。

关键是,我正在使用 do...while 循环,用于我程序中的第一个操作(即添加一个新网站),但是过了这一点,即使条件满足,循环也不会结束再也遇不到了。

我尝试调试了整整 30 分钟,但没有发现任何异常。

代码如下:

#include <iostream>
#include <string>
#include <vector>


using namespace std;
int main()
{
    /*Introduction
    //Ask whether the user(me) wants to open an account already created, add a new one, or remove an existing one
    To show credentials a master password is required
    I need something that can:
        1. Find the place where credentials are supposed to be filled
        2. Enter them efficiently
        3. Bonus : Submit the data on the website and automatically connect*/


    int userAction; // Variable de sélection de la première action
    string siteNameVar("site"), urlVar("url"), userNameVar("username"), passwordVar("pass") ; 
    char sureVerification;

    vector<string> siteName(0); // Vectors containing respectively : "The sites names"
    vector<string> url(0); // Vectors containing respectively : "The sites urls"
    vector<string> userName(0); // Vectors containing respectively : "The  usernames"
    vector<string> password(0); // Vectors containing respectively : "The  passwords"



    cout << "What will you do?" << endl;

    cout << "1. Add a website account" << endl
         << "2. Connect to an existing account" << endl
         << "3. Delete an account"<< endl;

    cin >> userAction; // This is where the user enter his choice

    switch (userAction){
        case 1: // Add a new element in the vectors


           do{
                //Site Name
                do{
                    cout << "Enter the site's name (or how you want to call it)" << endl;
                    cin >> siteNameVar;
                    cout << "Are you sure? 1 = yes | Anything else = no" << endl;
                    cin >> sureVerification;
                }
                while (sureVerification != 1);




                //Site's Url
                do{
                    cout << "Enter the site's login page url" << endl;
                    cin >> urlVar;
                    cout << "Are you sure? 1 = yes | Anything else = no" << endl;
                    cin >> sureVerification;
                }

                while(sureVerification != 1);
                url.push_back(urlVar);

                // Username
                do{
                    cout << "Enter your account's username" << endl;
                    cin >> userNameVar;
                    cout << "Are you sure? 1 = yes | Anything else = no" << endl;
                    cin >> sureVerification;
                }

                while(sureVerification != 1);
                userName.push_back(userNameVar);

                // Password
                do{
                    cout << "Enter your account's password" << endl;
                    cin >> passwordVar;
                    cout << "Are you sure? 1 = yes | Anything else = no" << endl;
                    cin >> sureVerification;
                }

                while(sureVerification != 1);
                password.push_back(passwordVar);

                //Display Everything

                cout << "So the site's name is :" << siteName.back() << endl 
                     << "The login page url is :" << url.back() << endl 
                     << "Your account's username is :" << userName.back() << endl 
                     << "And your password is :" << password.back() << endl;

                //Last verification
                cout << "Is everything alright? 1 = yes | Anything else = no" << endl;
                cin >> sureVerification;
            }
            while(sureVerification != 1);




            cin.get();

            break;
        case 2: // Connect to an existing account
            cout << "display map element names" << endl;
            break;

        case 3: // Delete an account
            cout << "display map element names2" <<endl;
            break;



    } // End of the choice sequence

    cin.get();
    return 0;

}

【问题讨论】:

  • 将所有相关代码直接作为文本发布在这里。

标签: c++ windows loops shared-libraries


【解决方案1】:

您应该尝试清理输入缓冲区。在读取用户输入之前使用 cin.clear() 和 cin.ignore()(例如,在 cin >>sureVerification 之前)

【讨论】:

  • 非常感谢您的快速回答。我尝试在(cin >>sureVerification)之前添加cin.clear()函数,但问题仍然存在,无论sureVerification值如何,循环都不会结束。
  • 我没有意识到 sureVerification 是一个字符。您是否尝试过sureVerification!=“1”?如果它是 char 变量,则应添加引号
【解决方案2】:

(代表问题作者发布).

哇,在将我的代码的早期版本与我发布的代码进行比较,并将一段旧代码替换为新代码(“奇怪”有效)之后,我意识到问题完全是由于SureVerification 的值类型(阅读代码以理解)是 char 并且在验证表达式中我写了 1(相当于“是”作为 int。

问题解决了!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-31
    • 2022-10-13
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多