C++ HELP

closed account (1vkSE3v7)
This is my Script and I do not know what Is wrong with it please rewrite the script correctly


#include <iostream>

using namespace std;

int main()
{
char b[] = {'h', 'e', 'l', 'l', 'o' , '\0' };
char a;


cin >> a;

if (a == b)
{
cout << "Hello To You Too";

}

}
You are comparing the single character 'a' to the array of characters 'b'. You have to compare an array to an array. I would recommend using <string> though.

http://www.cplusplus.com/reference/string/
Actually I think they are comparing the character a to the character at index 0 in the array b which in this case would be 'h'. (or the address of the first element anyways)
Last edited on
That may be true, but it doesn't compile for me so I can't test it.
The problem with your code is that char b[] is an array that holds the characters of hello and null terminating character, but char a only holds a single character 'h' form the input stream and the 'ello' is left in the stream. Then in your if statement you are comparing a ('h') to see if it is the same as b (hello) which returns false. You could use strings to make it much simpler. Like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string b {"hello"};
	string a;
	
	cin >> a;
	
	if (a == b){
		cout << "hello to you too\n";
	}
}
hello
hello to you too


Otherwise you would have to make char a an array large enough to hold hello from the input and then compare a with b. Like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;

int main()
{	
	char b[] = {'h', 'e', 'l', 'l', 'o','\0'};
	char a[6];
	
	cin >> a;
	
	if (a == b){
		cout << "hello to you too\n";
	}
}
hello
hello to you too

giblit wrote:
Actually I think they are comparing the character a to the character at index 0 in the array b which in this case would be 'h'. (or the address of the first element anyways)

No, it is comparing 'h' to 'hello'. To compare the character in a with an element of b he would have to use b with an index (b[0...n]). See:
1
2
3
4
5
6
7
8
9
10
 
#include <iostream>

using namespace std;

int main()
{
	char test[] = {'h','e', 'l','l', 'o', ' ', 'g', 'i', 'b', 'l', 'i', 't', '!', '\0'};
	cout << test;	
}
hello giblit!
Last edited on
I believe you have to include <cstring> to make that last code snippet work. You can't just cin to a char array by default. Apparently you can, and <cstring> is not needed. My last sentence is still true though.

You would also have to use strcmp() to compare the string, the == operator is not overloaded for that.

Corrected Code:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

int main ( )
{	
    char a[6], b[] = "hello";
	
    std::cin >> a;
	
    if ( !strcmp ( a, b ) ) std::cout << "hello to you too\n";

    else std::cout << "no\n";
}


Though, if you try to input something longer than five characters it will write out of bounds.
Last edited on
Right now I'm trying to figure out what is going on with my snippet. I tested all three of them before posting then pasted each output. When I tested that snippet it printed out the if statement so I assumed it was correct, but then wouldn't print it any test after you pointed that out. Definitely scratching my head on it right now.
Last edited on
No, it is comparing 'h' to 'hello'. To compare the character in a with an element of b he would have to use b with an index (b[0...n]). See:


What I meant they are doing is pretty much something like:

1
2
3
4
5
int *a = new int[2]{0, 1};

int b = 0;

if(b == a) //comparing value of 0 to what ever address a is at index 0 


AFAIK array names are converted to pointers to the first element.

I figured out what I did, I apparently pasted the output by accident of this test sample. Sorry about the confusion, bad idea to code multiple tests at 3am after being up all day with only 3 hrs sleep.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
using namespace std;

int main() {
	char b[] = {'h', 'e', 'l', 'l', 'o', '\0'};
	string a;
	
	cin >> a;
	
	if (a == b){
		cout << "Hello to you";
	}
	return 0;
}
hello
Hello to you

http://ideone.com/ZHHFXe
Yeah that would work but comparing two arrays wouldn't like that
http://www.cplusplus.com/reference/string/string/operators/

If you tried to compare to arrays you would be comparing their addresses.
Topic archived. No new replies allowed.