Getting Scanf

Please help, im trying to get the answer and when the answer is true, it will print, otherwise false, i dont know what im going to do, i cant ge the right if statement.. thanks in advance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 #define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<conio.h>
#include<string>
using namespace std;

int main(){
	string ans;
	string row = { 'r', 'o', 'w' };
    char array[3][10] = { "boat", "front", "best" };
	for (int i = 0; i <= 2; i++)
	{
		printf("%s \n", array[i]);
	}
	scanf(" %c", &ans);
	if (ans == row){
			printf("row boat, front row, bestrow");
	}
	else{ 
		printf("Wrong");
	}
	_getch();
	return 0;
}
First, I imagine you're trying to get a string. You're testing to see if the user entered "row" not 'r', 'o', 'w'. Because of this, your scanf statements needs to take a string, not a char. Also, your row variable needs to hold a string, not an array of characters. Secondly, if you're using scanf, you're not allowed to store input in the string class. You have to use a char array. Some amended code:
1
2
3
4
char ans[4]; //you hold your scanf input in a char array instead of a string
string row = "row"; //holds a string, rather than an array of chars like { 'r', 'o', 'w' }
....
scanf("%s", &ans); // %s instead of %c so that you can store a string rather than a char 

I'd suggest studying up on char arrays vs strings. Char arrays are typically kept to the C language and scanf. Strings are for the C++ language and are used with cin.
@Keene thank you very much! I've been stuck in this code for days, thanks a lot for the help. and will do look for strings and array. though im still studying the c language right now.
You cannot use C++ clases, namely std::string, with C input functions. Trying to do so will lead to unpleasant results.
closed account (1CfG1hU5)
note: #include <conio.h> won't run on cpp.sh web site.
Topic archived. No new replies allowed.