User Login Verification [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
The program is Working perfectly fine .. but in strings if i want to restrict my username to 8 characters what should i do ? ?
cout << "Enter your Username it should have a maximum limit of 8 characters\n " << endl;
getline(cin, user, '\n');
i want here that it should restrict user for 8 characters .. if greater then 8 .. then it should keep asking
user (while loop ??)) until user enters characters less then 8 ..

int mystrcmp(const char *s1, const char *s2)
{
while (*s1==*s2)
{
if(*s1=='\0')
return(0);
s1++;
s2++; }
return 1;//return(*s1-*s2);
}
your int mystrcmp(const char *s1, const char *s2) was returning the difference b/w the ascii values of first non-matched chars in strings s1 and s2 and it isnt always 1 as your code supposed to.
A clean implementation of your code can be found here ideone link

You have a few confusions, notably between C++ strings and C strings.
Your mystrcpy function is an attempt to copy a C style string, but it's not necessary in C++. Here's how you do it in C++. Replace this
mystrcpy(new_user,user)
with this
new_user = user;
As you can see it's a bit easier to copy a C++ string than a C string. You can delete the mystrcpy function.
Similarly your mystrcmp function is an attempt to compare C strings. Again it's not necessary in C++. Replace this
f = mystrcmp(user2,new_user);
if (f==0)
{
cout << "Successful Login!! \n ";
}
with this
if (user2 == new_user)
{
cout << "Successful Login!! \n ";
}
Again you can see that comparing C++ strings is easier than comparing C strings. Again you can delete the mystrcmp function.
There a few other errors, in the logic, and some missing semi-colons, but I'll leave you do figure those out.

Your code is pretty much all over the place. For instance:
You need to #include <string> if you're going to use std::string.
cin >> a; if ( a == 1 ) - what if the user doesn't enter an integer at all? You'll be comparing the value of an uninitialized int to 1, which puts you in undefined behavior land. You should check the return from cin, or at a minimum initialize a.
As John mentioned, you read in std::strings, and then attempt to treat them like C strings. Don't, just use new_user = user and if ( user2 == new_user ).
If the user doesn't "Enter 1 to enter Signup Screen" you take them to the signup screen anyway.
In your mystrcmp() function, you return 0 if *s1 == '\0', but you fail to check if *s2 == '\0'. This means two things - one, if s2 is shorter than s1, then you're going out of bounds and invoking undefined behavior again, and two, if s2 is longer than s1 but begins with s1 (e.g. if s1 contains "billy" and s2 contains "billybob") then your function will say they are equal when they are not.
Also in your mystrcmp() function, this: return(*s1-*s2) will either always return 0 less the value of the element of s2 with the same index as the last index of s1, or it'll invoke undefined behavior if s2 is shorter, as already explained. Either way, it's almost certainly not something you want to return.
mystrcpy(target,source) is not a valid function definition. This function also only copies the first character of source to the first character of target.
When you loop following an incorrect entry, you check if (f == 0) and if (f == 1), but you never set f during this loop, so no matter what the user enters, you'll always just be checking what the initial result was, regardless of what they enter next.

Related

