Jump to content
  • Advertisement
Sign in to follow this  
vrok137

[C++] figuring out the char

This topic is 4116 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Sorry if this is such a lame question, but I need help in getting this thing right. I've looked through countless resources, but none so far have granted to my favor. I am working on a console version of an accounting program, specifically the basic password protection part. This module divides into the following categories: - main.cpp - Password.cpp - Password.h The problem I have is narrowed down to the following functions: Password.h/.cpp functions: - char password[20]; - int SetPass(); - int GetPass(char *arg1); My ultimate question and fustration is in trying to get the program to print the password on the console screen. When compiled and executed, all that is printed out is the first character of "google" (hence 'g'). I know I must be doing something wrong, I just do not know what exactly. If anyone may have the slightest idea as to this, help would be greatly appreciated. Thanks. PS: For those inquiring why I do not use another variable type such as STRING, the success of my Password module depends on the specific use of char. Password.h:
#include <stdio.h>

class Password {
private:
	char password[20];
	char *ptr; // not important
	int GETCH(void); // not important
public:
	int BreakPass(); // not important
	int SetPass(char *arg1);
	char GetPass();
};

Password.cpp function definitions:
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include "Password.h"

/*
Other function definitions omitted
*/
int Password::SetPass(char *arg1) {
	strcpy_s(password, arg1); //copies arg. input to password variable
	return 0;
}

char Password::GetPass() {
	return *password; //returns password
}

main.cpp
#include <stdio.h>
#include <iostream>
#include <string.h>
#include "Password.h"

using namespace std;

int main()
{
	Password pwd;

	pwd.SetPass("google");
	cout << pwd.GetPass(); // prints password to user

	return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
Use std::string, and you won't have to deal with these things. :-)

EDIT: Sorry, forgot to actually answer.

Your function is only returning a single character. Its return type should be char* rather than char, and you chould return password, not *password.

*password dereferences the pointer and just gives you the actual character at that address.

Share this post


Link to post
Share on other sites
You return single character:
char Password::GetPass() {
return *password; //returns password
}

THUGSTOOLS

[suorce]char *Password::GetPass() {
return password; //returns password
}
[/source]Is what returns the string.

Of course, C with classes isn't really useful. std::string would take care of any such problems.

Share this post


Link to post
Share on other sites
Thanks, I never knew to use char* instead of char, I guess now I do. Thanks for the quick responses/help :)!

Share this post


Link to post
Share on other sites
#include <stdio.h>
#include <string>

class Password
{
private:
std::string password;
public:
void SetPass(std::string pass)
{
password = pass;
}

std::string GetPass()
{
return password;
}
};

Share this post


Link to post
Share on other sites
Quote:
Original post by vrok137
Thanks, I never knew to use char* instead of char, I guess now I do. Thanks for the quick responses/help :)!


That's not what you were told though. Use std::string instead of char*. [wink]

char* is for C programs, you claim to be using C++.

Share this post


Link to post
Share on other sites
Quote:
Original post by Spoonbender
That's not what you were told though. Use std::string instead of char*. [wink]

char* is for C programs, you claim to be using C++.


I would use std::string, but my BreakPass() method (which basically conceals the password as its being typed) requires the variable type char. Unless there is a way to convert between std::string and char effectively i'm open for ideas.

BTW here is the BreakPass() function definition:

int Password::GETCH(void) {
return _getch(); // does not echo input
}

int Password::BreakPass() { //uses global var. password and ptr
char input;
int count;

count = 0;

printf("enter your password: ");

do
{

input = GETCH();
if(input == '\xD') break; // enter pressed?
putc('*', stdout);
ptr = &input;
password[count] = *ptr;
count++;

}while(count < 20);

password[count++] = '\0'; // terminate the string

printf("\nyour password: %s\n", password);

return 0;
}

Share this post


Link to post
Share on other sites

int Password::BreakPass() {
//assumes password is declared std::string password
//also uses std::cin for input and std::cout for output

char input;
int count;

count = 0;

std::cout << "Please enter password: ";

do
{
std::cin >> input;
if(input == '\xD') break; // enter pressed?
std::cout << '*';
password[count] = input;
++count;
}while(count < 20);


Now someone will rearrange my (your) loop, but that's a C++ version of what you're trying to do.

Share this post


Link to post
Share on other sites
You can still use Alpha_ProgDes's code with your getch function:


// don't return an int
// since you always return 0, its kind of pointless
void Password::BreakPass() {
// assume we no longer care about how long the password is
// yay std::string!
// if you need to call this function multiple times
// you can empty the old password thusly:
// password.clear();

std::cout << "Please enter password: ";

char input;
while( (input = _getch()) != '\xD' )
{
std::cout << '*';
password.push_back(input);
}
std::cout << "\nYou password is: " << password << '\n';
}



In case you havent come across a loop with a condition like that,
(input = _getch()) != '\xD'
means
"assign input to _getch(). Then evaluate (assigned_value != '\xD')".

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!