Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3333306
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:46:55+00:00 2026-05-17T23:46:55+00:00

I have been asking in previous questions about how to get multiple string inputs

  • 0

I have been asking in previous questions about how to get multiple string inputs to control the flow of the program, using if else statements. and everything worked out fine.

the code for the YES and NO is fantastic and i’m very happy, except I built a test rig in the form of a function that is called again and again if the user doesn’t type exit.

The reason i did this was so i could just try all different combinations of words to see how durable the std::string is, whats the limits and such. the only issue is that after the code loops once (it goes back to main and into the function YESNO again) then the input goes the yes or no part as its meant to but instead of looping for a second or third time the program closes.

heres the code:

#include <iostream>
#include <algorithm>
#include <cctype>
#include <string>

//the above headers are for the control flow code (transforms i think)

using namespace std;


int YESNO (int var);


int main()
{
    int loop=0;

    if (loop == 0)
    {
        YESNO(0);
        loop=YESNO(0);

    }
    else
    {
        cout << "\n " << endl;
    }
    return 0;
}

int YESNO (int var)
{
    cout << "This is a simple yes or no program control flow test" << endl;
    cout << "Please enter yes or no in different ways to test the code, many thanks!!!" << endl;
    cout << "\nYES or NO or exit?" << endl;
    string input ="";
    cin >> input;
    //all good, the above i know how todo

    transform (input.begin(), input.end(), input.begin(), tolower);

    if ((std::string::npos != input.find( "yes" )) || (std::string::npos != input.find( "y"))  )
    {
        cout << "You entered yes!! " << endl;
    }
    else if ((std::string::npos != input.find( "no" )) || (std::string::npos != input.find( "n"))  )
    {
        cout << "You entered no!! " << endl;
    }
    else if  (std::string::npos != input.find( "exit" ))
    {
        var=1;
    }
    else 
    {
        cout << "ERROR REPEAT..........!!!" << endl;
        var=0;
    }
        return(var);
}

The idea was that once the user types in yes or no then the function automatically repeats so the user (me) can type in yes or no again to see how far we can push std::string, and typing exit will cause the program not to loop as loop will no longer equal 0 and the program will close.

the loop works but the function doesn’t behave correctly, and its goes straight to exist after running cout for the correct response.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:46:56+00:00Added an answer on May 17, 2026 at 11:46 pm

    There are no looping constructs in your program. Instead, you’re invoking YESNO twice via explicit calls.

    Try any of the following:

    // do...while executes the loop body once before testing the condition
    int loop = 0;
    do {
      loop = YESNO(0);
    } while (loop == 0);
    

    or

    // while loops test the condition before executing the loop body
    int loop = 0;
    while (loop == 0) {
      loop = YESNO(0);
    }
    

    or

    // somewhat odd use of "for" which limits the loop variable's scope
    for (int loop = 0; loop == 0; loop = YESNO(0)); // empty loop body
    

    or

    // The best option is probably to omit the loop variable entirely
    while (YESNO(0) == 0); // empty loop body
    

    Some notes on style:

    • Don’t write return(var);, instead just return var;
    • Don’t use CAPS for function or variable names, by convention they represent constants; name your function yesNo or yes_no
    • Don’t use variables names like var or tmp, your variables should describe their purpose and/or content
    • There doesn’t seem to be any need for YESNO to take an argument so it somewhat clutters things up
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.