1

Below is my do while loop with cin There a bit of problem, when user enter Fullname then press enter. 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: [email protected]

My test.cpp code:

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=="");
1
  • 1
    You should use a variable that is local to the scope of the do/while loops. I assume counter is declared outside of both while loops. Commented Aug 20, 2012 at 19:35

4 Answers 4

2

Instead of if(counter>0) use 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=="");
Sign up to request clarification or add additional context in comments.

Comments

1

Check for length and then @.

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

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

3 Comments

What does input sanitation have to do with the prompt not appearing when it should?
@steveg89:- OP's code has no error.support.microsoft.com/… ..what should I then suggest :)
It does have an error. Look at my post and re-read his question. He was getting an extra getline before getting his prompt, because his condition for showing the prompt was wrong.
0

Use the clear() and ignore() functions too to ignore words already stored in the input

6 Comments

That's more of a comment than an answer to his question.
It should resolve the problem - what more does an answer need?
Hmm how should i change it to make it work with your clear and ignore.
Once you've got the input stored, use like this:
var.clear(); or var.ignore(); for the string variable storing the input
|
0

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: ");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.