-1

This is a program that tests for password strength. I used simple tests to check the password, but I am having issues. Let me state that I am not very good with c++, so sorry if the error is apparent

#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <cctype>

using namespace std;

int main()
{
    cout << "Please enter your password!" << endl;
    cin >> password;

}
bool hasUpp = false;
bool hasLow = false;
bool hasDig = false;
bool hasSym = false;

string strlen, password; //The next line is throwing the error, expecting a declaration
for(int i = 0; < strlen(string1); ++i) //This is where my error is
{
    if (isupper(string1[i]))
        hasUpp = true;
    if (islower(string1[i])) //Do all of these if statements seem correct?
        hasLow = true;
    if (isdigit(string1[i]))
        hasDig = true;
    if (issymbol(string1[i]))
        hasSym = true;
    if (strlen <= 7) //would this be the right way to check for string length?
        return false;
    if (strlen >= 8)
        return true;
}
if (hasLow && hasUpp && hasDig && hasSym)
{
    return true;
}
else
{
    return false;
}

I am having issues with the for loop towards the top, I can not seem to get rid of this error. I don't know what else to write about it, I can't post this without adding more text though so I'm just going to write more words until it let's me post

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • *goldfish tango chilis pepper* maybe you should add relevant details? – Borgleader Oct 14 '14 at 17:38
  • You're missing the variable before `<`. Is that in the real code or a copying error? – Barmar Oct 14 '14 at 17:39
  • You have no declaration of the variable `string1`. You've also declared a local variable named `strlen`, this will prevent you from calling a function with the same name. – Barmar Oct 14 '14 at 17:41
  • I didn't even notice that I was missing the i variable before <, thank you. To declare string1, I would just add " string string 1;" right? Thank you – Chris Salazar Oct 14 '14 at 17:44
  • Besides all the errors in the previous comments, your for loop (and the if that follows it) is not declare inside a method. – lbarreira Oct 14 '14 at 17:44
  • I'm not trying to be offensive, but *"I am not very good with c++"* is an understatement. You don't know C++ just yet. I suggest you take a step back and read a good book. See [here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) for recommendations. – jrok Oct 14 '14 at 17:47

4 Answers4

2

You've put a bunch of statements (specificaly a for loop and an if statement) in global scope. Only declarations can appear there (althoug those can contain expressions).

To simplify your code:

int main()
{
    cout << "Please enter your password!" << endl;
    cin >> password;

}  // the body of main ends here

for (;;) {} // this doesn't belong here

Once you fix that, there's another error:

for(int i = 0;      < strlen(string1); ++i)
//             ^^^^
//             something's missing here
jrok
  • 54,456
  • 9
  • 109
  • 141
2

sorry to say but..there are all sorts of things missing in your code here is a more correct version.. try to check the differences.. still somethings may be missing.. but hopefully it puts you on the right track

#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <cctype>

using namespace std;

int main()
{
    string password; //must be declared before use

    cout << "Please enter your password!" << endl;
    cin >> password; //fetch password


int nlen = strlen(password); //collect the length of text
    if (nlen <= 7) //no need to be done inside loop
        return false;
    if (nlen >= 8)
        return true;

bool hasUpp = false;
bool hasLow = false;
bool hasDig = false;
bool hasSym = false;

for(int i = 0;  i < nlen ; ++i) //proper iteration
{
    if (isupper(password[i]))
        hasUpp = true;
    if (islower(password[i]))
        hasLow = true;
    if (isdigit(password[i]))
        hasDig = true;
    if (issymbol(password[i]))
        hasSym = true;
}
if (hasLow && hasUpp && hasDig && hasSym)
{
    return true;
}
else
{
    return false;
}



}
CaldasGSM
  • 3,032
  • 16
  • 26
  • Thank you, I got a little bit confused with placement. Thanks for actually showing me what I did wrong. – Chris Salazar Oct 15 '14 at 17:03
  • The only problem I am having now is that I am getting an error " no suitable conversion function from "std::string" to "const char*" exists. Which I read can be fixed using const.char * c, but would that be the right way for this specific instance? It says that it would just pass the string to the function. – Chris Salazar Oct 15 '14 at 17:37
  • yes you do not need to use the string from std.. you can simply use char* and it will work just as well... – CaldasGSM Oct 15 '14 at 18:04
0

In this line you declare 2 variables type string:

string strlen, password;

in this line you are trying to use variable strlen as a function and put there a variable string1 that does not exist:

for(int i = 0; < strlen(string1); ++i)

and you try to use string1 further in the code.

Looks like you copy paste code from somewhere without trying to understand what you are doing.

Slava
  • 43,454
  • 1
  • 47
  • 90
  • I didn't copy and paste, although I did try to find help on here prior to asking this, and found something like this. I am trying to understand how this works exactly. Like I said I'm not very good at c++ yet. – Chris Salazar Oct 14 '14 at 17:49
0
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <cctype>

using namespace std;

// Globals on top
bool hasUpp = false;
bool hasLow = false;
bool hasDig = false;
bool hasSym = false;

// Password Goes Here According to your code
string password;

int main() {
    cout << "Please enter your password!" << endl;
    cin >> password;

    int stringlength = strlen(password);


    // check string length here


    for(int i = 0; i < stringlength; i++) { // As said, the i was missing, also normally you see i++ in code

        if (isupper(string1[i]))
            hasUpp = true;
        if (islower(string1[i])) //Do all of these if statements seem correct? Yes
            hasLow = true;
        if (isdigit(string1[i]))
            hasDig = true;
        if (issymbol(string1[i]))
            hasSym = true;

        // dont check string length here
    }
}

I updated your code, it may or may not compile, but I fixed the errors I saw.

Evan Carslake
  • 2,267
  • 15
  • 38
  • 56