if statement with char array

Hi, I have the code
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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <windows.h>

using namespace std;

int main () {
	char firstname[10];
	char lastname[10];
	
	cout << "Enter your first name:\n";
	cin >> firstname;
	cout << "Your first name is " << firstname;
	
	cout << "\nEnter your last name:\n";
	cin >> lastname;

	if (firstname == "Sam" || firstname == "sam" || firstname == "SAM")
	{
		cout << "\n" << lastname << endl;
	}

	else
		cout << "You're not Sam... Get lost n00b!\n";
	system("pause");
	return 0;
}


and even if I put sam as the firstname it always goes to the you're not sam part... I can't figure out why. I have it working with strings... just need to know if there's anything special I have to do with a character array.
Last edited on
C style string comparisons are special. Look up strcmp in the reference section for a hint.
http://www.cplusplus.com/reference/clibrary/cstring/strcmp.html
Remember strcmp always returns a 0 if strings are equal.
Last edited on
Why don't you use std::string instead of char arrays?
This should work:

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
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <windows.h>

using namespace std;

int main () {
	char firstname[10];
	char lastname[10];
	
	cout << "Enter your first name:\n";
	cin >> firstname;
	cout << "Your first name is " << firstname;
	
	cout << "\nEnter your last name:\n";
	cin >> lastname;

	//if (firstname == "Sam" || firstname == "sam" || firstname == "SAM")
	//{
	//	cout << "\n" << lastname << endl;
	//}
	
	if (stricmp ("Sam",firstname) == 0)
	{
           cout << "You are: " << firstname << lastname << endl;
       
    }

	else
		cout << "You're not Sam... Get lost n00b!\n";
	system("pause");
	return 0;
}

hey, I was trying to make a letter guessing game, but when I use the stricmp command it's is giveing me
error C2664: 'stricmp' : cannot convert parameter 1 from 'char' to 'const char *'

here is what my code looks like
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
#include<iostream.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>

int Rand_Num=0,x=0;
char PcLet[27]="abcdefghijklmnopqrstuvwxyz", UsLetGuess[2]={0};

int main()
{
	srand(time(NULL));
		cout<<"Welcome to my Letter Guessing game!!";

	Rand_Num=rand()%((27-1)+1);

		cout<<"I have a letter what is it?\n";
			cin>>UsLetGuess[x];

			if (stricmp(PcLet[Rand_Num],UsLetGuess)==0)
				cout<<"Correct!!";
                        else
                                cout<<"Incorrect";

			cout<<endl;
				system("pause");




	return 0;
}


can any of you here help me with this problem? thanks.

Last edited on
PcLet[Rand_Num] is a character of PcLet not a char sequence, try with if ( PcLet[Rand_Num] == UsLetGuess[x] )
Thanks that seems to work, But I don't entirely Understand why it's not a char sequence? (Sorry I am kinda new to this, I did it in high school, put it down a little too long and now I need to refresh my memory of all this coding) thanks for the help.
Last edited on
1
2
3
char singleChar = 'c'; // this is a single character with value 'c'
char charArray[] = "abcde"; //This is a char sequence values: 'a', 'b', 'c', 'd', 'e', '\0'
singleChar = charArray[3]; // singleChar is assigned to the value of the (3+1)th character of charArray 'd' 

A character sequence is defined by a pointer to the first character of the sequence until the character '\0' is found or the buffer has been completely filled


http://www.cplusplus.com/doc/tutorial/ntcs.html
Ok I kinda understand a little better, but what do you mean by this part


// singleChar is assigned to the value of the (3+1)th character of charArray 'd'


why is it assigned to 'd'
Last edited on
char charArray[] = "abcde";
so
charArray[0] == 'a' charArray[1] == 'b' charArray[2] == 'c' charArray[3] == 'd' <- 4th character of the array is 'd'
charArray[4] == 'e' charArray[5] == '\0'
Last edited on
Ah so I think I get it now so sense you used
 
singleChar = charArray[3];


that singleChar Variable will be d. ok thanks, sorry if this is the wrong place or what ever but thanks alot.
Last edited on
Topic archived. No new replies allowed.