'std::string' has no member named 'username' error [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
My code won't compile because of a 'std::string' has no member named 'username' error. I'm using code::blocks. I'm guessing the problem is because I'm trying to assign a string to a class, can someone help?
#include <iostream>
#include <string>
using namespace std;
class userx
{
public:
string username;
string password;
};
int main()
{
userx X, Y;
X.username = "X";
X.password = "1234";
Y.username = "Y";
Y.password = "5678";
string a;
string b;
cout << "What is your name?" << endl;
cin >> a;
if (a.username == a)
{
cout << "What is your password?" << endl;
cin >> b
if (a.password == b)
{
cout << "Password Accepted" << endl;
};
};
};

You wrote a.username when you probably meant X.username or Y.username.

if(a.username == a)
since a is string so it doesn't have username as its member that can be accessible using dot operator

Your code says
if(a.username == a){
cout<<"What is your password?"<<endl;
cin>>b
if(a.password = b){
cout<<"Password Accepted"<<endl;
};
when you declared string a; And indeed a std::string has no username.
Perhaps you meant to compare what was entered with X:
if(X.username == a){
cout<<"What is your password?"<<endl;
cin>>b
if(X.password = b){
cout<<"Password Accepted"<<endl;
};
Maybe you also mean to check against Y.
You might want to think about what happens if if the username is wrong. As it stands it will say password accepted even if the username is wrong.
Edit
In fact if(X.password = b) is an assignment not a comparison, so your user will probably get through regardless of username and password.
Edit again
It sounds like what you want is to use std::find on a container or to see if a std::map contains a given key using its find method. There are plenty of online clues

Related

value is always set to 1 even if the input is 0 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
ok so i writ this little code in c++. keep in mind i just started learning c++ so this problem confused me a lot
when i run this code in code blocks (
using namespace std;
int Answer;
int main()
{
cout << "yo, are u male?"<< endl;
cin >>Answer;
if (Answer = 1){
cout << "ur male"<< endl;
} else {
cout <<"ur female"<< endl;
}
cout <<Answer;
}
)
the answer value is always set to one even if i type 0
i tried coding another if statement for the answer value if it was 0 but that didn't work either
In C++, = operator is an assignment operator and it sets value of left operand to value of right operand. Therefore the value of Answer becomes 1 thanks to Answer = 1.
You should use == operator to check equality.
Use the == operator in your code, as = operators does the job of assigning values.
using namespace std;
int Answer;
int main()
{
cout << "yo, are u male?"<< endl;
cin >>Answer;
if (Answer == 1){
cout << "ur male"<< endl;
} else {
cout <<"ur female"<< endl;
}
cout <<Answer;
}
)

C++ invalid use of nonstatic data member [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have looked around and have sen this question asked a fair bit but I the problems they are having seem different to mine.
I am only a beginner so I find it kind of hard to understand whats wrong with my program. Here is the code:
#include <string>
#include <iostream>
using namespace std;
class Character
{
int health;
string action;
public:
void setHealth(int hp) {health = hp;}
void setAction(string act) {action = act;}
int getHealth() {return health;}
string getAction() {return action;}
};
int main()
{
int difficulty;
Character player;
player.setHealth(15);
Character enemy;
cout << "What difficulty would you like to play? easy = 1, medium = 2, hard = 3 ";
cin >> difficulty;
switch (difficulty)
{
case 1 : enemy.setHealth(10); break;
case 2 : enemy.setHealth(15); break;
case 3 : enemy.setHealth(20); break;
}
cout << "\nEnemy health = " << enemy.getHealth << endl;
return 0;
}
And here is the error message I get: In function 'int main()':
36:39: error: invalid use of non-static member function
It appears the problem is at the cout at the bottom of the main function.
Please help!
getHealth() is a class method, not a member, so it should be:
cout << "\nEnemy health = " << enemy.getHealth() << endl;

Why doesn't my c++ code work? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
What have I done wrong with the following code?
#include <iostream>
using namespace std;
main ()
{
int a;
int b;
int sum;
cout << "Enter first number \n";
cin >> a;
cout << "Enter second number \n";
cin >> b;
sum = a+b;
cout << "The sum of both numbers is" << sum << endl;
return 0;
}
Does the editor you are using tells errors, so the code is not executing? Or som exception rises? Or it is executing but nothing is shown? Please specify your problem accurately.
Anyway, you must use
int main ()
instead of
main()
Notice that your code returns a value. The last line of you code is:
return 0;
Thus, you must specify an int return type.
Check your initial lines with this.
#include <iostream>
using namespace std;
int main ()
{

why am I getting random results when incrementing an int [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have to write a function that goes through a string, finds any capital letters present and makes them lowercase. I decided to add a bit of code to show what letters were found and how many were found. Although on each compilation, the value of 'cnt' yields ridiculous results.
#include <iostream>
#include <vector>
using namespace std;
int upper(string s) {
int cnt;
vector<char> v{};
for (auto& i : s) {
if (isupper(i)) {
v.push_back(i);
i = tolower(i);
++cnt;
}
}
cout << "new string is '" << s << "'\n"
<< "number of capitals found is " << cnt << "\n"
<< "letters found were ";
for (auto l : v)
cout << l << " ";
return 0;
}
int main() {
string l = "This IS a TeSt";
upper(l);
}
I'm sure I must have done something wrong with the loop but whatever the problem is, I cannot find it.
The variable cnt is never initialized when used, change
int cnt;
to
int cnt = 0;
You failed to initialize the local variable cnt. Using an uninitialized value provokes undefined behavior, under which basically anything can happen.
Use int cnt=0; and please turn on all your compiler warnings.

Verify if two strings are equal in C++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I'm doing an assignment that requires me to use a function to check whether two strings are equal. I keep getting a parse error on line 20, where the function is called, and I don't know what's wrong. Please take a look and let me know if you see what could be causing the problem. Thanks!
#include <iostream>
#include <string>
using namespace std;
bool checker(string firstWordParameter, string secondWordParameter);
int main()
{
string firstWord, secondWord;
bool match;
cout << "Hello user.\n"
<< "This program will determine whether two words are the same.\n"
<< "Please enter your first word you would like to check: ";
getline(cin, firstWord);
cout << "Great, now enter the second word: ";
getline(cin, secondWord);
match = bool checker(firstWord, secondWord);
if(match == true){
cout << "Match.";
}else{
cout << "Totally not a match.";
}
return 0;
}
bool checker(string firstWordParameter, string secondWordParameter)
{
if(firstWordParameter == secondWordParameter){
return true;
}else{
return false;
}
}
Try changing
match = bool checker(firstWord, secondWord);
into
match = checker(firstWord, secondWord);
Line 20 is
match = bool checker(firstWord, secondWord);
Change it to
match = checker(firstWord, secondWord);
Also when you see error in compiler, double click it then it will show you the line with the error.