Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Please Help.....

Hadi RehmanHadi Rehman Member Posts: 1

this is my Code

include "stdafx.h"

include

include

using namespace std;

bool ask() {

cout << " Enter Y/N " << endl;
char response;
cin >> response;
return (response == 'y') || (response == 'Y');

}

int main() {
bool result;
const int length = 2;

do
{
    for (int i = 1; i <= length; i++)
    {
        cout << " Enter Name "; // ask name 
        string ASK;
        getline(cin, ASK);
        cout << ASK << endl;
        }
    result = ask(); 


} while(result);

}

whenever i enter name and then it ask for return value whenever i enter true value 'Y' its starts loop again but ignores first loop like this

Enter name
Enter name John
John
Enter Y/N>

Tagged:

Comments

  • Tyson BlairTyson Blair Member Posts: 35
    bool ask() {
    
        cout << " Enter Y/N " << endl;
        char response;
        cin >> response;
    if (response == 'y' || response == 'Y')
    {
        return true;
    }
    else 
    {
        return false;
    }
        //return (response == 'y') || (response == 'Y');
    // if we get here something went wrong eg entered 'n','N','s', etc.
        return false;    
    }
    
  • B00B00 Member Posts: 106 ✭✭✭

    @Tyson Blair your code does not improve author's code, it does exactly the same.

Sign In or Register to comment.