My getline is making me skip a cin

The title doesnt really make sense but basically when I run my program, and I try to cin playerOne and playerTwo, it doesnt let me assign a string to playerOne,

if I use "cin" instead of getline, it works, but I wont be able to write a last name...
(Please enter A when you run)


One more problem, if I enter anything besides 'A' or 'B' it does the corect error message, but if I say 'ittsbfa' or anything more than 1 character, the error message will repeat itselft that many times.

is this because it is a char and not a string?
can I even make a string of 1 character?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//CornHole_Program

#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

int twoPlayers();
int fourPlayers();

int main()
{
    char choice;
	cout << "Hello! would you like to play singles or doubles?\n";
	cout << "For Singles, please press 'A', for Doubles, please press 'B'\n";
	
	cin >> choice;
	
	while (choice != 'A' && choice != 'B')
	{
        cout << "Invalid selection, Please enter 'A for singles and 'B' for Doubles.\n";
        cout << "Your Choice: ";
        cin >> choice;
	}
	
	if (choice == 'A')
	{
	    cout << "Singles, Have fun!\n";
	    twoPlayers();
	}
	else
	{
	    cout << "Doubles, Have fun!\n";
	    fourPlayers();
	}
}






int twoPlayers()
{
    string playerOne;
    string playerTwo;
    
    cout << " \n" << " \n" << "______________SINGLES______________\n" << endl;
    cout << "Please enter the name for player one and player two.\n";
    
    cout << "Player One: ";
    getline(cin, playerOne);
    cout << "Player Two: "; 
    getline(cin, playerTwo);
    
}






int fourPlayers()
{
    cout << "Working on it" << endl;
}
Last edited on
the issue with getline is on lines 51-54
The problem is mixing formatted input, std::cin (line 23 in main), with std::getline input (line 52 in twoPlayers).

https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction
thanks that helped, is there a solution for the multiple error messages?
And what exactly are the errors?

https://www.learncpp.com/cpp-tutorial/function-return-values/

I'll give ya a hint, your function declarations say they will return an int value, yet they don't.

Declare both functions as void instead of int.
thats not the error, and in the future it will return an int, the issue is for the main function, when it asks to enter A or B, and anything else will be an error message. for example it i enter C it will give me an error message, if I enter CCC it will give me 3 error messages in a row. Should I change choice to a string variable?
... in the future it will return an int

You made the contractual promise your functions would return a value, add code to do so even if it is not used at this time. Deliberately writing code wrong is, well, wrong.

https://stackoverflow.com/questions/52303608/validate-string-input-in-c-for-letters-and-spaces-only

Putting your "it's possible to enter wrong data" input logic into a loop so if the user enters bogus data the program loops back to getting the input again and again until the expected data is entered.

The type of loop (for, while, do/while) is up to you, and how you structure your input logic.
so until It returns a value I should have it as void while I am testing?

hmm I never thought of that, it does make sense though
I recommend you add a generic return statement to the end of each function for testing purposes. Say return -1;. That way you don't have to change the function declarations/definitions later, just modify the return statements when you actually write the code for computing what you want to return.

A lot less work that way, and easier to change one statement instead of multiple ones.
true that, thanks so much for your help man!

I will post again if I have any questions I cant figure out.
Topic archived. No new replies allowed.