![](/img/trans.png)
[英]Why is Xcode allocating unnecessary bytes before main function C++?
[英]Functions in main() are executing before the previous function is complete (c++ Xcode)
就像标题中所说的那样,我在功能完成之前先执行功能时遇到了问题。 这是一个问题,因为它弄乱了我的程序流程。 我的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();
}
问题在于VideoFile1()与FilePath2()同时在FilePath1()之后直接启动。 有谁知道为什么会这样?
我尝试使用waitKey()作为VideoConfig1()的初始化,但是没有任何效果。
编辑:
该程序应按如下方式工作:
- 运行FilePath1()
- 让用户输入要保存的video1的文件路径
- 运行Filepath2()
- 让用户输入要保存的video2的文件路径
- 运行VideoConfig1()
- 运行VideoConfig2()
- 运行VideoRecord()
这两个FilePath程序在终端中运行,而其他程序则打开各自的视频窗口。 该程序当前正在执行的操作是:
- 运行FilePath1()
- 让用户输入要保存的video1的文件路径
- 运行Filepath2()和VideoConfig1()
- 让用户输入要保存的video2的文件路径
- 如果在VideoConfig1之前FilePath2尚未完成,则使整个程序崩溃
- 运行VideoConfig2()
- 运行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 vs getline的很好参考: http ://www.cplusplus.com/doc/tutorial/basic_io/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.