Cin Problem, I think Overflow?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Cin Problem, I think Overflow?

  1. #1
    Join Date
    Jan 2011
    Location
    Orange County, CA
    Posts
    82

    Resolved Resolved.

    I'm making a quick program for my English class's movie that involves me hacking into a bank and stealing money (of course this doesn't actually hack anything), but I haven't programmed in C++ in a long time and I don't remember how to fix this problem here:
    Code:
    int main()
    {
        system ("TITLE BSP");
        system ("COLOR 0A");
    
        cout << "Credit Card Number: ";
        getline(cin, number); // Either Here...
        hackingSimulator(0, 10);
        displayInformation();
    }
    
    void takeFunds()
    {
        int amount = 0;
        char ch;
    
        system("CLS");
    
        cout << "Amount: ";
        cin >> amount; // And/Or Here
        funds += amount;
        hackingSimulator(7, 10);
    
        displayInformation();
    }
    When I input anything in for number, such as:
    Code:
    1
    1234
    1234 1234 1234 1234 // Similar to what will be entered in the movie (with spaces included)
    ...it will also show up for amount and will not give me a prompt to enter any value. Does anyone know how to fix this? I've tried researching it by looking up a cin reference guide and Googling it, and I found some similar problems but no solutions that worked for me. I'm not sure if I'm missing something in the reference or if the problem is something else entirely. Any and all help will be appreciated, thanks in advance.

    PS: Here's the full program if you need it:
    (Please excuse my lazy programming, but this is all basically a dummy program with no real function other than being filmed which is why I used a lot of shortcuts like the goto command, and also why I have a lot of menu options but few menu functions)
    Code:
    #include <cstdlib>
    #include <iomanip>
    #include <iostream>
    #include <string>
    #include <windows.h>
    
    using namespace std;
    
    void hackingSimulator(int, int);
    void displayInformation();
    void displayMenu();
    void checkStatus();
    void takeFunds();
    void displayWarning();
    
    const char PTR[] = "=> ";
    const char SPC[] = "   ";
    float funds = -8013.68;
    string number = "";
    
    int main()
    {
        system ("TITLE BSP");
        system ("COLOR 0A");
    
        cout << "Credit Card Number: ";
        getline(cin, number);
        hackingSimulator(0, 10);
        displayInformation();
    }
    
    void hackingSimulator(int start, int end)
    {
        string hackList[10] = {"Loading Command", "Encrypting Connection", "Purging System", "Bypassing First Firewall",
                               "Bypassing Secondary Firewall", "Bypassing Backup Firewall", "Gathering Information",
                               "Decrypting Information", "Compiling Information", "Compressing Information"};
        int time = 1;
    
        system("CLS");
    
        for(int i = start;i < end;i++)
        {
            cout << hackList[i];
    
            if(i == 0)
              time = 2400;
            else if(i > 1 && i < 3)
              time = 7200;
            else if(i > 2 && i < 6)
              time = 9000;
            else
              time = 3600;
    
            for(int q = 0;q < time / 300;q++)
            {
                cout << ".";
                Sleep(100);
            }
            system("CLS");
        }
    
    }
    
    void displayInformation()
    {
        system("CLS");
    
        cout << setw(20) << "Wells Fargo Bank Member\n" << endl;
        cout << setw(20) << "Routing Number: " << "01679434973" << endl;
        cout << setw(20) << "Account Number: " << "2467297642" << endl;
        cout << setw(20) << "Account Password: " << "iHEARTdoritos" << endl;
        cout << setw(20) << "Name: " << "Curtis Spikoli" << endl;
        cout << setw(20) << "Address: " << "94673 Second Street, Cypress, CA, USA" << endl;
        cout << setw(20) << "Email: " << "awesomespikolio@hotmail.com" << endl;
        cout << setw(20) << "Email Password: " << "IaminLOVEwithDORITOS" << endl;
        cout << setw(20) << "Credit Card Number: " << number << endl;
        cout << setw(20) << "PIN Number: " << "1350" << endl;
        cout << setw(20) << "Credit Card ID: " << "467" << endl;
        cout << "------------------------------------------------------------" << endl;
        cout << setw(20) << "Current Funds: " << setprecision(2) << fixed << showpoint << funds << endl << endl;
        system("PAUSE");
    
        displayMenu();
    }
    
    void displayMenu()
    {
        char sel = 0;
    
        _sel:
        system("CLS");
        cout << (!sel ? PTR : SPC) << "List Active Credit Cards" << endl;
        cout << ((sel == 1) ? PTR : SPC) << "List Deactivated Credit Cards" << endl;
        cout << ((sel == 2) ? PTR : SPC) << "List Lost/Stolen Credit Cards" << endl;
        cout << ((sel == 3) ? PTR : SPC) << "List Pending Credit Cards" << endl;
        cout << ((sel == 4) ? PTR : SPC) << "Report Credit Card" << endl;
        cout << ((sel == 5) ? PTR : SPC) << "Display Account Information" << endl;
        cout << ((sel == 6) ? PTR : SPC) << "Check Account Status" << endl;
        cout << ((sel == 7) ? PTR : SPC) << "Change Account Password" << endl;
        cout << ((sel == 8) ? PTR : SPC) << "Freeze Account" << endl;
        cout << ((sel == 9) ? PTR : SPC) << "Mark Account as Flagged" << endl;
        cout << ((sel == 10) ? PTR : SPC) << "Close Account" << endl;
        cout << ((sel == 11) ? PTR : SPC) << "Give Funds" << endl;
        cout << ((sel == 12) ? PTR : SPC) << "Take Funds" << endl;
        cout << ((sel == 13) ? PTR : SPC) << "Exit Account" << endl;
        Sleep(100);
    
        while (true)
        {
            if (GetKeyState(VK_UP) < 0)
            {
                sel = (!sel ? 13 : (sel - 1));
                goto _sel;
            }
            if (GetKeyState(VK_DOWN) < 0)
            {
                sel = ((sel == 13) ? 0 : (sel + 1));
                goto _sel;
            }
            if (GetKeyState(VK_RETURN) < 0)
                break;
        }
    
        switch(sel)
        {
            case 5:  displayInformation();
                     break;
            case 6:  checkStatus();
                     break;
            case 11: takeFunds();
                     break;
            case 13: exit(0);
                     break;
            default: displayWarning();
                     break;
        }
    }
    
    void checkStatus()
    {
        system("CLS");
    
        cout << setw(17) << "::Flags::" << endl;
        cout << setw(7) << "Black: " << "Federal Government Watching" << endl;
        cout << setw(7) << "Red: " << "Local Government Watching" << endl;
        cout << setw(7) << "White: " << "All Clear\n\n" << endl;
        cout << setw(23) << "Account Flag: " << "Black" << endl;
        cout << setw(23) << "Estimated FBI Arrival: " << "T-20 Seconds and Counting..." << endl;
        system("PAUSE");
    
        displayMenu();
    }
    
    void takeFunds()
    {
        int amount = 0;
        char ch;
    
        system("CLS");
    
        cout << "Amount: ";
        cin >> amount;
        funds += amount;
        hackingSimulator(7, 10);
    
        displayInformation();
    }
    
    void displayWarning()
    {
        system("CLS");
    
        cout << "You picked the wrong choice >:/" << endl;
        system("PAUSE");
    
        displayMenu();
    }
    Last edited by iiSoMeGuY 7x; February 14th, 2013 at 01:20 PM. Reason: Resolved.

  2. #2
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,327

    Re: Cin Problem, I think Overflow?

    Nasty! The problem you've got is nothing to do with the c++ I/O system but the fact that you are trying to combine two different input systems - c++ cin and Windows input using GetkeyState() and the two don't mix very easily!

    When you press the up-arrow (or down arrow or any other special key) then GetkeyState correctly tells the state of the tested key BUT the key still stays in the input buffer and is not processed. If this were a windows program, then windows would extract the key from the input buffer and send messages. But as this is not a windows progam, it doesn't. So up-arrow and down-arrow etc are still in the input buffer and then you do a cin >> to get input from the console. cin finds the arrows and treats them like it would if you were using them at the command prompt ie goes back and forth through the input history. It then finds the CR and uses whatever is in the input buffer as input.

    If you want to use special keys in a console program you have to do it another way. In your case this is easiest using the special console input function getch(). This will read the next character from the input WITHOUT performing any special processing for special characters. The special characters you use all produce 2 input characters 0xe0 followed by 0x48 for up and 0x50 for down. So your display menu loop could be something like

    Code:
        while (true) {
    	char ch = getch();
    
           //If have special key code then get next char as they come in pairs
    	if (ch == 0xe0) {
                  ch = getch();
    	}
    
            //if (GetKeyState(VK_UP) < 0)
    	if (ch == 0x48) 
            {
                sel = (!sel ? 13 : (sel - 1));
                goto _sel;
            }
    
            //if (GetKeyState(VK_DOWN) < 0)
    	if (ch == 0x50)
            {
                sel = ((sel == 13) ? 0 : (sel + 1));
                goto _sel;
            }
            //if (GetKeyState(VK_RETURN) < 0)
    	if (ch == 0x0d)
                break;
    	}
    You'll need to insert for the definition of getch().

    #include <conio.h>

    This seems to work OK now on my system.

  3. #3
    Join Date
    Jan 2011
    Location
    Orange County, CA
    Posts
    82

    Re: Cin Problem, I think Overflow?

    Quote Originally Posted by 2kaud View Post
    Nasty! The problem you've got is nothing to do with the c++ I/O system but the fact that you are trying to combine two different input systems - c++ cin and Windows input using GetkeyState() and the two don't mix very easily!
    Now you sir, are my new best friend! I had no idea that was the issue, no wonder I couldn't get anything else to work! Thank you so much, the program works perfectly now and the movie shall continue

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,327

    Re: Cin Problem, I think Overflow?

    Pleased to be of help.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center