简体   繁体   中英

C++ for loop skips cin.get()

In this for loop the cin.get(); is being skipped every other time. I have no idea why it is being skipped. Any help would be greatly appreciated.

Code:

for(Pointer = StartPointer; Pointer < EndPointer; Pointer += MemInfo.RegionSize)
{

    VirtualQueryEx(hProc, (LPCVOID)(Pointer), &MemInfo, sizeof(MemInfo));
    cout << "MemInfo AllocationBase: " << MemInfo.AllocationBase << endl;
    cout << "MemInfo AllocationProtect: " << MemInfo.AllocationProtect << endl;
    cout << "MemInfo BaseAddress: " << MemInfo.BaseAddress << endl;
    cout << "MemInfo Protect: " << MemInfo.Protect << endl;
    cout << "MemInfo RegoinSize: " << MemInfo.RegionSize << endl;
    cout << "MemInfo State: " << MemInfo.State << endl;
    cout << "MemInfo Type: " << MemInfo.Type << endl;
    cout << "MemInfo Size: " << sizeof(MemInfo) << endl;
    cout << "Starter pointer is: " << StartPointer << endl;
    cin.get();
}

Example output between cin.get();

MemInfo AllocationBase: 00000000
MemInfo AllocationProtect: 0
MemInfo BaseAddress: 00000000
MemInfo Protect: 1
MemInfo RegoinSize: 65536
MemInfo State: 65536
MemInfo Type: 0
MemInfo Size: 28
Starter pointer is: 0
MemInfo AllocationBase: 00010000
MemInfo AllocationProtect: 4
MemInfo BaseAddress: 00010000
MemInfo Protect: 4
MemInfo RegoinSize: 65536
MemInfo State: 4096
MemInfo Type: 262144
MemInfo Size: 28
Starter pointer is: 0

cin.get() doesn't get the '\\n' you typed~

try to use,

string str;
getline(cin, str) 

instead of cin.get()

or add a getchar() after cin.get()

Since you are on Windows:

#include <conio.h>

// .. your other stuff

_getch();

cin.get() is going to grab whatever is in the input buffer (and if you pressed enter, or space, or pretty much breathed on the keyboard, it will have something), or the EOF if there is nothing, so it will always return.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM