Console closed after user input 2

When i type username , its works fine but when i write write password , it closes immediately.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
void main(){
	char username;
	char password;
	printf("Please enter your fuckin' username: \n");
	cin>>username;
	printf("Please enter your freakin' password niggah: \n");
	cin>>password;
	cout << "Your username is "<< username << " and your password is " << password << endl;
	getch();
	system ("PAUSE");
	}
Last edited on
Line 6: usename is a single char. Assuming you're entering more than a single character, the remaining characters are going to remain in the input buffer.

Line 11: Since there are characters available in the input buffer, this statement removes the next available character (the second character of whatever username you entered).

Line 13: Again, assuming characters are available in the input buffer, the getch() will be satisfied immediately.

Change your declaration of username and password to character arrays of a reasonable length.

1
2
  char username[10];
  char password[10];

If you want to keep them aschar though you could also usecin.sync(); after each input. However if you want to use several characters instead of a char array with a definite length perhaps use a string instead.
Topic archived. No new replies allowed.