Sign in to follow this  
mastrgamr

whats wrong with this C++ code!!??

Recommended Posts

ive been wrecking my brain on trying to fix this code *pops an advil*
#include<iostream>
#include<string>
using namespace std;

    int security = 0;
    string user="mastrgamr", pass="c++";

int logins()
{
    int attempt=3;
    
    if (! security)
    {
          cout<<"Username was not found on the servers! You have "<< attempt--<<" more attempts left!";
          logins();
    }
    if (! security)
    {
          cout<<"Password was not found on the servers!";
          logins();
    }
          return 0;
}

int main()
{
    int choice;
    
    cout<< "Hello welcome to the login server!\n";
    cout<< "Are you a new member? (1:y/2:n) ";
    cin>> choice;
    
    if (choice == 1)
    {
               cout<<"\nRegister a new username to the server: ";
               cin>> user;
               cout<<"\nRegister a new password to the server: ";
               cin>> pass;
    }
    
    cout<<"Username: ";
    cin>> user;
    logins();
    cout<<"Password: ";
    cin>> pass;
    logins();
    
    cin.get();
    return 0;
}

When i run it and i type a wrong user, the logins() functions loops infinatly. Here is what im trying to do: -If new person logs in they get to create there own login -once login is set they get to login (then program ends) --If user is not new then they login to the server --If the username is wrong then the prog goes into login() and loops the username attempts 3 times (if wrong 3 times, program ends) --If username is correct but password is wrong then the user must re-do the login process (while user attempts countdown from 3) can someone tell me what i am doing wrong?!?!?!?!!! [Edited by - mastrgamr on March 22, 2008 8:54:07 PM]

Share this post


Link to post
Share on other sites
Quote:

Original post by mastrgamr
can someone tell me what i am doing wrong?!?!?!?!!!


int security = 0;

int logins()
{
if (! security) // true
{
logins(); // recursive call
}
}



security has the value 0, so logins() call itself recursively ad infinitum. You should get a stack overflow after some time.

Share this post


Link to post
Share on other sites

int logins()
{
int attempt=3;

if (! security)
{
cout<<"Username was not found on the servers! You have "<< attempt--<<" more attempts left!";
logins();
}
if (! security)
{
cout<<"Password was not found on the servers!";
logins();
}
return 0;
}




Please use the source /source tags when posting code snippets.
That said -- I think you need to take a look at your code. First of all, you are using an integer as a conditional -- which makes me suspect you've used C before when you didn't have a boolean datatype. Use bool if you have it available to you!

Next thing -- this function I've copy pasted is invoked many times in your code. Are you even sure what it is doing? The reason you're getting an infinite loop (and eventually a stack overflow I bet) is because inside the logins() function, you are again calling that logins function once over. This is a method called 'recursion' -- and when solving simple problems is worth avoiding in nearly all cases. It complicates code and makes things difficult for other programmers to read. Wikipedia explains it better than me: Recursion.

I'm not going to explain how you ought to write your program, but by avoiding recursion you'll a least be a little closer to your objective ;)

hth

~Shiny

[edit] beaten darnit.

Share this post


Link to post
Share on other sites
thing is that i dont know what i am doing 100%. im just starting out in C++.

its common sense to me now that the "if (!security)" recursion is happening, ill fix this.

Share this post


Link to post
Share on other sites
why cant i do this?


if (false)
{
.....
}



false tests the string right? (to see if what the user typed matches with the value of the "user" string?) It doesnt work when i compile

Share this post


Link to post
Share on other sites
Quote:
Original post by mastrgamr
why cant i do this?

*** Source Snippet Removed ***

false tests the string right? (to see if what the user typed matches with the value of the "user" string?) It doesnt work when i compile



if (false)




This isn't testing 'false' against anything. If what is false? You need to test against a boolean, e.g:


//Define a boolean
bool Something;

//Get some input here, set 'Something' as true or false


//Now test against 'Something'
if (Something == false)
{
//'Something' is false, do this
}
else
{
//'Something' must be true, so do this instead
}


Share this post


Link to post
Share on other sites
im trying to test if the user's input of the string value is the same as the program's value of the string user.

something like this

string user="mastrgamr";
bool user = true;

cout<<"input username: ";
cin>> user;
/*IF the user inputs the wrong string value then it moves on to testing the value*/

if (user == false)
{
logins();
}

Share this post


Link to post
Share on other sites
cin >> user stores the input into the user variable. It doesn't compare them. You need to store the input into variables and then compare them to user and pass

You should have something like


string user="mastrgamr", pass="c++";
string login, loginpass;
...
//Set login and loginpass to the username and password they attempt to use

if(login == user)
{
...
}
else
cout<<"Username was not found on the servers! You have "<< attempt--<<" more attempts left!";
if(loginpass == pass)
{
...
}
else
cout<<"Password was not found on the servers!";



Share this post


Link to post
Share on other sites
Quote:
Original post by mastrgamr
im trying to test if the user's input of the string value is the same as the program's value of the string user.

something like this
*** Source Snippet Removed ***


No need for the boolean (and the above won't compile anyway, you have two variables of the same name).

1. Input a string from the user
2. Test that string against your stored string


//String to store the password
string Password = "Hello";

//String to store the user's input
string UserInput;

//Ask for password, store it in 'UserInput'
cout << "Enter password: ";
cin >> UserInput;

//Test the user's input against the stored password
if(UserInput == Password)
{
//Password is correct, do something here
}
else
{
//Password is incorrect, do something else here
}




EDIT: Beat me to it

Share this post


Link to post
Share on other sites
Quote:
Original post by plastic_dragon99
cin >> user stores the input into the user variable. It doesn't compare them. You need to store the input into variables and then compare them to user and pass

You should have something like

*** Source Snippet Removed ***


OHHH i see, youve shown me the light! im going to make that happen right now. tell you if ist comes out lol

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this