I'm making a software as an ATM, so when the user try to enter the password the user only sees *******, but when trying to delete it doesn't delete a character. It just adds a new one. Here's my code:
string password2 = "";
cout << "PASSWORD: ";
ch = _getch();
while(ch != 13) //character 13 is enter
{
password2.push_back(ch);
cout << '*';
ch = _getch();
}
And also, I try to use pop_back(); but it doesn't work either. Can anybody help me please?
Just did a little magic and created this:
I must admit, I don't like the style... But it "works" !
#include<iostream>
#include<string>
#include <conio.h>
bool verify_pass(const std::string& pass)
{
std::string input = "";
char ch = '0';
while(true)
{
ch = getch();
if(ch == '\b')
{
if(input.size() > 0)
{
input.erase(input.begin() + input.size() - 1); // erase last char
std::cout << "\b \b";
}
}
else if(ch != '\r\n' && ch != '\n' && ch != '\r')
{
input += ch;
std::cout << '*';
}
else
break;
}
return input == pass;
}
int main()
{
std::string insecurePass = "1234";
std::cout << "Enter Password: ";
if(verify_pass(insecurePass))
std::cout << "\nCorrect!\n";
else
std::cout << "\nFalse!\n";
}
By the way, you can use the vector like I used the string, but use push_back instead of += and pop_back() should work too instead of my method with erase.
Related
So, I have to encrypt my console application with a password, i did something that's working but there is a problem, backsapce doesn't erase the character entered, it is also counted as a character, how can I make it to do its job, to erase the character?
This is the code:
void main()
{
char password[20], my_password[20] = "password";
int i;
char ch;
system("cls");
cout << "PASSWORD: ";
i = 0;
do
{
ch = _getch();
password[i] = ch;
if (ch != 27 && ch != 13 && ch != 9)
cout<<"*";
else
break;
i++;
} while (i < 19);
password[i] = '\0';
if (strcmp(password, my_password) != 0)
{
cout << "\n\nIncorrect password !!!";
cin.get();
return;
}
cout << "\n\nPassword is correct !";
cout <<"\n\nThe program is executed !";
cin.get();
}
void main()
{
char password[20], my_password[20] = "password";
int i;
char ch;
system("cls");
cout << "PASSWORD: ";
i = 0;
do
{
ch = _getch();
if (ch == 8)
{
i--;
cout << "\b \b";
continue;
}
password[i] = ch;
if (ch != 27 && ch != 13 && ch != 9)
cout << "*";
else
break;
i++;
} while (i < 19);
password[i] = '\0';
if (strcmp(password, my_password) != 0)
{
cout << "\n\nIncorrect password !!!";
cin.get();
return;
}
cout << "\n\nPassword is correct !";
cout << "\n\nThe program is executed !";
cin.get();
}
Not the cleanest code but it works. Decrement the counter to over write the previous character and output two backspace characters separated by a space.
"how can I make it to do its job, to erase the character?"
Use a curses library. Like ncurses.
You could check if the character received is a backspace, if it is decrement i which will effectively remove the last character.
i = 0;
do
{
ch = _getch(); // get the character
if(ch == DEL || ch == BS) // check for backspace
{
i--;
cout << BS;
}
else if(ch >= ' ' && ch <= '~') // check if its valid ASCII
{
password[i] = ch;
cout << "*";
i++;
}
else if (ch == 27 || ch == 13 || ch == 9) // check if entry is complete
{
break;
}
} while (i < 19);
password[i] = '\0';
somewhere else
#define BS '\b'
#define DEL 127
The first part of my code is used to receive a single string input from the user and replace certain characters in that word using string class member functions. This part was easy for me to figure out, but I can't seem to figure out how to form a new string out of these changed characters since I will need to use this string for further manipulation later on in my code.
This is problematic since the for loop outputs single char variables that can't be manipulated as a single string.
#include <iostream>
#include <string>
using namespace std;
int main(){
string word;
char letter;
int i;
cout << "Enter a word: ";
cin >> word;
cout << "\n";
for ( i = 0; i < word.size(); i++)
{
if ( word.at(i) == 'e')
{
word.at(i) = '3';
}
if ( word.at(i) == 'i' )
{
word.at(i) = '1';
}
if ( word.at(i) == 'x' )
{
word.at(i) = '*';
}
cout << word.at(i);
}
cout << "\n";
}
As my code currently stands, a user might, for example, input the string "cheese" and receive the output ch33s3. However this output is not a string; it is an assortment of chars without a space to separate them. I can't continue my code any further with my for loop output remaining as it currently is.
Edit: I realize now that I already have what I need, but confused myself into thinking the scope wouldn't apply outside my for loop. Thanks for the quick and easy answers.
You were pretty much done already:
#include <iostream>
#include <string>
using namespace std;
int main(){
string word;
char letter;
int i;
cout << "Enter a word: ";
cin >> word;
cout << "\n";
for ( i = 0; i < word.size(); i++)
{
if ( word.at(i) == 'e')
{
word.at(i) = '3';
}
if ( word.at(i) == 'i' )
{
word.at(i) = '1';
}
if ( word.at(i) == 'x' )
{
word.at(i) = '*';
}
}
cout << word << "\n";
}
As it turns out, your work is already done for you. Your variable "word" would hold the value "ch33s3" after the loop ends.
The variable word contains the altered string and can be manipulated as such. It appears that you already have what you need.
Also - you may already know this - but you don't need the "at" function to accomplish what you're doing here. You can index the string like an array. For example:
word[i] = 'e';
cout << word[i];
However this output is not a string
Did you mean to use std::ostringstream instead of std::cout to receive the results?
The word.at(i) = xxx; statements already manipulated the word string, and you have it.
Here's a sample to show how to get a std:string result, without manipulating word directly:
#include <iostream>
#include <string>
#include <sstream>
int main() {
std::string word;
char letter;
int i;
std::cout << "Enter a word: ";
std::cin >> word;
std::ostringstream leetWord;
for ( i = 0; i < word.size(); i++) {
if ( word[i] == 'e') {
leetWord << '3';
}
else if ( word[i] == 'i' ) {
leetWord << '1';
}
else if ( word[i] == 'x' ) {
leetWord << '*';
}
else {
leetWord << word[i];
}
}
// Here you can refer to the ostringstream::str() function to
// get a string
std::string theWantedString = leetWord.str();
std::cout << word << " => " << theWantedString << std::endl;
}
See the working sample.
I'm a C++ beginner and wrote a program to check if two phrases are anagrams. The characters are being read one at a time and stored in an array. I have everything working, except in some cases, extra characters are being inserted into the array.
For example, if I enter the phrases aabb and abba, this is the output of the program:
Enter two lines that might be anagrams:
--> aabb
--> abba
String A is aabb
String B is abbai?
The two strings are NOT anagrams.
They should be anagrams, but for some reason, i? is added into the array, causing the phrases to not be anagrams. I'm probably overlooking a simple mistake in the code, but would really appreciate any feedback.
Here is the code:
#include <string>
#include <algorithm>
#include <iostream>
using namespace std;
int check_anagram(char [], char []);
int main()
{
char ch, a[60], b[60];
int flag, i;
cout << "Enter two lines that might be anagrams:" << endl;
cout << "--> ";
cin.get(ch);
ch = tolower(ch);
i = 0;
while (ch != '\n')
{
if (ch > '#') {
a[i] = ch;
i++;
}
cin.get(ch);
ch = tolower(ch);
}
cout << "--> ";
cin.get(ch);
ch = tolower(ch);
i = 0;
while (ch != '\n')
{
if (ch > '#') {
b[i] = ch;
i++;
}
cin.get(ch);
ch = tolower(ch);
}
flag = check_anagram(a, b);
cout << "String A is " << a << endl;
cout << "String B is " << b << endl;
cout << "The two strings ";
if (flag == 1)
cout << "ARE";
else
cout << "are NOT";
cout << " anagrams." << endl << endl;
return 0;
}
int check_anagram(char a[], char b[])
{
int first[26] = {0}, second[26] = {0}, c = 0;
while (a[c] != '\0')
{
first[a[c]-'a']++;
c++;
}
c = 0;
while (b[c] != '\0')
{
second[b[c]-'a']++;
c++;
}
for (c = 0; c < 26; c++)
{
if (first[c] != second[c])
return 0;
}
return 1;
}
Thanks in advance!
You just need to terminate the two character arrays with '\0' because the logic in check_anagram treats both arrays as NULL-terminated.
..
while (ch != '\n')
{
if (ch > '#') {
a[i] = ch;
i++;
}
cin.get(ch);
ch = tolower(ch);
}
a[i] = '\0'; // <<<<<<<< Add this line
cout << "--> ";
cin.get(ch);
ch = tolower(ch);
i = 0;
while (ch != '\n')
{
if (ch > '#') {
b[i] = ch;
i++;
}
cin.get(ch);
ch = tolower(ch);
}
b[i] = '\0'; // <<<<<<<< Add this line
..
Here is the result:
Enter two lines that might be anagrams:
--> aabb
--> abba
String A is aabb
String B is abba
The two strings ARE anagrams.
Hey i want to stop the user from entering integers when i ask the user to input a name. I have achieved this for an integer and a char. Can anyone help me adapt my code for a string.
int getNum()
{
int num;
std::cout << "\nWhat is your age? ";
while (!(std::cin >> num))
{
// reset the status of the stream
std::cin.clear();
// ignore remaining characters in the stream
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
std::cout << "Enter an *integer*: ";
}
std::cout << "You entered: " << num << std::endl;
return num;
}
char getChar(string q)
{
char input;
do
{
cout << q.c_str() << endl;
cin >> input;
}
while(!isalpha(input));
return input;
}
string q = "This is a test123";
for(string::iterator i = q.begin(); i != q.end(); i++)
{
if((*i < 'A' || *i > 'z') && (*i != ' '))
{
return false;
}
}
would also be an option, if you allow for spaces and other characters.
Edit: updated for checking a single char:
char c;
bool finished = false;
printf("Please enter your sex, M/F?\n");
while(!finished)
{
cin >> c;
if(!(c == 'm' || c == 'M' || c== 'f' || c=='F'))
{
printf("Please try again...\n");
}
else
{
finished = true;
}
}
Note that c is only input, char by char, when Enter is pressed, before that the line feed does not happen.
If you plan on using std:string, then you can use this to find if the entered string has any digits or not:
if (std::string::npos != s.find_first_of("0123456789"))
{
std::cout << "digit(s)found!" << std::endl;
}
I keep running into an issue with this code in C++:
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string words[25];
int i = 0;
char * word;
cout << "Input a phrase, no capital letters please.";
char phrase[100] = "this is a phrase";
word = strtok (phrase, " ,.");
while (word != NULL)
{
i++;
words[i] = word;
cout << words[i] << " ";
word = strtok (NULL, " ,.-");
int g = 0;
}
cout << endl << endl;
int g = 0;
while (g < i)
{
g++;
char f = words[g].at(0);
if ((f == 'a') || (f == 'e') || (f == 'i') || (f == 'o') || (f == 'u') || (f == 'y'))
{
words[g].append("way");
cout << words[g] << " ";
}
else
{
words[g].erase (0,1);
cout << words[g] << f << "ay" << " ";
}
}
cout << endl;
system("PAUSE");
}
I actually want my program user to generate the phrase to be put in char phrase[100] but I can't figure out the proper syntax to initiate input on it without screwing up the translation.
This is a program that translates phrases into pig latin BTW.
What you want is:
char phrase[100];
fgets(phrase, 100, stdin);
Though, as stated in the comments and other answer, you're using C string functions in C++ and this is very odd. You should not do so unless you are required by an assignment or something.
Instead use:
string input;
getline(cin, input);
To tokenize you can do the following:
string token;
size_t spacePos;
...
while(input.size() != 0)
{
spacePos = input.find(" ");
if(spacePos != npos)
{
token = input.substr(0, spacePos );
input = input.substr(spacePos + 1);
}
else
{
token = input;
input = "";
}
// do token stuff
}
Or, to skip all of that jazz:
string token;
while(cin >> token)
{
// Do stuff to token
// User exits by pressing ctrl + d, or you add something to break (like if(token == "quit") or if(token == "."))
}
The preferred way of doing terminal I/O in C++ are streams. Use std::cin and the std::getline function to read strings from input output.
std::string input;
std::getline(std::cin, input);
After that you probably want to get rid of strtok and look at this question to understand how to do string tokenization in C++.