简体   繁体   English

C ++使用cin执行while循环

[英]C++ do while loop with cin

Below is my do while loop with cin There a bit of problem, when user enter Fullname then press enter. 下面是我用cin做while while循环有点问题,当用户输入Fullname然后按回车键。 The pointer will go to next line. 指针将转到下一行。 until user press enter again, then it prompt Email address, how do i make it prompt email address right after full name is entered on my code below 直到用户再次按回车键,然后它会提示电子邮件地址,如何在我的代码输入全名后立即提示电子邮件地址

Terminal Look: 终端外观:

Full Name: John
Email Address: some_email@gmail.com

My test.cpp code: 我的test.cpp代码:

emailAddress= "";
fullname = "";
counter = 0;

if(department_selection!="")
{

while(fullname=="")
{
if(counter>0)
{
//so it wont print twice
cout << "Full Name: ";
}

getline(cin,fullname);
cin.clear();
cin.ignore();
counter++;
}

counter = 0;

while(emailAddress=="")
{
if(counter>0)
{
//so it wont print twice
cout << "Email Address: ";
}

getline(cin,emailAddress);
cin.clear();
cin.ignore();
counter++;
}

}// if department selection not blank

Still same issue. 还是同样的问题。 i need to tab enter once then it prompt for email address. 我需要输入一次,然后提示输入电子邮件地址。

Latest Update: Manage to fix it. 最新更新:管理以修复它。 I make changes to the code and it is this version: 我对代码进行了更改,它是这个版本:

do
{
  if(counter==0)
  {
     //so it wont print twice
     cout << "Full Name: ";
  }
  if(counter>1)
  {
     //so it wont print twice
     cout << "Full Name: ";
  }

  getline(cin,fullname);
  counter++;
} while (fullname=="");

counter = 0;

do
{
  if(counter==0)
  {
     //so it wont print twice
     cout << "Email Address: ";
  }
  if(counter>1)
    {
         cout << "Email Address: ";
    }

  getline(cin,emailAddress);
  counter++;
} while (emailAddress=="");

Instead of if(counter>0) use if(counter==0) 而不是if(counter>0)使用if(counter==0)

My working test app: 我的工作测试应用:

int counter = 0;
string fullname, emailAddress;
do
{
  if(counter==0)
  {
     //so it wont print twice
     cout << "Full Name: ";
  }

  getline(cin,fullname);
  counter++;
} while (fullname=="");

counter = 0;

do
{
  if(counter==0)
  {
     //so it wont print twice
     cout << "Email Address: ";
  }

  getline(cin,emailAddress);
  counter++;
} while (emailAddress=="");

Check for length and then @. 检查长度,然后检查@。

 do
    {
    ...
    }while(fullname.length()<2);

    do
    {
    ...
    }while(emailAddress.length()<3||emailAddress.find("@")==string::npos);

使用clear()和ignore()函数也可以忽略已存储在输入中的单词

Define a function to avoid repeating code: 定义一个函数以避免重复代码:

#include <iostream>
#include <string>
#include <limits>

using namespace std;

string get_input(const std::string& prompt)
{
    string temp;
    cout << prompt;
    while (!getline(cin, temp) || temp.empty()) {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    }
    return temp;
}

int main()
{
    string fullname = get_input("Full Name: ");
    string email = get_input("Email Adress: ");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM