简体   繁体   中英

Segmentation fault erore in c++

when I run the code below,This class is written for registration, will I get a segmentation error? what's the reason ? How can I fix it? Where's my code problem? I used c4droid in android please help.

#include <iostream>
#include <string>
using namespace std;
// registration class
class registration 
{
public:
string signup ();
string signin ();
private:
string newuser, newpass, 
user, pass;
};
// signup function 
string registration::signup()
{
cout << " sign up :) " << endl;
cout << " please Enter username : ";
cin >> newuser;
cout << " please Enter password : ";
while (cin >> pass)
{
     cout << " registration complete ;) " << endl;
     break;
}
}
// signin function 
string registration::signin()
{
cout << " signin :) " << endl;
cout << " please Enter username : ";
cin >> user;
cout << " please Enter password : ";
cin >> pass;
if (user == newuser && pass == newpass)
    cout << " you logged in ; ";
else
    cout << " wrong username or password, please try again ";
}
// main
int main ()
{
cout << " welcome " << 
endl;
registration sign;
sign.signup();
sign.signin();
}

Erore picture

Your method is declared as

string registration::signin()

ie you declared it to return a string , but you do not return anything from the method. Same problem with the other method. Not returning something from a function that you declared to return something is undefined behavior 1 . In a nutshell this means your code is incorrect, compilers are not required to diagnose that, can produce any output they feel like, and when you run the code anything could happen.

Either return something or declare them with void return.

Another problem is that in signup you read pass and also in signin you read pass and compare that to newpass . I suppose you want to make the two functions read into two different members and then compare them.

1 : main is an excpetion, because: if control reaches the end of main without encountering a return statement, the effect is that of executing return 0;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM