Issue with creating password function

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 2003 Views - Last Post: 11 September 2011 - 03:12 PM Rate Topic: -----

#16 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

Re: Issue with creating password function

Posted 25 August 2011 - 05:33 AM

View Postjpass, on 25 August 2011 - 11:23 PM, said:

do I have to use each one for each instance?


You need to use those three lines to replace each occurrence of system("PAUSE").

Is that your question?
Was This Post Helpful? 1
  • +
  • -

#17 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Issue with creating password function

Posted 25 August 2011 - 06:00 AM

I'll apply the cin.clear, cin.ignore and cin.get to your newest code . . .
//
// Encryption Program
//
#include<cstdio>
#include<cstdlib>
#include<string>
#include<iostream>
using namespace std;
int main()
{
    //This program changes text into various levels of
    //encryption based on the user's specification.
    //Before beginning user must input proper username
    //and password.
    //First get username and password
    string password;
    password="elevenfingers";  //Create the ability to use 
                               //several different usernames and
                               //passwords
    string username;
    username="jpass";  //Username is first initial & first 
                       //four of last name
    string userinput;
    do
    {
        cout<<"Enter username:";
        cin>>userinput;
        if(userinput==username)
        {
            cout<<endl<<"Enter password:";
            cin>>password;
        }
        else
        {
             system("CLS");
             cout<<"Invalid Username.\n"<<endl;
             system("PAUSE");
             return 0;
        }
    }
    while(userinput!=username);
    if(password!="elevenfingers")
    {
    system("CLS");
    cout<<endl<<"Invalid Password.\n\nACCESS DENIED.\n"<<endl;
    cin.clear;
    cin.ignore;
    cin.get;
    return 0;
    }
    else
    {
        system("CLS");
        cout<<endl<<"Welcome Mr. Pxxxxxxxxx\n\n"<<endl;  //Call up different welcome 
                                             //message based on  
                                             //username/password
        cin.clear;
        cin.ignore;
        cin.get;
        return 0;
    }
//Now begin encryption program
cin.clear;
cin.ignore;
cin.get;
return 0;
}



It should work now. Let me know if you have any errors or questions.
Was This Post Helpful? 1
  • +
  • -

#18 jimblumberg  Icon User is offline

  • member icon


Reputation: 4233
  • View blog
  • Posts: 13,300
  • Joined: 25-December 09

Re: Issue with creating password function

Posted 25 August 2011 - 06:40 AM

@hulla Did you forget something? I get the following errors when I try to compile the code with your additions:

Quote

||=== c++homework, Debug ===|
main.cpp||In function ‘int main()’:|
main.cpp|46|error: statement cannot resolve address of overloaded function|
main.cpp|47|error: statement cannot resolve address of overloaded function|
main.cpp|48|error: statement cannot resolve address of overloaded function|
main.cpp|57|error: statement cannot resolve address of overloaded function|
main.cpp|58|error: statement cannot resolve address of overloaded function|
main.cpp|59|error: statement cannot resolve address of overloaded function|
main.cpp|63|error: statement cannot resolve address of overloaded function|
main.cpp|64|error: statement cannot resolve address of overloaded function|
main.cpp|65|error: statement cannot resolve address of overloaded function|
||=== Build finished: 9 errors, 0 warnings ===|


These are functions
cin.clear();
cin.ignore();
cin.get();


Second the cin.ignore() function as you have provided will only ignore one character. What happens if there are multiple characters left in the input buffer? In most cases you do not need the cin.clear(). This is only to clear any error flags from the input stream, and since you are using strings for all of your input you should not run into an error condition with your entries.


Jim
Was This Post Helpful? 2
  • +
  • -

#19 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Issue with creating password function

Posted 25 August 2011 - 07:24 AM

Really? Someone told me that clear and ignore delete remaining chars in the input buffer . . . Maybe I got confused.

Quote

These are functions

cin.clear();
cin.ignore();
cin.get();

Oh right. For some reason I mistook them for keywords. My bad. Thanks Jim.
Was This Post Helpful? 1
  • +
  • -

#20 jpass  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 22-August 11

Re: Issue with creating password function

Posted 25 August 2011 - 01:30 PM

View Postjanotte, on 25 August 2011 - 08:33 AM, said:

You need to use those three lines to replace each occurrence of system("PAUSE").

Is that your question?

Yes, that is what I was asking thanks.

@ All, thanks for the advice, I'll work on it as much as I can this weekend, provided we don't get hit by irene too bad...
Was This Post Helpful? 0
  • +
  • -

#21 jpass  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 22-August 11

Re: Issue with creating password function

Posted 10 September 2011 - 11:14 AM

