Jump to content

Yet Again More C++ Help


Archerzz
 Share

Recommended Posts

How do you get a C++ program to open another .exe file like it is going to prompt you for a password then if you get it right it will open firefox.exe but if you get it wrong it will open a batch file that will log you off.

Share this post


Link to post
Share on other sites

Ok, this thread is now hijacked! (Archerzz and I are working on the same program and I don't feel like starting a new thread)

 

Basically in this portion of code I am trying to display a string after being converted from an array of chars. It works all right except for one thing. The newly created string prints what I typed but is followed by a load of gibberish. If I type "hello", it prints "hello(insert random ASCII here)"

I am a total n00b at C++, so any help is much appreciated. Thanks.

 

 

          y=0;
         while(y!=5)
         {
              password[y] = getch();
              printf("*");
              y++;
         }
         string temp=password;
         cout << temp;

Share this post


Link to post
Share on other sites

Ok, this thread is now hijacked!  (Archerzz and I are working on the same program and I don't feel like starting a new thread)

 

Basically in this portion of code I am trying to display a string after being converted from an array of chars.  It works all right except for one thing.  The newly created string prints what I typed but is followed by a load of gibberish.  If I type "hello", it prints "hello(insert random ASCII here)"

  I am a total n00b at C++, so any help is much appreciated.  Thanks.

          y=0;
         while(y!=5)
         {
              password[y] = getch();
              printf("*");
              y++;
         }
         string temp=password;
         cout << temp;

574516[/snapback]

 

Have you properly terminated your string? I can't see your whole code, so I don't know.

 

After you build a character array out of single characters, you need to "terminate" it. Basically, you put a special character at the end that says "the string is done". It works like this: If you want to make a char array that has the word "cool" in it, it's contents would look like this

 

Char[0] = 'c'

Char[1] = 'o'

Char[2] = 'o'

Char[3] = 'l'

Char[4] = '/0'

 

The '/0' is the null character and it ends any string. Yes, /0 is actually two characters, but the compiler will treat it like one.

 

When you do a cout (for instance) on that string, the output stream will get to the '/0' and know the string is done. If the '/0' is not there, the output stream will often continue outputting whatever junk happens to be in memory after the string.

 

Not sure if this is your problem, but it is a common mistake with strings that can cause what you're seeing.

Share this post


Link to post
Share on other sites

hmm, ok but the gibberish is also present after each individual char.  How would I tell each char that it is done, without deleting the char?

574532[/snapback]

Nah, you don't want to terminate after every char, that's no good.

 

I just reinstalled Dev C++ because I wanted to check this out myself :P

 

I did this:

#include <iostream>
#include <iomanip>
#include <string>
#include <conio.h>

using namespace std;

int main()
{
        
        char password[5];
        int y;
        
        cout << "Enter Password:  ";
        
        y=0;
        while(y!=5)
        {
             password[y] = getch();
             printf("*");
             y++;
        }
        password[5] = '\0';
        cout << "\nYou entered:  '" << password << "'\n";
        
        cout << ("Press ENTER to continue.\n");
        getchar (); // wait for input

  return 0;
  
}

 

That works like I think you want it to. Give it a shot and let me know if it works...

 

PS

I always get my slashes mixed up. The null char is '\0'.... NOT '/0' :P

 

EDIT###

Tested it without the conversion to string, and it works just fine. The string.h header should let you treat char* like a string in most cases. I updated the code above. That works for me, at least how I think it should :)

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...