i dont know what is wrong with my program

ive just started programing and im 13.
this is what ive done so far
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;
int main()

{
char a[3];
char b[3];
int c;
b = 'cat';

c = '1';

cout<<"please enter your password: ";
cin>>a;
if (a = b) {
cout<<"correct password";
c = 2;
}
if (c = 1){
cout<<"incorrect password";
}

  
}

could any one help?
1) You should use strings, not char arrays.

2) You're mixing up '=' (assignment) with '==' (comparison)

3) Proper indentation wouldn't hurt, either

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>  // for string
using namespace std;
int main()
{
  string a;  // make these strings
  string b;
  int c;
  b = "cat";  // strings go in double quotes

  c = 1;  // you probalby meant 1, not '1'

  cout<<"please enter your password: ";
  cin>>a;
  if (a == b) {  // note:  == for comparison, not =
    cout<<"correct password";
    c = 2;
  }
  if (c == 1){  // again:  == instead of =
    cout<<"incorrect password";
  }

  
}
thanks
i dont realy understand strings and arrays but that works
For now, just stick with strings when you want to represent string data. char arrays are a lot more dangerous, harder to use, and harder to understand.

Once you get more familiar with the language you can come back and try to figure out how char arrays work, but don't worry about it for now.
You are using C-strings incorrectly. http://cplusplus.com/doc/tutorial/ntcs/
I recommend using std::string anyways.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Your code rewritten using std::string
#include <iostream>
#include <string>

int main()
{
	std::string a;
	std::string b = "cat";
	
	std::cout << "Please enter your password: ";
	std::cin >> a;
	if(a == b) // Check for equality (using two '='s)
		std::cout << "Correct password" << std::endl; // std::endl is a new-line that also flushes the buffer
	else // If a != b
		std::cout << "Incorrect password" << std::endl;

	return 0; // Return something from main
}


And your if statements aren't doing what you expect them to (they are used correctly in the example above).
1
2
3
int a = 3; // One '=' for assignment
if(a == 3) // Two '='s for comparison
    ...


Using the variable c is unnecessary. An if-else block can be used instead (see the std::string example).


EDIT: That's what I get for taking my time lol. Anyways I'd recommend reading the entire language tutorial on this website. http://cplusplus.com/doc/tutorial/. It will definitely help you on your way.
Last edited on
Topic archived. No new replies allowed.