Errors in passing data to fuctions
Sep 16, 2014 at 8:42am UTC
So I basically have to write a simple program that accepts a username and password then requires you to confirm it again. I also have to make it so that when you confirm the password "*" appear instead of what you are typing. The problem is that when I try to compare the two inputs, it always results in failure. I tried printing out the inputted username and password and it only results in gibberish.
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
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#include<iostream>
using namespace std;
char username_set()
{
char username[20];
cout << "Please set your username:" ;
cin >> username;
return (username[20]);
}
char password_set()
{
char password[20];
cout << "Please set your password:" ;
cin >> password;
return (password[20]);
}
int user_confirm(char input[20])
{
int a;
char output[20],attempt[20];
cout << "Please input your username:" ;
cin >> attempt;
a=strcmp(input,attempt);
if (a==0)
{
cout << "Username is correct" ;
}
else
{
cout << "Username is wrong. Attempt = " << attempt << input;
}
return (a);
}
int pass_confirm(char input[20])
{
int a;
char hold,b[20];
string attempt="" ;
cout << "\nPlease input your password:" ;
hold=getch();
while (hold!=13)
{
attempt.push_back(hold);
printf("*" );
hold=getch();
}
strcpy(b,attempt.c_str());
a=strcmp(b,input);
if (a==0)
{
cout << "\nPassword is correct" ;
}
else
{
cout << "\nPassword is wrong. Attempt = " << b << input;
}
return (a);
}
int main(void )
{
int a,b,c,attempt_user,attempt_pass;
char pass[20],user[20];
cout << "Registration process:\n" ;
user[20]=username_set();
pass[20]=password_set();
printf("\nIn order to continue, please enter your username and password\n" );
attempt_user=user_confirm(user);
attempt_pass=pass_confirm(pass);
getch();
}
Sep 16, 2014 at 9:02am UTC
return (password[20]);
It reads as "return 21st character from 20 character array"
user[20]=username_set();
reads as "get a single character from function and write it to the 21th place in 20 character array
Read more on how arrays operate.
About passing arrays to functions.
http://www.cplusplus.com/doc/tutorial/arrays/
Do not use c-strings, use std::string instead.
Do not mix C++ streams and C IO.
Sep 16, 2014 at 9:43am UTC
Thanks MiiNiPaa, managed to fix it.
Topic archived. No new replies allowed.