0

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

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
Reza Shakeri
  • 97
  • 1
  • 12
  • 1
    This is very much not C#. If you get an error or not is something you have to try. We can not tell you if you will get an error or no error. – Christopher Mar 31 '20 at 21:51
  • 1
    either pick the tag C or C++ or C#, it cannot be the 3 at the same time... – B. Go Mar 31 '20 at 21:52
  • FYI, The C# language has garbage collection, C and C++ don't. C and C++ support pointers; the C# language hides pointer usage (discourages the use of them). C and C++ don't require the .NET or COM objects. So, which single language are you programming in? – Thomas Matthews Mar 31 '20 at 21:56
  • BTW, the C language doesn't have the scope resolution operator, `::`. So I guess you are not programming in C. – Thomas Matthews Mar 31 '20 at 21:57

1 Answers1

2

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;

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • plus a recommendation to _heed warnings_ wouldn't go amiss – Asteroids With Wings Mar 31 '20 at 22:01
  • Error was fixed , Thank you very much for your guidance – Reza Shakeri Mar 31 '20 at 22:02
  • @AsteroidsWithWings afaik warnings for no return is nothing to rely on. Of course here compilers should warn, but in general I would not count on it – 463035818_is_not_an_ai Mar 31 '20 at 22:03
  • Another problem now is when I enter my registration information and when I want to log in it tells me that my username and password are incorrect. – Reza Shakeri Mar 31 '20 at 22:23
  • @RezaShakeri did you fix `pass` vs `newpass` ? – 463035818_is_not_an_ai Mar 31 '20 at 22:29
  • @idclev463035818 Yeah, thank you, I didn't really care, I didn't do anything to change the pass and the newpass to make it work, so much, thank you very much for your guidance. – Reza Shakeri Mar 31 '20 at 22:34
  • @idclev463035818 I've never seen it not happen when it should. – Asteroids With Wings Mar 31 '20 at 22:54
  • @AsteroidsWithWings I was doing some research and found out that the reason the warning cannot be an error is not that it cannot be detected reliably (that was my previous understanding), but because there are cases, where you cannot return on all branches (but are still valid, example [here](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=43943#c4)). So well yes, one can rely on that warning. – 463035818_is_not_an_ai Apr 01 '20 at 08:11
  • @AsteroidsWithWings reading that comment on the bugreport again, it also mentions cases that cannot be diagnosed, I think they are rare, but they do exists, so I changed my point again: I would not 100% rely on the warning ;) – 463035818_is_not_an_ai Apr 01 '20 at 08:29
  • @AsteroidsWithWings fwiw, I asked a question about it https://stackoverflow.com/questions/60966763/are-there-cases-of-complilers-not-being-able-to-diagnose-missing-return – 463035818_is_not_an_ai Apr 01 '20 at 08:40
  • @idclev463035818 Yes that is literally the opposite case: getting the warning when you don't want it, because you didn't use a compiler intrinsic to signal that that particular code path is to be deemed unreachable. – Asteroids With Wings Apr 01 '20 at 11:14