why does my code work?

Ok, since this code is too long for you to read, I labelled the code. Firstly, i created a pointer called user1 while defining function userpass. The function accepts the memory location of 2 arrays as it's arguments which get transferred to user1 and pass1 pointers. So my question is at the "do" loop, which i marked with another comment "!!!!!!!". When i "cout" user1, it displays the username i entered at the do loop marked with comment "AAAAAAAAA". My question is, why doesnt the cout statement display the memory address of the "user" string from the main(), and why does it display a string instead?

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
  #include<iostream>
#include<conio.h>
#include<windows.h>

using namespace std;
#define endp cin.ignore()

void userpass(char *user1,char *pass1)                           
{
	system("cls");
	int ct1,ct2,i;
	
	cout<<"**Function USERPASS running**";
	
	cout<<"\n\nEnter USERNAME: ";	
	ct1=0;
	
	do
	{
		*user1=getche();	                       //AAAAAAAAA
		ct1++;
		user1++;	
	}while(*(user1-1)!='\r'&&ct1<=50);
	
	user1-=ct1;	 //bringing user1 back to start of username
	
	cout<<"\nEnter PASSWORD: ";	
	ct2=0;
	
	do
	{
		
		//hiding the password code
		system("cls");
		cout<<"**Function USERPASS running**";		
	    cout<<"\n\nEnter USERNAME: "<<user1;	          //!!!!!!!!! 
	    cout<<"\nEnter PASSWORD: ";	    
	    for(i=0;i<ct2;i++)
	    cout<<"*";
	    //hidden
		
		*pass1=getch();	
		ct2++;
		pass1++;
		
		
					
	}while(*(pass1-1)!='\r'&&ct2<=50);
	
	cout<<"\n\n**Function USERPASS ends.";
}

void printup(char *user2,char *pass2)
{
	system("cls");
	cout<<"**Function PRINTUP running**";
	
	cout<<"\n\nUSERNAME: ";	
		
	do
	{
		cout<<*user2;	
		user2++;	
	}while(*(user2)!='\r');
	
	cout<<"\nPASSWORD: ";	
		
	do
	{
		cout<<*pass2;	
		pass2++;	
	}while(*(pass2)!='\r');
	
	cout<<"\n\n**Function PRINTUP ends.";
}

int main()
{
	char user[50],pass[50];
	char ans;
	cout<<"Would you like to create an account?: ";
	ans=getche();
		
	if(ans=='y'||ans=='Y')
	userpass(&user[0],&pass[0]);
	else
	return 0;
	
	// if you want to view your username and password
	cout<<"\n\nWould you like to view your username and password? ";
	ans=getche();
	
	if(ans=='y'||ans=='Y')
	printup(&user[0],&pass[0]);
	else
	return 0;
	//
	
	endp;
	return 0;		
}
If I have understood your question correctly, then UNBELIEVABLY this is the 3rd time I've come across this question in 2 days!!! Following link may help you understand better:

http://www.cplusplus.com/forum/beginner/109640/#msg597903
thanks for the reply, but that just took me to a topic which does not have a certain answer. if you know it, i would like it if you could explain it, because i still don't understand.
When you do
 
cout<<"\n\nEnter USERNAME: "<<user1; // where user1 is a pointer to a char 

The operator << interprets it as a CString, rather than a pointer. This is why the entire string is printed out. If you want to output the address, you will have to typecast it as a void pointer.
 
cout << "address of char: " << static_cast<void *>( user1 ) << endl;
i actually wanted to output the string itself. it was just that when i was reviewing the code, i realised there was some abnormality. thanks for the help!
You are {not} adding a null terminator to your char[]s when you take the user name and password.

Edit: I meant to say you are not adding...
Last edited on
do you mean when i am calling my function userpass? how does that affect my program?
> My question is, why doesnt the cout statement display the memory address of the "user" string
> from the main(), and why does it display a string instead?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int main()
{
    const char cstr[] = "abcd" ;
    const void* p = cstr ;

    std::cout << cstr ;
    // overload resolves to: free function which prints a c-style string
    // std::ostream& operator<< ( std::ostream&, const char* ) ;
    std::operator<< ( std::cout, cstr ) ;

    std::cout << p ;
    // overload resolves to: member function which prints the value of a pointer
    // std::ostream::operator<< ( const void* ) ;
    std::cout.operator<< (p) ;
}

http://ideone.com/wdO7Lo
Topic archived. No new replies allowed.