gpt4 book ai didi

c++ - main() 中的函数在前一个函数完成之前执行(c++ Xcode)

转载 作者:行者123 更新时间:2023-11-27 23:19:33 26 4
gpt4 key购买 nike

正如标题所说,我遇到了函数在函数完成之前执行的问题。这是一个问题,因为它打乱了我的程序流程。我的 main() 函数如下所示:

int main()
{
FilePath1();
FilePath2();

VideoConfig1();
VideoConfig2();

VideoRecord();

return 0;
}

FilePath 函数如下所示:

void FilePath1()
{
cout << "Please enter the full filepath for where you want to save the first video: ";
cin >> filepath1;
cin.ignore();

}

问题是 VideoConfig1() 直接在 FilePath1() 之后启动,与 FilePath2() 同时启动。有谁知道为什么会这样?

我试过使用 waitKey() 作为 VideoConfig1() 的初始化,但没有效果。

编辑:

程序应该按如下方式工作:

  • Run FilePath1()
    • get the user to enter the filepath for video1 to be saved
  • Run Filepath2()
    • get the user to enter the filepath for video2 to be saved
  • Run VideoConfig1()
  • Run VideoConfig2()
  • Run VideoRecord()

两个 FilePath 程序在终端中运行,而其他程序则打开各自的视频窗口。该程序目前正在做的是:

  • Run FilePath1()
    • get the user to enter the filepath for video1 to be saved
  • Run Filepath2() AND VideoConfig1()
    • get the user to enter the filepath for video2 to be saved
    • if FilePath2 hasn't been completed by the time VideoConfig1 has, crash entire program
  • Run VideoConfig2()
  • Run VideoRecord()

最佳答案

既然你说你正在输入带空格的文件路径,这就是我认为正在发生的事情:

FilePath1();
FilePath2();
...

void FilePath1()
{
cout << "Please enter the full filepath for where you want to save the first video: ";
cin >> filepath1;

// user enters the following sans quotes: "c:\my files\file.ext"
// filepath1 becomes "c:\my"

cin.ignore();
// cin.ignore() with no params will clear ONE character from the stream
// the space is removed from the input stream

}

void FilePath2()
{
cout << "Please enter the full filepath for where you want to save the first video: ";
cin >> filepath2;

// at this point, there is still data in the input buffer,
// so filepath2 becomes: "files\file.ext" and execution continues
// without waiting on user input

cin.ignore();
// the return is removed from the input stream, input stream is now empty

}

正如您所建议的,您希望使用 getline,因为您希望输入中有空格。这是清除 cin 与 getline 的一个很好的引用:http://www.cplusplus.com/doc/tutorial/basic_io/

关于c++ - main() 中的函数在前一个函数完成之前执行(c++ Xcode),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14271627/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com