I am writing a program for my college class :eek: It has to record a password, but it cant display it on the screen. It is a unix based enviroment. Does this look correct to you guys?

main()
{
  int  i;
  char buffer[80];            
  initscr();                

  printw("Please enter a password => ");    
  refresh();                
  noecho();                
                    
  while((buffer[i] = getch()) != '\n') i++;

  printw("\nPassword is %s - press return to continue.",buffer);
  refresh();
  getch();

  endwin();                 }

I see the program must be using curses?? Are you allowed to use non-ansi standard functions?

>>Does this look correct to you guys
How it "looks" is unimportant. Does your program compile with 0 errors and does it work ok when you run and test it?

Yes it compiles with 0 errors. I just wanted to make sure it was the correct way to do this so i can get full credit.

Yes it uses curses.

I see the program must be using curses?? Are you allowed to use non-ansi standard functions?

>>Does this look correct to you guys
How it "looks" is unimportant. Does your program compile with 0 errors and does it work ok when you run and test it?

>>Are you allowed to use non-ansi standard functions?

You didn't answer that question. If not then you may not get any credit at all. Of course if your class is studying curses then the question is moot.

Not being familiar with whatever curses is I still have a couple concerns with the code as posted.

First, main() should be declared with a return type, just like any other function. That return type should be type int to be standard, though implementations of some compilers allow you to use void as a return type.

Second, C style strings are null terminated char arrays. To my knowledge, getch() doesn't automatically append a terminating null char to buffers like scanf() or >> or getline() will. Therefore I am concerned that the char array you call buffer is not truly a string, but instead remains a routine array of char, until you specifically add the null terminating char.

most programs that use passwords display an asterisk for each character typed. Your program probably should do that too, and it can be done within that while loop. If you look at the curses library you might find there it already a function that will do it for you. That loop also needs to check for buffer overflow, do not allow i to exceed the size of input buffer.

Thanks for all the help I got a 97% on the project. :)

Thanks for all the help I got a 97% on the project. :)

Congratulations, just keep on putting effort in your work and the rewards will definately come back to you. And if you encounter any other problem just dont hesitate to ask it.

Hope you are enjoying your stay at the community.

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.