即使条件不满足,我的程序也会在第一个do ... while循环中循环

问题描述 投票:-1回答:2

我在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
2个回答
0
投票

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


-1
投票

(代表作者提问)。

哇,在将我的代码的早期版本与我发布的代码进行比较,并将一段旧代码替换为新代码(“奇怪地”工作)之后,我意识到问题全部归因于这个问题 - sureVerification的类型(读取代码要理解)是char,在验证表达式中我写了1(相当于“是”作为int。

问题解决了!

© www.soinside.com 2019 - 2024. All rights reserved.