Matt
Matt

Reputation: 57

Why is C++ Doing this?

There are no errors showing for this but for some reason just after I enter the integer from the cin the window closes ignoring my answer, which should determine if it says "No that isn't my fav number" or continue to a function.

Here is my code:

#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.
}

This is the output:

'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).

Upvotes: 1

Views: 205

Answers (2)

NathanOliver
NathanOliver

Reputation: 180415

You have fallen into a trap that comes up often when using cin.get(); to pause the program. When you use this you need to make sure the input buffer is empty so the code will actually block until a character is read in.

The reason there is input left in the stream is that cin >> y; leaves the newline(from pressing enter) in the input stream. In order to make sure the input stream is empty you can use

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

This will pause the program if there is nothing in the input stream

For other ways to pause your program see: Preventing console window from closing on Visual Studio C/C++ Console application

Upvotes: 3

Cheers and hth. - Alf
Cheers and hth. - Alf

Reputation: 145194

In Visual Studio run the program via Ctrl+F5.

Or run it from a command interpreter instance.

Or, run it via the Visual Studio debugger (as you've been doing) but with a breakpoint on the last } brace of main.


You don't need

cin.get(); //Meant to keep the terminal open.

at all.

It doesn't work in this case because there's at least a newline character left in the input buffer from the previous input action, and this character is just consumed by the get(), without further ado.

Upvotes: 2

Related Questions