fearlessShopkeeper
fearlessShopkeeper

Reputation: 11

Using while>>cin to keep asking user for input in C++

Code for a blackjack card counting program.

the issue is that it does not exit the while loop upon receiving no cin input from the user.

for example) User would input x chars and then hit enter to exit the while loop.

#include<iostream>

using namespace std;

int main(){

    int count = 0;
    char currcard;
    cout<<"Enter cards seen on table: "<<endl;

    while (cin>>currcard)
    {
        switch (currcard)
            {
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
                count++;
                break;

            case '7':
            case '8':
            case '9':
                break;

            case 'A':
            case 'J':
            case 'Q':
            case 'K':
                count--;
                break;

            default:
                cout<<"Invalid Entry";
                break;
            }
    
    }
    cout <<"Current count: "<< count << endl;
    //user enter cards seen on table and if below 7 increment
    //based on count the program returns if you should hit or quit
    return 0;

}

Expecting program to exit when enter is hit by user

Upvotes: 0

Views: 88

Answers (2)

Giogre
Giogre

Reputation: 1572

EDIT

As OP wants undetermined length of input, I suggest to switch the input itself from char to std::string.

This way access is gained to more intuitive and effective I\O operations:

#include <iostream> // std::cin, cout, endl
#include <string>   // std::string: can omit this line
#include <cctype>   // isspace(): can omit

int main(){
    int count = 0;
    std::string currcard{""};
    std::cout << "Enter cards seen on table: "<< std::endl;

    std::getline(std::cin, currcard);
    for (char c : currcard) {
        if (isspace(c))
            continue;
        switch (c) {
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
                ++count;
                break;

            case '7':
            case '8':
            case '9':
                break;

            case 'A':
            case 'J':
            case 'Q':
            case 'K':
                --count;
                break;

            default:
                //std::cout << "Invalid Entry\n";
                break;
        }
    }
    std::cout <<"Current count: "<< count << std::endl;
    //user enter cards seen on table and if below 7 increment
    //based on count the program returns if you should hit or quit
    return 0;
}

Notice I have added a check for white spaces, and removed the message for invalid entries: both simply get ignored. But if needed that line can be uncommented.


Old solution

You can use cin.getline() as suggested in the comments, in conjunction with a flag that triggers exit from the loop once three inputs are given:

#include <iostream>

int main(){
    static int count = 0, flag = 0;
    char currcard;
    std::cout << "Enter cards seen on table: "<< std::endl;

    while(std::cin.getline(&currcard, 3)){
        switch (currcard)
        {
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
                ++count;
                break;

            case '7':
            case '8':
            case '9':
                break;

            case 'A':
            case 'J':
            case 'Q':
            case 'K':
                --count;
                break;

            default:
                std::cout << "Invalid Entry\n";
                --flag;
                break;
        }
        ++flag;
        if (flag == 3)
            break;
    }
    std::cout <<"Current count: "<< count << std::endl;
    //user enter cards seen on table and if below 7 increment
    //based on count the program returns if you should hit or quit
    return 0;
}

There is also a flag decrement for invalid entries.

Upvotes: 0

kuriboh
kuriboh

Reputation: 141

You can use cin.get() like this.

    while (cin>>currcard)
    {
        // your logic
        if (cin.get() == '\n') {
            break;
        }
    }

In this way, your input is supposed to be something like 1 2 3 4 A J Q ending with Enter.

Upvotes: 0

Related Questions