#include <iostream>
usingnamespace 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";
}
}
#include <iostream>
#include <string> // for string
usingnamespace 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";
}
}
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.
// 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.