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?

Threaded View

  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 02:20 PM. Reason: Resolved.

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center