Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/161.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C+中是否有合适的等待函数+;? 在C++中我学到的第一件事就是 #include <iostream> int main() { std::cout<<"Hello, World!\n"; return 0; }_C++_Wait - Fatal编程技术网

C+中是否有合适的等待函数+;? 在C++中我学到的第一件事就是 #include <iostream> int main() { std::cout<<"Hello, World!\n"; return 0; }

C+中是否有合适的等待函数+;? 在C++中我学到的第一件事就是 #include <iostream> int main() { std::cout<<"Hello, World!\n"; return 0; },c++,wait,C++,Wait,ase 当我需要创建一堆小的测试程序时,这变得很乏味,最终我简单地把while(true)在我的大多数测试程序的最后,只是为了让我能看到结果。我可以使用更好的等待函数吗?您可以使用sleep()或usleep() 您可以要求用户在关闭程序之前按enter键。。。像这样的东西很管用 #include <iostream> int main() { std::cout << "Hello, World\n"; std::cin.ignore(); return 0

ase

当我需要创建一堆小的测试程序时,这变得很乏味,最终我简单地把
while(true)在我的大多数测试程序的最后,只是为了让我能看到结果。我可以使用更好的等待函数吗?

您可以使用sleep()或usleep()


您可以要求用户在关闭程序之前按enter键。。。像这样的东西很管用

