gpt4 book ai didi

c++ - 为什么 C++ 这样做?

转载 作者:行者123 更新时间:2023-11-30 01:10:27 27 4
gpt4 key购买 nike

没有为此显示任何错误,但出于某种原因,在我从 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).

最佳答案

您陷入了使用 cin.get(); 暂停程序时经常出现的陷阱。当您使用它时,您需要确保输入缓冲区为空,这样代码实际上会阻塞,直到读入一个字符。

在流中留下输入的原因是 cin >> y; 在输入流中留下了换行符(按回车键)。为了确保输入流为空,您可以使用

std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n')

如果输入流中没有任何内容,这将暂停程序

有关暂停程序的其他方法,请参阅:Preventing console window from closing on Visual Studio C/C++ Console application

关于c++ - 为什么 C++ 这样做?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37664198/

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