没有显示错误,但出于某种原因,在我从cin
输入整数之后,窗口关闭忽略了我的答案,这应该确定它是否说“不,这不是我的最爱号码”或继续执行功能。
这是我的代码:
#include <iostream>
using namespace std;
void printSomething(int x); //Called Prototyping, means that you don't have to have the function on top.
int main() {
cout << "Yo,\n";
int a = 16;
int b = 12;
int y;
if(a > b) {
cout << "Opening Function.\n";
cout << "\nWhat is my fav number, I forgot: ";
cin >> y;
if(y == 10) { //If my fav number is 10 then continue.
printSomething(y); //Calls the function. (y) is the interger called at (int x)
}
else {
cout << "No that isn't my fav number";
}
}
}
void printSomething(int x) { //This is the function.
cout << "\nHey! I am the function!\n#########" << endl;
cout << "I iz lonely.";
cin.get(); //Meant to keep the terminal open.
}
这是输出:
'Things.exe' (Win32): Loaded 'C:\Users\Matt\Documents\Visual Studio 2015\Projects\Things\Debug\Things.exe'. Symbols loaded.
'Things.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. Cannot find or open the PDB file.
'Things.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. Cannot find or open the PDB file.
'Things.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. Cannot find or open the PDB file.
'Things.exe' (Win32): Loaded 'C:\Windows\SysWOW64\vcruntime140d.dll'. Cannot find or open the PDB file.
'Things.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcp140d.dll'. Cannot find or open the PDB file.
'Things.exe' (Win32): Loaded 'C:\Windows\SysWOW64\advapi32.dll'. Cannot find or open the PDB file.
'Things.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ucrtbased.dll'. Cannot find or open the PDB file.
'Things.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msvcrt.dll'. Cannot find or open the PDB file.
'Things.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sechost.dll'. Cannot find or open the PDB file.
'Things.exe' (Win32): Loaded 'C:\Windows\SysWOW64\rpcrt4.dll'. Cannot find or open the PDB file.
'Things.exe' (Win32): Loaded 'C:\Windows\SysWOW64\sspicli.dll'. Cannot find or open the PDB file.
'Things.exe' (Win32): Loaded 'C:\Windows\SysWOW64\cryptbase.dll'. Cannot find or open the PDB file.
'Things.exe' (Win32): Loaded 'C:\Windows\SysWOW64\bcryptprimitives.dll'. Cannot find or open the PDB file.
The thread 0x2410 has exited with code -1073741510 (0xc000013a).
The thread 0x12a8 has exited with code -1073741510 (0xc000013a).
The thread 0x2b18 has exited with code -1073741510 (0xc000013a).
The thread 0x264c has exited with code -1073741749 (0xc000004b).
The program '[92] Things.exe' has exited with code -1073741510 (0xc000013a).
答案 0 :(得分:3)
使用cin.get();
暂停程序时,您经常陷入陷阱。当你使用它时,你需要确保输入缓冲区为空,这样代码实际上会阻塞,直到读入一个字符。
流中还有输入的原因是cin >> y;
在输入流中离开换行符(从按Enter键)。为了确保输入流为空,您可以使用
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n')
如果输入流中没有任何内容
,这将暂停程序有关暂停程序的其他方法,请参阅:Preventing console window from closing on Visual Studio C/C++ Console application
答案 1 :(得分:2)
在Visual Studio中,通过 Ctrl + F5 运行程序。
或者从命令解释器实例运行它。
或者,通过Visual Studio调试器运行它(就像你一直在做的那样),但在like_time
的{{1}}大括号上有一个断点。
您不需要
}
。
在这种情况下它不起作用,因为输入缓冲区中至少有一个换行符留在上一个输入操作中,而main
只消耗了这个字符,没有进一步的麻烦。