I've been working with a different portion of my program lately. My question concerns this part:
 switch (userChoice)
    {
        case 1:
        //Allow user to enter plaintext.
        char ptext;
        system ("CLS");
        std::cout << "Enter your text.\n\n";
        std::cin  >> ptext;
        seesir();
        break;

(there is more too this which is why you don't see the closing bracket)

I have the function "seesir()" included in a header file, and it looks like this::


void seesir()
{
    char *ptext; // ciphertext
    int shift = -13; // ciphershift

for( char *letter = ptext; letter; ++letter )
{
  bool isCaps = ( *letter >= 'A' ) && ( *letter <= 'Z' );
  if( isCaps )
    *letter -= 'A'-'a';

  letter -= 'a';
  letter = ( letter + shift + 26 ) % 26;
  letter += 'a';

  if( isCaps )
    letter += 'A'-'a';
}
}


The error I'm getting is in line 13 and states:: error: invalid operands of types 'char*' and 'int' to binary 'operator%'.

My question is what this error means in layman's terms, not how to fix it - I'd like to do that myself when I understand the exact problem. Thanks in advance!
Was This Post Helpful? 0
  • +
  • -

#22 jimblumberg  Icon User is offline

  • member icon


Reputation: 4233
  • View blog
  • Posts: 13,300
  • Joined: 25-December 09

Re: Issue with creating password function

Posted 10 September 2011 - 11:36 AM

There are several problems with your seesir() function.

Fist what are you trying to do with the pointers?

Second you have defined a pointer ptext that you never initialize. Then you define another pointer (letter) which you initialize with the uninitialized variable ptext. And finally on line 13 you are using a pointer with % (modulus) which is not allowed.

Jim
Was This Post Helpful? 1
  • +
  • -

#23 hulla  Icon User is offline

  • Writing Lines


Reputation: 49
  • View blog
  • Posts: 732
  • Joined: 05-March 11

Re: Issue with creating password function

Posted 10 September 2011 - 10:45 PM

You do know % is the modulo operator which finds the remainder of floats and doubles.

cout << "Modulus of 10 MOD 3: " << 10 % 3 << endl;


This will print 1 because 10/3 leaves a remainder of 1.
Was This Post Helpful? 1
  • +
  • -

#24 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6092
  • View blog
  • Posts: 23,612
  • Joined: 23-August 08

Re: Issue with creating password function

Posted 11 September 2011 - 05:05 AM

ptext is a variable capable of holding only a SINGLE character.

char ptext; // Room for one character
system ("CLS");
std::cout << "Enter your text.\n\n";
std::cin  >> ptext;
seesir();


Why did you switch from C++ strings to char arrays anyway?

If you're expecting the seesir function to know anything about ptext that was entered, you need to tell it. Read my tutorials on functions in my signature.
Was This Post Helpful? 1
  • +
  • -

#25 jpass  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 22-August 11

Re: Issue with creating password function

Posted 11 September 2011 - 12:59 PM

Thanks for all the replies. I had been basing the code off of a sample that I found online, which proved pretty useless - so I'm thinking I'll stay away from that. This is what I'm working with right now:

#include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>

void seeSir()
{
    int shift;        //number of characters to shift
    shift == 13;
    char ptext[800];  //plain text
    char ntext[800];  //new text

    std::cout << "Enter your text.\n\n";
    std::cin  >> ptext;

    //Change uppercase letters to lowercase
    for (char c=0; c<800; c++)
    {
        if ( ptext[c] < 91 &&  ptext[c] > 64)
        ptext[c] =  ptext[c] + 32;
    }

    //Output the new text
    ntext = ptext[c] + shift;  //I added this after getting an "Undeclared" error.
    std::cout << "\n";
    std::cout << "Your enciphered text is:\n\n" <<  ntext + shift;

}





As you can see, I added the "#include" s to the header file, and kept just the ".hpp" file in the main program. I added a couple of comments to explain what I'm thinking. My errors are:

Line 23 :: incompatible types in assignment of 'int' to 'char[800]
Line 23 :: name look up for 'c' changed for ISO 'for' scoping
Line 23 :: *NOTE* if you use '-fpermissive' G++ will accept your code.

I'm pretty sure the first error is pretty self explanitory, but I'm not sure of another way to tell the compiler to make the shift, I'll keep working at it though. I'm not sure what "-fpermissive" is.

I've done a few line deletions / move "up" or "down"s to try to get rid of my errors (I've gotten rid of several actually...) but I think it is making the code ugly & after I get it working the way I want it to, I plan on making it pretty again. These two errors have me stumped though.
Was This Post Helpful? 0
  • +
  • -

#26 jimblumberg  Icon User is offline

  • member icon


Reputation: 4233
  • View blog
  • Posts: 13,300
  • Joined: 25-December 09

Re: Issue with creating password function

Posted 11 September 2011 - 02:06 PM

When I compile your code I get the following errors/warnings.

Quote

main.cpp||In function ‘void seeSir()’:|
main.cpp|9|warning: statement has no effect|
main.cpp|17|warning: comparison is always true due to limited range of data type|
main.cpp|19|warning: array subscript has type ‘char’|
main.cpp|19|warning: array subscript has type ‘char’|
main.cpp|20|warning: array subscript has type ‘char’|
main.cpp|20|warning: array subscript has type ‘char’|
main.cpp|24|error: name lookup of ‘c’ changed for ISO ‘for’ scoping|
main.cpp|24|note: (if you use ‘-fpermissive’ G++ will accept your code)|
main.cpp|24|warning: array subscript has type ‘char’|
main.cpp|24|error: incompatible types in assignment of ‘int’ to ‘char [800]’|


The first is on line 6 where you use the comparison operator== instead of the assignment operator=.

Next on line 17 you use a char for your loop counter, then try to compare that counter to 800. A char can only hold a value between -128 to 127. So this counter should probably be an int.
    for (char c=0; c<800; c++)
    {
        if ( ptext[c] < 91 &&  ptext[c] > 64)
        ptext[c] =  ptext[c] + 32;
    }

    //Output the new text
    ntext = ptext[c] + shift;

Next since you defined this counter in the loop it will go out of scope when the loop finishes and will not exist after the loop ends. So in the last line in the above snippet c is undefined. You should declare this variable before your loop if you want to use it after the loop. This line is also incorrect because you are trying to assign a char (ptext[c]) to a C-string(a char*).

Also if you are trying to change the C-string to upper case you should look up the toupper() function.

In future please post the complete error/warning messages exactly as they appear in your development environment.

Jim

This post has been edited by jimblumberg: 11 September 2011 - 02:07 PM

Was This Post Helpful? 1
  • +
  • -

#27 jpass  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 22-August 11

Re: Issue with creating password function

Posted 11 September 2011 - 02:20 PM

Jim - I posted the errors, leaving out the warnings because I was more concerned with them; that being said, I'll post everything next time. I'm assuming that if there is a toupper() function, there is also a tolower() ? Thanks for pointing out my errors, I'll post my results.
Was This Post Helpful? 0
  • +
  • -

#28 jimblumberg  Icon User is offline

  • member icon


Reputation: 4233
  • View blog
  • Posts: 13,300
  • Joined: 25-December 09

Re: Issue with creating password function

Posted 11 September 2011 - 02:34 PM

Yes there is a tolower() function.

Quote

I posted the errors, leaving out the warnings because I was more concerned with them

The warning messages I received when I compiled the code should be treated as errors. Until you get much more familiar with the language always treat your warnings as errors. Start at the first error/warning and fix it first, many times fixing a warning or error will fix several other warnings or errors.

Jim
Was This Post Helpful? 2
  • +
  • -

#29 jpass  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 17
  • Joined: 22-August 11

Re: Issue with creating password function

Posted 11 September 2011 - 02:36 PM

The only error I'm still getting is "incompatible types in assignment of 'int' to 'char[800]'". What I want to do is shift the letters of the text X-number of spaces (I'm sure you had that figured out); from my understanding "int" stands for "integer" and "char" for "character," since I want to be able to enter text, and not only integers I need to be able to use "char." Is my understanding complete on this? If so, is there some way I can convert the 'char's into 'int's and back again before the final display?

