Hey could someone help me bout my problem....
can someone tell me how to make an asterisk frame using for loop statement in C++

and also 1 thing....
how can i display an input letters into an asterisk...
eg..
username: bulbulito
password: ********

Recommended Answers

All 6 Replies

>>can someone tell me how to make an asterisk frame using for loop statement in C++

Give it your best shot and post a specific question with code/error statements/etc as needed. This is a common task that any number of people could help you with, but most of us won't write the code for you.

the other question requires use on nonstandard functions. Something like this might work.

if(kbhit())
{
char ch = getch();
cout << "*";
}

kbhit() detects pushing a key on the keyboard but doesn't indicate what the key is/was.

getch() stores the input into a variable without echoing the input to the screen. It is in the conio.h header file, I believe, which isn't a standard file, and may or may not come with your compiler.

commented: Great answer to the question, and it helped me a lot. +1

>how to make an asterisk frame using for loop statement in C++
A frame for what? The top left and bottom are easy, and if the strings are all the same length, the right is easy too:

#include <iostream>
#include <string>
#include <vector>

int main()
{
  using namespace std;

  vector<string> v;

  v.push_back("data 1");
  v.push_back("data 2");
  v.push_back("data 3");

  cout << "**********\n";
  for (vector<string>::size_type i = 0; i < v.size(); ++i)
    cout << "* " << v[i] << " *\n";
  cout << "**********\n";
}

The hard part is when the strings can vary in length. That's when you need to find out what the longest string is and pad the inside of the frame with spaces so that the right side asterisks are all lined up neatly.

>how can i display an input letters into an asterisk...
There's not a standard way to do that in C++. You have to use a raw input using a non-standard mechanism like getch that doesn't buffer and doesn't echo. That way you can take over control of the display and do it all yourself:

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

int main()
{
  using namespace std;

  int ch;

  while ((ch = getch()) != '\r') {
    // Process the character

    cout << '*';
  }
}

i don't know any alternative way of clearing the screen...so i guess this code of mine is not portable but it works...in a more fancy way.

#include <iostream>
#include <conio.h>
#define MAX 256

int main()
{
  using namespace std;

  char ch, len;

  while ((ch = getch()) != '\r') {
    if( ch == 8 ) { system( "CLS" );  --len; for( int i = 0; i < len; ++i )cout << '*'; continue; }
    //process the char
    ++len; system( "CLS" ); for( int i = 0; i < len; ++i )cout << '*';
  }
  return 0;
}

>i guess this code of mine is not portable but it works...
The whole task is not portable, so it's all good. :)

// Has unexpected side effects
system( "CLS" ); for( int i = 0; i < len; ++i )cout << '*';

There are a few problems with this. First and foremost is that system() is a security hole. Most of the time you'll be thinking about portability in that the CLS program may or may not exist, but what if CLS does exist and has a malicious payload? One way for a hacker to break through your program is to replace the system's CLS with a higher priority version that he writes and let your code call it.

The unexpected side effect if CLS exists and clears the screen safely is that it clears all of the screen. Meaning everything on the screen, even if it doesn't belong to your program, is wiped away. The first problem with this side effect is that it doesn't play well with other programs running on the console. The second problem is that even if you own the console, you have to redisplay everything if there's any formatting. For example, what if your program's interface looks like this?

**********************************************************
******************* My Awesome Program *******************
**********************************************************

----------- Main Menu -----------
1) Personal Info

	----------- Personal Info Menu -----------
	1) Log In

		: Please enter your user name> Radical Edward
		: Please enter your password> _

	2) View Personal Info
	3) Edit Personal Info
	4) Log Out
	5) Back

2) About
3) Quit

That's a lot of redrawing that needs to be done. :| C++ has a special escape character that stands for a backspace, and you can use that to simulate the logic of backspacing in your code. It makes handling the input harder, but it also fixes both of the unexpected problems with clearing the screen:

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

int main()
{
  using namespace std;

  string password;

  while (true) {
    int ch = getch();

    if (ch == '\r') {
      cout << '\n';
      break;
    }
    else if (ch == '\b') {
      // Handle a backspace
      if (!password.empty())
        password.erase(password.size() - 1, 1);

      cout << "\b \b";
    }
    else {
      // Add a new character
      password += ch;
      cout << '*';
    }
  }

  cout << "The password is '" << password << "'\n";
}

Wow, thanks! i didn't know there's such a way to handle backspaces and send them out through the stream...awesome stuff, thanks Ed!

Dude thanks a lot for the help......
this really sucks me a lot....
coz this program is my case study.....
damn man...
wow...
your great!!!!
thanx really for the help!!!!!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.