Click the alphabet twice? [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 6 years ago.
Improve this question
void password()
{
int i=0, j;
char str[30], ch;
gotoxy (23,10);
cout<< "Welcome to our project! \n";
delay (800);
gotoxy (23,11);
cout<< "Please enter the password = ";
while ((ch=getch())!=13)
{
ch=getch();
str[i]=ch;
i++;
clrscr();
gotoxy (20,20);
cout<< "Enter Password = ";
for (j=0; j<i; j++)
cout<< "*";
}
str[i]='\0';
if (strcmp(str,"script")!=0)
{
cout<< endl;
cout<< "Incorrect Password! \n";
cout<< "Access Denied! \n";
getche();
exit (0);
}
}
i made this a while back, and i came across a problem. So the problem is that, there are no errors whatsoever when I compile the program and run it. The only issue is that when it asks me to enter the password for the script, I have to PRESS every alphabet TWICE and it shows up once (for example, password is TOLS, I have to press it like TTOOLLSS and it shows up as TOLS). How can I fix it?

Remove the line:
ch=getch();
since you already got the char in the line:
while ((ch=getch())!=13)

Related

I don't understand how cin.ignore() works. When I run this piece of code, my program breaks down [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 12 months ago.
Improve this question
There is an array of classes. I want to input an amount of players and then by using a for-loop, input player's names. The problem is that I don't understand how to avoid the program crashing, using cin.ignore().
void main() {
int numberOfPlayers;
cout << "Input amount of players:";
getline(cin, numberOfPlayers);
Player** arrOfPlayers = new Player*[numberOfPlayers];
string newName;
for (int i = 0; i < numberOfPlayers; i++) {
cout << "\nInput player " << i + 1 << " nickname: ";
getline(cin, newName);
arrOfPlayers[i]->setName(newName);
}
}
You have not allocated the individual players
This
Player** arrOfPlayers = new Player*[numberOfPlayers];
only allocates an array of pointers to players
You also need to create those players
for (int i = 0; i < numberOfPlayers; ++i)
arrOfPlayers[i] = new Player;

How to undo system("pause") [closed]

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 7 years ago.
Improve this question
I'm making a program in C++ and did some experimenting. I included the system('pause') function. Now obviously I cannot close the program. How do I undo it and quit the program? Do I really need to restart my PC?
Basically I pranked my self. Here is the src code
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
char answer;
cout << "Can gh0st be VIP?:(y=Yes/n=No)";
cout << endl;
cin >> answer;
if (answer == 'y')
{
cout << "thanx ande =)";
cout << endl;
}
else if (answer == 'n')
{
cout << "time to pay ande ahaha!\a\a\a\a\a\a\a...*with at least 200 more of these*";
cout << endl;
}
else
cout << "just put the letter y or n lol try it again";
cout << endl;
system("pause");
return 0;
}
Press "ENTER". .
Instead of doing system("pause") I would encourage doing a
cin.ignore();
cin.get();
This is a longer form of pause that I have found is very easy to use. And also you just hit the little red square close to the help menu.
pause is not part of C or C++, this is a command from Windows Command Prompt, which is called using the system() syscall.
Once system() is called, your thread hangs at that point until whatever program or command you call with system() is concluded and returns the exit code.
pause is a shell command that will wait for any key to be pressed by the user. Any key pressed will interrupt pause and your program will continue.
Just make sure the key you press is NOT the reset button.

C++ How check characters in real time with _getch() [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 8 years ago.
Improve this question
i need in my loop get character in realtime and check it by conditions. If user press whatever except enter, program works fine. Can anyone help me ? thanks !
while (read != '\n')
{
cout << "Enter character:\n";
read = _getwch();
if (read == '\n') {
cout << "You pressed : ENTER\n";
}
else {
cout << "Your character is: \"" << read << "\"\n\n";
read = '\0';
}
}
include
using namespace std;
int main()
{
cout << "Press the ENTER key";
if (cin.get() == '\n')
{
cout << "Good job.\n";
}
else
{
cout << "I meant ONLY the ENTER key... Oh well.\n";
}
return 0;
}
This code will help in detecting the ENTER key when pressed.
Hope this helps you.

Why do I keep ending up on infinite loop? [closed]

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 8 years ago.
Improve this question
I keep ending up on a infinite loop even on do while loop!
What am I doing wrong? I tried everything but I still cant figure it it out. any help?
here is my code :
#include <iostream>
#include <string>
#include <Windows.h>
using namespace std;
//function prototypes
//prototype for IsAccessible
int IsAccessible(string username, string password);
//prototype for menu
void menu();
int main()
{
string username;
string password;
//make user to login
cout << "Enter username : ";
getline(cin, username);
cout << "\nEnter Password : ";
cin >> password;
IsAccessible(username, password);
cout << "Thank you for logging in!!";
cin.ignore();
cin.get();
return 0;
}
//function definitions
//definition for IsAccesible
int IsAccessible(string username, string password)
{
//check if user entered correct details
do
{
int x = 0;
if(password == "123" && username == "asdqw")
{
cout << "\n\nThank you for loggin in John!";
break;
}
//if user entered wrong details
else if(password != "123" && username != "asdqw")
{
cout << "\nYou have either entered a wrong password or username.\n";
cout << "Please retry.";
}
//if user exceeds limitation
if(x == 5)
{
cout << "\n\nYou have exceeded the 5 retry limitations......\n";
Sleep(4000);
cout << "Exiting program....";
Sleep(5000);
return 0;
}
}while(password != "123" && username != "asdqw");
return 0;
}
The while will keep looping until the username is not "asqdf" and the password is not "123" and the code never asks for a fresh username & password, so it will just keep looping to infinity. Also, you don't incrementx every time the loop iterates, so the 5-maximum-attempts code will never run.
Just a final tip - if your methods don't need to return, you can make the return type void. Instead of returning to exit the do-while, you could use a break statement.

cin.getline Not working with switch statement [closed]

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 8 years ago.
Improve this question
Here is my piece of code from my program, cin.getline is not working with switch statement but without switch statement it works, when I use it with switch statement it just skips the input So in my code I have commented cin.getline and replaced it with cin>>userData;
Please tell me what is the problem When I use it with switch statement.
switch(option)
{
case 1:
cout<<"Enter string "<<endl;
//cin.getline(userData, 100);
cin.getline(userData,100);
It is likely that there is an \n in your input stream, from previous code that you unfortunately don't show in the question, and you need to flush it for cin.getline() to work.
The accepted wisdom is to use ignore:
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.getline(userData,100);
Your code works
#include <iostream>
int main(){
char userData[100];
auto option = 1;
switch(option) {
case 1:
std::cout << "Enter string" << std::endl;
std::cin.getline(userData, 100);
}
std::cout << userData;
return 0;
}
There must be something you're not showing.