getline not working

i am a beginner to programming and i dont know why this doesnt work. plz help
#include <iostream>
#include <string>
using namespace std;

int main() {
string p1Turn = "";
getline(cin, p1Turn);
if (p1Turn == "a1 a2"){
cout << "hi" << endl;
}
}
Instead of p1Turn=="a1 a2" use p1Turn.compare("a1 a2") == 0 .
Last edited on
still doesnt work
nevermind it works
Last edited on
is there a way using a for loop in a for loop to increase a1 and a2 everytime the for loop runs. I want to test if the user types a1 a2, a1 b1, a2 a3, a2 b2 etc all the way up to 10 and then change 'a' all the way up to j. basically i want every combination of a-f with 1-10 and check if that is what the user typed, otherwise i would have to type every combonation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
std::string input;
std::getline(std::cin, input);
char characters[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
bool exitLoop = false;
for (unsigned int i = 0; i < 6; i++){
	for (unsigned int j = 0; j < 10; j++){
		std::string customString = "";
		customString += characters[i] + std::to_string(j + 1);
		if (input.compare(customString) == 0){
			std::cout << "found";
			exitLoop = true;
			break;
		}
	}
	if (exitLoop)
		break;
}
Last edited on
i generally understand this but what is exitLoop do
Last edited on
just figured out what exitLoop does but first of all it just says Program ended with exit code: 0 (im using xcode-beta by the way) and also this would just to test if it is like a1 not a1 a2. I need to type in 2 of those and it needs to detect that. thanks for the help though
Last edited on
Sorry for not adding comments. Basically what's going on is first you create an array with the characters you want and create a loop for each variable that will effect final string combination. The code above has 2 different variables which are [character][number] so if you wanna do it with 2 of that sequence you want to create 4 loops. Here:

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
std::string input;
std::getline(std::cin, input);
char characters[] = { 'a', 'b', 'c', 'd', 'e', 'f' };
bool exitLoop = false;
for (unsigned int i = 0; i < 6; i++){
	for (unsigned int j = 0; j < 6; j++){
		for (unsigned int k = 0; k < 10; k++){
			for (unsigned int l = 0; l < 10; l++){
				std::string customString = "";
				customString += characters[i] + std::to_string(k + 1) + " " + characters[j] + std::to_string(l + 1);
				if (input.compare(customString) == 0){
					std::cout << "found";
					exitLoop = true;
					break;
				}
			}
			if (exitLoop)
				break;
		}
		if (exitLoop)
			break;
	}
	if (exitLoop)
		break;
}
Last edited on
one last thing. how can i change an array's elements. for example I want to change box [0][1] from " " to "-"
Do you mean how to change a1 b2 to a1-b2 ? If so you can do it by changing + " " + to + "-" +.
Last edited on
nevermind i figured it out thanks for all the help
No problem :)
Topic archived. No new replies allowed.