getch....help

i can not print my "pass" eventhough i input a tons of thing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>
#include<cstring>
#include<conio.h>
using namespace std;
int main(){

char pass;



while(pass!=13)
{
pass=getch();
cout<<".";
}
cout<<pass;
try 10. some systems use 10, some 13.
you might also initialize pass -- it could randomly start at 13 from time to time which would cause weird behavior.
Last edited on
pass is a char, meaning it can contain only 1 character.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cstring>
#include <conio.h>
#include <cctype>

using namespace std;

int main()
{
    char pass;
       
    while( pass!=13)
    {
        pass = getch();
        cout << ".";
    }
    
    if (isprint(pass))
        cout << pass;
    else
        cout << "ASCII value : " << (int)pass;
                        
}
1
2
3
4
5
6
    while( pass!=13) //loop till pass is 13
    {
        pass = getch();
        cout << ".";
    }
//so here pass is 13 
Hello phongvants123

i can not print my "pass" eventhough i input a tons of thing

As whitenite1 pointed out "pass" only holds one character. So, by the time the while loop finishes the only thing in "pass" is 13.

Consider this to get something that you can use:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
//#include <cstring>  // <--- Not needed with what you have.
#include <string>
#include <conio.h>
#include <cctype> // <--- May not be needed.
int main()
{
	char pass{};  // <--- Always good practice to initialize your variables.
	std::string str{};

	while (pass != 13)
	{
		pass = _getch();
		std::cout << ".";
		str.push_back(pass);  // <--- Builds a std::string.
	}

	std::cout << '\n' << str << std::endl;  // <--- Prints the string built in the while loop.

        //  I use this to keep the console window open, so it does not close to early.
	std::cout << "\n\n\n\n Press anykey to continue";
	_getch();

	return 0;
}


This way you have "str" to use later in the program to compare with a stored password.

Hope that helps,

Andy
"Andy" makes any thing become crystal clear....really really apreciate that


beside, i wanna say thanks to chervil, whitenight,johnny for details....
you guys just make my day




Last edited on
Topic archived. No new replies allowed.