#include <iostream>
int main()
{
  std::cout << "Hello, World\n";
  std::cin.ignore();
  return 0;
}
#包括
int main()
{

std::cout与批处理程序等效的是

#include<iostream>
int main()
{
    std::cout<<"Hello, World!\n";
    std::cin.get();
    return 0;
}
#包括
int main()
{
std::cout语法:

无效睡眠(无符号秒)

sleep()暂停执行一段时间(秒)。 调用sleep时,当前程序暂停执行的秒数由参数seconds指定。间隔仅精确到百分之一秒或操作系统时钟的精度,以精度较低的为准

这个例子应该说明:

#include <dos.h>
#include <stdio.h>
#include <conio.h>

int main()
{
   printf("Message 1\n");
   sleep(2); //Parameter in sleep is in seconds
   printf("Message 2 a two seconds after Message 1");
   return 0;
}

请注意,winapi提供的函数中的参数以毫秒为单位,因此上面代码段的sleep行看起来像“sleep(2000);”

getchar()提供了一个简单的答案(等待键盘输入)。 调用Sleep(毫秒)以在退出前休眠。

< P>第二件要学习的事情(一种认为这应该是第一种)是你的操作系统和编译器/链接器标志和开关的命令行界面。

< P>显示文本的窗口的出现和消失是运行程序的一个特征,而不是C++。
在持久的命令行环境中运行,或在程序中包含窗口支持,或使用
睡眠
或等待输入,如其他答案所示。

可以更轻松地编写您所拥有的内容。 而不是:

#include<iostream>
int main()
{
    std::cout<<"Hello, World!\n";
    return 0;
}
#包括
int main()
{

std::cout好吧,这是一篇老文章,但我只想提出一个问题——以后可能有人会发现它很有用:

在返回main()之前添加'cin.get();'函数似乎总是在打印结果之前阻止程序退出:请参阅下面的示例代码:

int main(){ 字符串fname,lname

  //ask user to enter name first and last name
  cout << "Please enter your first name: ";
  cin >> fname;

  cout << "Please enter your last name: ";
  cin >> lname;     
  cout << "\n\n\n\nyour first name is: " << fname << "\nyour last name is: " 
  << lname <<endl;

  //stop program from exiting before printing results on the screen
  cin.get();
  return 0;
//要求用户输入姓名和姓氏
cout>fname;
cout>lname;

cout在main中的return语句之前,插入以下代码:

系统(“暂停”)

这将保持控制台,直到您按下一个键

#include<iostream>
#include<string>

using namespace std;

int main()
{
    string s;
    cout << "Please enter your first name followed by a newline\n";
    cin >> s;
    cout << "Hello, " << s << '\n';
    system("pause");
    return 0; // this return statement isn't necessary
}
#包括
#包括
使用名称空间std;
int main()
{
字符串s;
cout>s;

cout请注意,上面的代码是在code::Blocks 12.11和Visual Studio 2012上测试的
在Windows7上

要强制您的节目停止或等待,您有几个选项:


  • 睡眠(无符号整数)
该值必须是以毫秒为单位的正整数。 这意味着,如果您想让程序等待2秒钟,请输入2000

下面是一个例子:

#include <iostream>     //for using cout
#include <stdlib.h>     //for using the function sleep

using namespace std;    //for using cout

int main(void)         
{
   cout << "test" << endl;
   sleep(5000);         //make the programme waiting for 5 seconds
   cout << "test" << endl;
   sleep(2000);         // wait for 2 seconds before closing

   return 0;
}
对于那些在使用sleep时收到错误消息或出现问题的人,请尝试用_sleep或sleep来代替它,尤其是在Code::Bloks上。
如果您仍然遇到问题,请尝试在代码开头添加此库中的一个

#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <dos.h>
#include <windows.h>
#包括
#包括
#包括
#包括
#包括

  • 系统(“暂停”)
windows控制台应用程序上的一个简单的“Hello world”程序可能会在您看到任何东西之前关闭。在这种情况下,您可以使用系统(“暂停”)

#包括
使用名称空间std;
内部主(空)
{

cout事实上,与其他答案相反,我相信OP的解是最优雅的

以下是使用外部
.bat
包装器的好处:

  • 应用程序显然在等待用户输入,所以它已经做了您想要做的事情
  • 您不会因为笨拙的调用而使代码混乱。谁应该等?
    main()
  • 您不需要处理跨平台问题-请参见此处有多少人建议系统(“暂停”)
  • 否则,要在黑盒测试模型中以自动方式测试可执行文件,您需要模拟
    enter
    keypress(除非您执行脚注中提到的操作)
  • 也许最重要的是——如果任何用户希望通过终端运行您的应用程序(
    cmd.exe
    ,在Windows平台上),他们不想等待,因为他们无论如何都会看到输出。使用
    .bat
    包装技术,他们可以决定是否运行
    .bat
    (或
    .sh
    )或者直接运行可执行文件
  • 关注最后两点-对于任何其他技术,我希望程序至少提供
    --no wait
    开关,这样,作为用户,我可以将应用程序用于所有类型的操作,如管道输出、将其与其他程序链接等。这是正常CLI工作流的一部分,并在结束时添加等待你已经进入了一个终端,这会妨碍你,破坏你的用户体验


    由于这些原因,IMO <代码> .BAT解决方案是最好的。

    <代码>睡眠>代码>、Windows >代码>睡眠>代码>、Windows >代码>系统(“暂停”)/C++ >代码> CIN>……(…)/代码>……甚至有一个DOS代码> GETCHER()/<代码>,从大约20世纪20年代末开始。

    请不要这样做。

    所有这些解决方案都无法通过我的团队的代码审查。这意味着,如果您提交此代码以包含在我们的产品中,您的提交将被阻止,并且您将被告知去寻找另一个解决方案。(有人可能会说,当你只是一个业余爱好者时,事情并没有那么严重,但我建议,在你的宠物项目中养成良好的习惯,这将使你成为商业组织中一名有价值的专业人士,并让你继续被聘用。)

    保持控制台为w
    #include<iostream>
    int main()
    {
        std::cout<<"Hello, World!\n";
        system("PAUSE");
        return 0;
    }
    
      //ask user to enter name first and last name
      cout << "Please enter your first name: ";
      cin >> fname;
    
      cout << "Please enter your last name: ";
      cin >> lname;     
      cout << "\n\n\n\nyour first name is: " << fname << "\nyour last name is: " 
      << lname <<endl;
    
      //stop program from exiting before printing results on the screen
      cin.get();
      return 0;
    
    #include<iostream>
    #include<string>
    
    using namespace std;
    
    int main()
    {
        string s;
        cout << "Please enter your first name followed by a newline\n";
        cin >> s;
        cout << "Hello, " << s << '\n';
        system("pause");
        return 0; // this return statement isn't necessary
    }
    
    #include <iostream>     //for using cout
    #include <stdlib.h>     //for using the function sleep
    
    using namespace std;    //for using cout
    
    int main(void)         
    {
       cout << "test" << endl;
       sleep(5000);         //make the programme waiting for 5 seconds
       cout << "test" << endl;
       sleep(2000);         // wait for 2 seconds before closing
    
       return 0;
    }
    
    sleep(5);
    
    #include <stdio.h>
    #include <time.h>
    #include <unistd.h>
    #include <dos.h>
    #include <windows.h>
    
    #include <iostream>    
    
    using namespace std;   
    
    int main(void)         
    {
        cout << "Hello world!" << endl;
    
        system("PAUSE");
    
        return 0;
    }
    
    #include <cstdlib>
    
    #include <iostream>     
    
    using namespace std;    
    
    int main(void)         
    {
        cout << "Hello world!" << endl;
    
        cin.ignore();
    
        return 0;
    }
    
    #include <iostream>     
    
    using namespace std;    
    
    int main(void)         
    {
        cout << "Hello world!" << endl;
    
        cin.get();
    
        return 0;
    }
    
    #include <iostream>     
    #include <conio.h>    //for using the function getch()
    
    using namespace std;    
    
    int main(void)
    {
    
        cout << "Hello world!" << endl;
    
        getch();
    
        return 0;
    }
    
    #include <chrono> // std::chrono::microseconds
    #include <thread> // std::this_thread::sleep_for
    std::this_thread::sleep_for(std::chrono::microseconds{});