Problem storing characters as an string inside a while loop [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 months ago.
This post was edited and submitted for review 3 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I'm having problems with this simple example.
The program inquires as to how many letters are required to form a complete word. Then it will ask for each letter individually, which is fine, but I can't make the code save the value from the current character and the next one from the next iteration until the number of letters finishes the word and print it to confirm the word.
E.g. Let's say house, which has 5 letters.
int numbersOfCharacters=5;
int counter=0;
char character;
string phrase;
while (counter < numbersOfCharacters)
{
cout << "Introduce character's number" << counter << ": ";
cin >> character;
counter = counter + 1;
phrase=character+character; //I'm not sure if I need an array here.
}
cout << "Concatenated characters: " << phrase << endl;
The output is:
Introduce the character number 1: h
Introduce the character number 2: o
Introduce the character number 3: u
Introduce the character number 4: s
Introduce the character number 5: e
Concatenated characters: ?
And the expected output should be:
Concatenated characters: house
Update
Using John's comments I was able to resolve the issue. It's not a lack of debugging information but instead know the proper operator for this solution. Thanks also to Remy Lebeau for the detailed info.
The expression phrase=character+character; doesn't do what you think it does. You are taking the user's input, adding its numeric value to itself, and then assigning (not appending) that numeric result as a char to the string.
So, for example, on the 1st iteration, the letter h has an ASCII value of 104, which you double to 208, which is outside the ASCII range. On the next iteration, the letter o is ASCII 111 which you double to 222, which is also outside of ASCII. And so on. That is why the final string is not house like you are expecting.
Perhaps you meant to use phrase=phrase+character; instead? But, that won't work either, because you can't concatenate a char value directly to a string object using operator+.
What you can do is use string::operator+= instead:
phrase += character;
Or the string::push_back() method:
phrase.push_back(character);

Why when a character array is compared to another character array, output is wrong but when character array is compared to a string output is correct? [duplicate]

This question already has an answer here:
C++ string and string literal comparison
(1 answer)
Closed 1 year ago.
Question - The translation from the Berland language into the Birland language is not an easy task. Those languages are very similar: a berlandish word differs from a birlandish word with the same meaning a little: it is spelled (and pronounced) reversely. For example, a Berlandish word code corresponds to a Birlandish word edoc. However, it's easy to make a mistake during the «translation». Vasya translated word s from Berlandish into Birlandish as t. Help him: find out if he translated the word correctly.
Input -
The first line contains word s, the second line contains word t. The words consist of lowercase Latin letters. The input data do not consist unnecessary spaces. The words are not empty and their lengths do not exceed 100 symbols.
Output -
If the word t is a word s, written reversely, print YES, otherwise print NO.
When I write this code, the output is wrong -
int main(){
char s[100000],a[100000];
cin >> s >> a;
strrev(s);
if(s==a){
cout << "YES";
}else{cout << "NO";}
}
But when I write this code, the output is correct -
int main(){
char s[100000];
string a;
cin >> s >> a;
strrev(s);
if(s==a){
cout << "YES";
}else{cout << "NO";}
}
Why is it like this, is there a rule that a character array cannot be compared to another character array and if so, how can it be compared to a string?
Remember that arrays naturally decay to pointers to their first elements, and it's such pointers that you are comparing.
In short, what you're really doing is:
if(&s[0] == &a[0])
And those two pointers will never be equal.
To compare the contents of character arrays, you need to use strcmp() or similar function instead, eg:
if(strcmp(s, a) == 0)
Since you're programming in C++, please use std::string for all your strings. There are overloads for the == operator that do the right thing if you have std::string values.

Comparing two strings without using strcmp [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
This is my question:
Write a function name compareStrings(char * str1, char * str1, int i=0), which returns decides whether
the two received string are equal or not. The third parameter decides whether to take case
sensitiveness while comparing strings: 0 means case sensitive, otherwise case sensitive.
The function returns 0 if two strings are equal
Returns 1 if str1 > str2
Returns -1 if str1 < str2.
Example:
compareStrings( “apple”, “Apple” ) returns 1
compareStrings( “apple a day keeps the doctor away”, “apple are good for health” ) returns -1
This code I have done yet but it is not comparing all Ascii's. According to me I must put all Ascii's checks but it would be so long
Please tell me any other logic regarding this Question.
#include<iostream>
using namespace std;
int compareStrings(char * str1, char * str2);
int main()
{
char str1[]="apple";
char str2[]="Apple";
int ret;
ret=compareStrings(str1,str2);
if(ret==0)
cout<<"Both strings are equal"<<endl;
else if(ret==1)
cout<<"string 1 is bigger than 2"<<endl;
else
cout<<"string 1 is lower than 2"<<endl;
return 0;
}
int compareStrings(char * str1, char * str2)
{
for(int i=0;i<20;i++)
{
if(str1[i]==str2[i])
return 0;
else if(str1[i] >= 'A' && str1[i] <= 'Z' &&str2[i] <='a' && str2[i]<='z')
return -1;
else if(str2[i] >= 'A' && str2[i] <= 'Z' &&str1[i] <='a' && str1[i]<='z')
return 1;
}
}
There are multiple problems with the code as shown. I'm ignoring the fact that you aren't using C++ std::string type, though that is another issue.
You only compare the first twenty characters of the strings.
What happens if the strings are longer?
What is the return value from the function if the loop ends?
You compare the first twenty characters of the strings even if the strings are shorter.
You return 0 on the first character that's the same.
You return -1 if the current character in the first string is upper-case and the current character in the second is lower-case, regardless of whether the case-sensitivity flag is set or whether the letters are equivalent.
Similarly you return +1 for the converse condition.
You don't use the isalpha(), isupper(), islower() macros (prefixed with std:: from <cctype> or equivalent functions.
You don't recognize that if one string contains a 7 and the other a 9, you should come to a decision.
Since the comparison function is not supposed to modify either string, the function prototype should use const char * arguments.
Etc.
You will need to rethink your code rather carefully. Ignore case-insensitivity until you have case-sensitive comparisons working correctly. Then you can modify it to handle case-insensitive comparisons too.

How to get a user to input 10 letters, then stores these in a char array [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've tried this, but I having no luck, I'm am looking to get user input 5 letters and then print them out.
string input = "";
const int max = 5;
char string[max] = { };
cout << "Please enter 5 letters: " << endl;
cin.getline(string, max, '\n');
cout << "Your letters :" << string[max];
I think I figured out what's not working:
First, you are printing out string[max] at the end. Since string is a char[] of size max, it actually has no data at index max--its indices are 0 to max-1. You are actually printing out a random character from whatever happens to be in memory immediately after the characters of your string variable.
So instead of << string[max] in the last line, it should be << string.
Second, after making that change, it will still seem to print only 4 characters, instead of the 5 that were entered. This is because strings in the form of char[]s have a null terminator. So since you are telling cin.getline to only fill up 5 characters in string, it fills the first 4 with actual characters from input, and then the last character is '\0'.
So if the input is "hello", then string will contain the following values: { 'h', 'e', 'l', 'l', '\0' }. And then when you print it, there are, of course, really only 4 characters in the array.
And two notes: string input is not used anywhere in your program, so it should be taken out of the question. And also, you really should call your char string[max] variable something else, to reduce confusion.
I hope this helps!

How to remove a character from the string and change data if need it?

I have possible inputs 1M 2M .. 11M and 1Y (M and Y stand for months ) and I want to output "somestring1 somestring2.... and somestring12" note M and Y are removed and the last string is changed to 12
Example: input "11M" "hello" output: hello11
input "1Y" "hello" output: hello1
char * (const char * date, const char * somestr)
{
// just need to output final string no need to change the original string
cout<< finalStr<<endl;
}
The second string is getting output as a whole itself. So no change in its output.
The second string would be output as long as M or Y are encountered. As Stack Overflow discourages providing exact source codes, so I can give you some portion of it. There is a condition to be placed which is up to you to figure out.(The second answer gives that as well)
Code would be somewhat like this.
//Code for first string. Just for output.
for (auto i = 0 ; date[i] != '\0' ; ++i)
{
// A condition comes here.
cout << date[i] ;
}
And note that this is considering you just output the string. Otherwise you can create another string and add up the two or concatenate the existing ones.
is this homework? If not, here's what i'd suggest. (i ask about homework because you may have restrictions, not because we're not here to help)
1) do a find on 'M' in your string (using find), insert a '\0' at that position if one is found (btw i'm assuming you have well formatted input)
2) do a find on 'Y'. if one is found, insert a '\0' at that position. then do an atoi() or stringstream conversion on your string to convert to number. multiply by 12.
3) concatenate your string representation of part 1 or part 2 to your somestr
4) output.
This can probably be done in < 10 lines if i could be bothered.
the a.find('M') part and its checks can be conditional operator, then the conversion/concatenation in two or three lines at most.