简体   繁体   English

做/循环不循环时

[英]Do/While loop not looping

I am a beginner in C++ and I was doing a do/while loop exercise and I am having trouble with it recognising the condition let alone it not looping properly. 我是C ++的初学者,当时我正在做一个do / while循环练习,并且在识别条件(更不用说它不能正确循环)的情况下遇到了麻烦。 Can you guys give me a good foundation on how a simple problem like this is solved? 你们能为我解决此类简单问题打下良好基础吗? I want to try and use a string to fulfill the condition of the do/while loop. 我想尝试并使用一个字符串来满足do / while循环的条件。

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main ()
{
    double mean = 0;
    string continuer;
    do
    {
        cout << "Do you want to proceed?" << endl;
        getline (cin, continuer);
        cout << "something" << endl;
        cin >> mean;
    }
    while (continuer == "Y" || continuer == "y");

    return 0;
}

What I gather from your question and comment, you want to iterate through the loop at user's will. 我从您的问题和评论中收集的信息是,您想按照用户的意愿遍历循环。

You just want a char variable for that, like this. 您只需要一个char变量,就像这样。

string input ;
int number = 0 ;

do
{
  cout << "Please enter a number" << endl ;
  cin >> number ;
  cout << "If you want to continue, Press Y" << endl ;
  cin >> input ;

} while (input == "Y" || input == "y") ;

This do-while loop will execute at least one time, because the condition gets checked at the end of the loop execution. 此do-while循环将至少执行一次,因为条件是在循环执行结束时检查的。 So even if the user does not press Y when asked the first time, this loop would have been executed once. 因此,即使用户在第一次询问时未按Y,该循环也将执行一次。 After that, it will go on as long as the condition is fulfilled. 在那之后,只要条件满足,它就会继续。

Learn more about the do-while loop here. 在此处了解有关do-while循环的更多信息。

http://www.cplusplus.com/doc/tutorial/control/ http://www.cplusplus.com/doc/tutorial/control/

What do you see? 你看到了什么? The body of the loop should be executed at least once. 循环的主体应至少执行一次。 Does that happen? 会发生吗?

Also, Continuer can be longer than one character, for instance "Y\\n". 此外,Continuer可以长于一个字符,例如“ Y \\ n”。 Do test for that. 做测试。

Here is what I would do: 这是我会做的:

#include <string>
#include <sstream>
using namespace std;



    int main ()
    {
        double number = 0;
        string continuer;
        int loop = 0
        do
        {

            cout << "Do you want to proceed?" << endl;
            getline (cin, number);
            cout << "something" << endl;
            cin>> mean;
            getline (cin, continuer);

            cout << "Your answer was '" << continuer << "'." << endl;
            loop = strcmp("Y", continuer);
            if (loop != 0) strcmp("y", continuer);
            if (loop == 0) cout << "Your choice is to stop." << endl;
            else cout << "Your choice is to continue." << endl;
        } while (loop == 0);
        cout << "Bye" << endl;
        return 0;
    }

Be as explicit as you can untill you are confident enough in the language and the algorithm you are working with. 尽可能明确,直到您对使用的语言和算法足够自信为止。 It is easier to see what is happening and when it works it is easy to remove the 'cout' lines. 更容易看到正在发生的事情,当它起作用时,很容易删除“ cout”行。

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

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