username and password

hey! im doing a project using c++ programme. i need help on how to create a username and password in microsoft visual studio 2010. please help me!
A good start would be to look at some of the tutorials on this site. for example, getting user input from the command line:
http://www.cplusplus.com/doc/tutorial/basic_io/

however i use <stdio.h> is it the same?
Last edited on
No.
That's for C input and output like printf and scanf. If you are definitely doing this in C++ you want to be using cout and cin for output and input, and use:
#include<iostream>
ohh my mistake. im using c input and output. how am i supposed to create the username and password?
So you don't want to write this in C++ then?
if you want to use C I suggest googling "C input and output". If you want to use C++, I'd stick with my first suggestion and go through some of the tutorials on this site.

People on this site can help you a bit more if you actually post some of your code.
this is my code.

#include<stdio.h>
void main(void)

char UserName[] = "iluvcake";
char PassWord[] = "Chocolate";

printf("Please enter your username.\n");
scanf("%s", UserName);
printf("Please enter your password: \n");
scanf("%s", PassWord);

if (UserName == "iluvcake")
{
if (PassWord == "Chocolate")
{
printf("Welcome!\n");
}
}
else
{
printf("The user name or password you entered is invalid.\n");
}


i dont know how to create a loop in this. so if the person enters a wrong user name and password.. it should say invalid username and password and starts from the start again
Last edited on
Ah it's C. I'm no use to you then sorry :)

But I see some mistakes e.g:

1
2
3
4
5
void main(void)
...
 char UserName[] = "iluvcake";
 char PassWord[] = "Chocolate";
...


need to be
1
2
3
4
5
intmain(void)
{  // <--- don't forget your braces!
 char UserName[] = "iluvcake";
 char PassWord[] = "Chocolate";

be carefull with buffer overrun
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cstring>

int main(){
	int n=2;
	bool access=false;
	char password[7];
	std::cin>>password;

	if(strcmp(password, "******")==0)
		access=true;
	if(access)
		std::cout << n << " misiles launched\n";
	if(not access)
		std::cout << "Access denied\n";
	return 0;
}
we_will_we_will_rock_you
2002740599 misiles launched
Access denied



> i dont know how to create a loop in this
1
2
3
4
while( !password(input) ){
   std::cerr << "Invalid password. Enter it again.\n";
   //...
}



> UserName == "iluvcake"
to compare c-strings you need to use the strcmp() function
(pay attention, as your code will compile but it compares the pointers, not the content)
Topic archived. No new replies allowed.