Also, I took out the case - changing portion for now, since its not as important as the actual letter shift.

#include <cstdio>
#include <cstdlib>
#include <string>
#include <iostream>

void seeSir()
{
    int shift;        //number of characters to shift
    shift = 13;
    char ptext[800];  //plain text
    char ntext[800]; //new text
    int c=0;

    for (int c=0; c<800; c++)
    {
        if ( ptext[c] < 91 &&  ptext[c] > 64)
        ptext[c] =  ptext[c] + 32;
    }

    std::cout << "Enter your text.\n\n";
    std::cin  >> ptext;

    //Output the new text
    ntext = ptext[c] + shift;  //I added this after getting an "Undeclared" error.
    std::cout << "\n";
    std::cout << "Your enciphered text is:\n\n" <<  ntext + shift;

}

This post has been edited by jpass: 11 September 2011 - 02:36 PM

Was This Post Helpful? 0
  • +
  • -

#30 jimblumberg  Icon User is offline

  • member icon


Reputation: 4233
  • View blog
  • Posts: 13,300
  • Joined: 25-December 09

Re: Issue with creating password function

Posted 11 September 2011 - 03:12 PM

Quote

from my understanding "int" stands for "integer" and "char" for "character," since I want to be able to enter text, and not only integers I need to be able to use "char." Is my understanding complete on this?

Yes if you want to use characters you must use a character.

You probably should review Arrays and Character sequences as it looks like you don't really understand C-strings.

In your latest code your for loop is using uninitialized variables (ptext). After your loop you then ask the use for this C-string. You should reverse the order of the getting user input and using the variable. Also if you need to look into the functions contained in the cctype header. This header has many functions to test the type of the character. Also the tolower and toupper functions are prototyped in this header. Also you should be checking for the end of string character '\0' in your for loop instead of always checking, changing every character in your array. When you enter a value into this variable with cin the end of string character is automatically added to the C-string after the last character entered.

The following line:
ntext = ptext[c] + shift;

The variable ntext is a pointer to a character array, ptext[c] is a single character and shift is an integer. I am not really sure what you are trying to do with this line but you can not try to put a int or character (ptext[c] + shift) into a character array. You can replace a particular character but not the entire C-string. You probably want to loop through your ptext array and place the "shifted" characters into the ntext array.

Jim
Was This Post Helpful? 2
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2