[C++] figuring out the char

Started by
13 comments, last by vrok137 16 years, 9 months ago
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;
}

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.
In c++, text is represented by std::string.
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.
Thanks, I never knew to use char* instead of char, I guess now I do. Thanks for the quick responses/help :)!
#include <stdio.h>#include <string>class Password {private:	std::string password;public:	void SetPass(std::string pass)        {            password = pass;                    }	std::string GetPass()        {            return password;        }};
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++.
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;}
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.

Beginner in Game Development?  Read here. And read here.

 

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 pointlessvoid 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')".

This topic is closed to new replies.

Advertisement