Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. char array issues

char array issues

Scheduled Pinned Locked Moved C / C++ / MFC
data-structures
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gamefreak2291
    wrote on last edited by
    #1

    #include
    #include
    #include
    using namespace std;
    char password1[25];
    int x=1;
    int i=0;
    int q;
    int main()
    {
    do{

    cout << "Enter a test password: " ;
    for(i=0;i<25;i++){
           password1\[i\]=getch();
           if(((password1\[i\]>=65)&&(password1\[i\]<=90)) || ((password1\[i\]>=97)&&(password1\[i\]<=122))
           || ((password1\[i\]>=48)&&(password1\[i\]<=57)) || ((password1\[i\]>=35)&&(password1\[i\]<=38))
           || ((password1\[i\]>=40)&&(password1\[i\]<=42)) || ((password1\[i\]==33))||((password1\[i\]==64))
           || ((password1\[i\]==94)) || ((password1\[i\]==32))){cout << "\*";
           i+1;}
           if(password1\[i\]==13){i-1; goto end;}
           else{i-1;
                cout << "";}
           }
           end:
                   for (i; i < strlen(password1); i++){
        password1\[i\] = '\\0';}
    cout << "\\nYour password is ";
    for(q=0;q<i;q++){cout><< password1\[q\];}
    Sleep(2000);
    

    if(password1[q]='p','a','s','s','w','o','r','d','\0'){goto end1;}
    system("cls");
    }while(x=1);
    end1:
    cout << "\nIT WORKS!";
    getch();
    return 0;
    }

    I cannot seem to get the program to check the password properly, it either doesn't work. Or anything works

    D S 2 Replies Last reply
    0
    • G gamefreak2291

      #include
      #include
      #include
      using namespace std;
      char password1[25];
      int x=1;
      int i=0;
      int q;
      int main()
      {
      do{

      cout << "Enter a test password: " ;
      for(i=0;i<25;i++){
             password1\[i\]=getch();
             if(((password1\[i\]>=65)&&(password1\[i\]<=90)) || ((password1\[i\]>=97)&&(password1\[i\]<=122))
             || ((password1\[i\]>=48)&&(password1\[i\]<=57)) || ((password1\[i\]>=35)&&(password1\[i\]<=38))
             || ((password1\[i\]>=40)&&(password1\[i\]<=42)) || ((password1\[i\]==33))||((password1\[i\]==64))
             || ((password1\[i\]==94)) || ((password1\[i\]==32))){cout << "\*";
             i+1;}
             if(password1\[i\]==13){i-1; goto end;}
             else{i-1;
                  cout << "";}
             }
             end:
                     for (i; i < strlen(password1); i++){
          password1\[i\] = '\\0';}
      cout << "\\nYour password is ";
      for(q=0;q<i;q++){cout><< password1\[q\];}
      Sleep(2000);
      

      if(password1[q]='p','a','s','s','w','o','r','d','\0'){goto end1;}
      system("cls");
      }while(x=1);
      end1:
      cout << "\nIT WORKS!";
      getch();
      return 0;
      }

      I cannot seem to get the program to check the password properly, it either doesn't work. Or anything works

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      gamefreak2291 wrote:

      if(((password1[i]>=65)&&(password1[i]<=90)) || ((password1[i]>=97)&&(password1[i]<=122)) || ((password1[i]>=48)&&(password1[i]<=57)) || ((password1[i]>=35)&&(password1[i]<=38)) || ((password1[i]>=40)&&(password1[i]<=42)) || ((password1[i]==33))||((password1[i]==64)) || ((password1[i]==94)) || ((password1[i]==32))){cout << "*"; i+1;}

      Have you considered isdigit(), isalnum(), isalpha(), isxdigit(), etc? It won't necessarily solve your problem (I've not looked to see what the actual problem is), but making it easier to read won't hurt.

      gamefreak2291 wrote:

      if(password1[q]='p','a','s','s','w','o','r','d','\0'){goto end1;}

      Is this supposed to do something?

      gamefreak2291 wrote:

      }while(x=1);

      Look at this line real close. Also, your i+1 and i-1 statements are not doing what you expect.

      gamefreak2291 wrote:

      for(q=0;q<i;q++){cout><< password1[q];}

      Does this even compile? What warning level are you using?

      "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      G CPalliniC 2 Replies Last reply
      0
      • D David Crow

        gamefreak2291 wrote:

        if(((password1[i]>=65)&&(password1[i]<=90)) || ((password1[i]>=97)&&(password1[i]<=122)) || ((password1[i]>=48)&&(password1[i]<=57)) || ((password1[i]>=35)&&(password1[i]<=38)) || ((password1[i]>=40)&&(password1[i]<=42)) || ((password1[i]==33))||((password1[i]==64)) || ((password1[i]==94)) || ((password1[i]==32))){cout << "*"; i+1;}

        Have you considered isdigit(), isalnum(), isalpha(), isxdigit(), etc? It won't necessarily solve your problem (I've not looked to see what the actual problem is), but making it easier to read won't hurt.

        gamefreak2291 wrote:

        if(password1[q]='p','a','s','s','w','o','r','d','\0'){goto end1;}

        Is this supposed to do something?

        gamefreak2291 wrote:

        }while(x=1);

        Look at this line real close. Also, your i+1 and i-1 statements are not doing what you expect.

        gamefreak2291 wrote:

        for(q=0;q<i;q++){cout><< password1[q];}

        Does this even compile? What warning level are you using?

        "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        G Offline
        G Offline
        gamefreak2291
        wrote on last edited by
        #3

        no, i have not considered the others, because it took long enough to get the ascii values going.

        DavidCrow wrote:

        if(password1[q]='p','a','s','s','w','o','r','d','\0'){goto end1;}

        yes, that takes you to the end which prints that the password works.

        DavidCrow wrote:

        }while(x=1);

        okay, im looking at it. It does exactly what it is supposed to, that bit of code is only in the program at this point for debugging purposes, it allows the user to keep trying to get the correct password. As for i+1, and i-1.. i++ and i-- was not working properly, I changed them to +1 and -i, then had them print to make sure they were working. And of course it compiles. the reason why "for(q=0;q<i;q++){cout><< password1[q];}" is in the code is because password1[i] allows you to enter 25 characters max, and if you only enter 5 characters, the rest of them are filled in with null terminators. However, I don't want my password to be full of null terminators, so if i input 5 characters, i is currently 5, so i have q=i; then i fills out with the rest of the array with null terminators. Then the program prints password1[q] from 0-5, which is really like printing password1[0], password1[1], password1[2], password1[3], password1[4], and password1[5]. I previously had the program print the entire array however it was over lapping on multiple tries, so now it only prints the needed numbers. Thanks again for the help

        M 1 Reply Last reply
        0
        • G gamefreak2291

          no, i have not considered the others, because it took long enough to get the ascii values going.

          DavidCrow wrote:

          if(password1[q]='p','a','s','s','w','o','r','d','\0'){goto end1;}

          yes, that takes you to the end which prints that the password works.

          DavidCrow wrote:

          }while(x=1);

          okay, im looking at it. It does exactly what it is supposed to, that bit of code is only in the program at this point for debugging purposes, it allows the user to keep trying to get the correct password. As for i+1, and i-1.. i++ and i-- was not working properly, I changed them to +1 and -i, then had them print to make sure they were working. And of course it compiles. the reason why "for(q=0;q<i;q++){cout><< password1[q];}" is in the code is because password1[i] allows you to enter 25 characters max, and if you only enter 5 characters, the rest of them are filled in with null terminators. However, I don't want my password to be full of null terminators, so if i input 5 characters, i is currently 5, so i have q=i; then i fills out with the rest of the array with null terminators. Then the program prints password1[q] from 0-5, which is really like printing password1[0], password1[1], password1[2], password1[3], password1[4], and password1[5]. I previously had the program print the entire array however it was over lapping on multiple tries, so now it only prints the needed numbers. Thanks again for the help

          M Offline
          M Offline
          Maximilien
          wrote on last edited by
          #4

          gamefreak2291 wrote:

          okay, im looking at it. It does exactly what it is supposed to, that bit of code is only in the program at this point for debugging purposes, it allows the user to keep trying to get the correct password.

          better get a good C programming book.

          This signature was proudly tested on animals.

          1 Reply Last reply
          0
          • D David Crow

            gamefreak2291 wrote:

            if(((password1[i]>=65)&&(password1[i]<=90)) || ((password1[i]>=97)&&(password1[i]<=122)) || ((password1[i]>=48)&&(password1[i]<=57)) || ((password1[i]>=35)&&(password1[i]<=38)) || ((password1[i]>=40)&&(password1[i]<=42)) || ((password1[i]==33))||((password1[i]==64)) || ((password1[i]==94)) || ((password1[i]==32))){cout << "*"; i+1;}

            Have you considered isdigit(), isalnum(), isalpha(), isxdigit(), etc? It won't necessarily solve your problem (I've not looked to see what the actual problem is), but making it easier to read won't hurt.

            gamefreak2291 wrote:

            if(password1[q]='p','a','s','s','w','o','r','d','\0'){goto end1;}

            Is this supposed to do something?

            gamefreak2291 wrote:

            }while(x=1);

            Look at this line real close. Also, your i+1 and i-1 statements are not doing what you expect.

            gamefreak2291 wrote:

            for(q=0;q<i;q++){cout><< password1[q];}

            Does this even compile? What warning level are you using?

            "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            Welcome again in the CP's memorable quotes page. :-D

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            In testa che avete, signor di Ceprano?

            1 Reply Last reply
            0
            • G gamefreak2291

              #include
              #include
              #include
              using namespace std;
              char password1[25];
              int x=1;
              int i=0;
              int q;
              int main()
              {
              do{

              cout << "Enter a test password: " ;
              for(i=0;i<25;i++){
                     password1\[i\]=getch();
                     if(((password1\[i\]>=65)&&(password1\[i\]<=90)) || ((password1\[i\]>=97)&&(password1\[i\]<=122))
                     || ((password1\[i\]>=48)&&(password1\[i\]<=57)) || ((password1\[i\]>=35)&&(password1\[i\]<=38))
                     || ((password1\[i\]>=40)&&(password1\[i\]<=42)) || ((password1\[i\]==33))||((password1\[i\]==64))
                     || ((password1\[i\]==94)) || ((password1\[i\]==32))){cout << "\*";
                     i+1;}
                     if(password1\[i\]==13){i-1; goto end;}
                     else{i-1;
                          cout << "";}
                     }
                     end:
                             for (i; i < strlen(password1); i++){
                  password1\[i\] = '\\0';}
              cout << "\\nYour password is ";
              for(q=0;q<i;q++){cout><< password1\[q\];}
              Sleep(2000);
              

              if(password1[q]='p','a','s','s','w','o','r','d','\0'){goto end1;}
              system("cls");
              }while(x=1);
              end1:
              cout << "\nIT WORKS!";
              getch();
              return 0;
              }

              I cannot seem to get the program to check the password properly, it either doesn't work. Or anything works

              S Offline
              S Offline
              Stuart Dootson
              wrote on last edited by
              #6

              gamefreak2291 wrote:

              if(password1[q]='p','a','s','s','w','o','r','d','\0'){goto end1;}

              I suspect that line should read

              if (0==strcmp(password1, "password")) { goto end1;}

              That's how you compare two strings, anyway. What you've done is assign 'p' to the q'th character in password (probably overwriting the null terminator, I suspect). The rest of the expression is repeated application of the comma operator[^].

              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups