I can`t enter values when I run my code - c++

void Manager::ManagerView1()
{
system("cls");
string Ipass;
string Opass;
ifstream Password("Password.txt", ios::in);
if (!Password)
{
cerr << "Check File Integrity";
}
else
{
while (!Password.eof())
{
getline(Password,Ipass);
cout << "Enter Password :\n";
cin >> Opass;
if (Opass == Ipass)
{
cout << "Passwords Match";
}
}
}
}
Text inside the "Password.txt":
Abhik Ray 1123
The password is being read properly, I have already checked that.
When I run the code it lets me enter the password but then the Passwords match doesn't show up as it should. And then it again asks for the password where I am unable to enter it. And then it just quits.
What should I change?

You have several problems, like trying to match only one line from the password file at a time.
The reason for the message is that if (Opass == Ipass) compares the addresses of the character arrays, not their contents.
If you had used std::string to store the strings, the comparison would have worked, but with C style strings you need to use if(strcmp(Opass, Ipass) == 0).
You might also want to check this question for how to terminate the loop:
Why is iostream::eof inside a loop condition considered wrong?
In the new version of the code with cin >> Opass; the >> will only read one word at a time (it stops at each space). So if you type Abhik Ray 1123 you will only get Abhik in Opass, and the rest of the line will remain in the input buffer until the next read.
That's also why it doesn't ask for the next input, it just reads the following words that are already there.
To read a whole line of input, you need to use getline(cin, Opass);, just like when you read from the textfile.

Opass and Ipass are pointers and by doing Opass == Ipass you check if they point to the same area in the memory.
You have to use strcmp to compare their values.

Related

Pause Function it is Looping Forever

I am trying to implement a pause function in C++, but it is looping forever.
I am using macOS but I am trying to create a pause function that will work in any system... I believe my cin >> is not capturing '\n' or '\r' from the keyboard and it is looping forever.
void Transferencia::pause() {
char enter = 0;
while(enter != '\n' && enter != '\r') {
cout << "(Press Enter to Continue...) ";
cin >> enter;
}
cin.clear();
}
I want to pause my program until user press the key "enter".
But even when I press "enter/return" it keeps looping...
At very first: enter != '\n' || enter != '\r' is a tautology: Even if enter does equal one of the characters it cannot be equal to the other one. So one of the tests must be true... You actually want to stay in the loop when enter is unequal to both values.
std::cin >> ... won't read data before you press enter, but it will discard the newlines (actually, all whitespace). So it would suffice just to read one single character right without loop (the loop again would get an endless one); solely: If the user doesn't enter anything at all before pressing 'enter' key, there's no character to read from std::cin and we'd still be waiting.
What you can do is reading entire lines:
std::string s;
std::getline(std::cin, s);
That will accept empty lines as well, so does exactly what you want (note: no loop around!).
Edit (stolen from the comments; thanks, Thomas Matthews): An even more elegant way is
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
as it won't create any additional resources that would be discarded afterwards anyway (the std::string object!).
Edit 2:
Depending on type of last input operation, there might still be a newline (or even further data) buffered, e. g. after int n; std::cin >> n;. In this case, you need to skip the input yet buffered. So you would need ignore twice.
However, if the last input operation consumed the newline already (e. g. std::getline – or if there wasn't any preceding input operation at all), then this would lead to user having to press enter twice. So you need to detect what's has been going on before.
std::cin.rdbuf().in_avail() allows you to detect how many characters are yet buffered. So you can have:
if(std::cin.rdbuf().in_avail())
{
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
std::cout << "press enter" << std::endl;
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
On some systems (including mine), though, in_avail can return 0 even though a newline is yet buffered! std::cin.sync_with_stdio(false); can fix the issue; you should execute it before very first input operation. Hopefully, you don't use C++ (streams) and C (scanf, printf, etc) IO intermixed then...
The easiest way to do this is with getline().
cin >> ignores whitespace, newline characters included. getline() will read an entire line, newline character included. However, it does not copy the newline character to the output string. If the user simply hit the enter key and nothing else, you'd end up with an empty string.
So, to get your desired behavior, you would construct your loop like this:
string line;
while(true)
{
cout << "(Press Enter to Continue...) " << endl;
getline(cin, line);
if(line == "")
break;
}
#Aconcagua has answered your question but this is what I want to add in.
Normally, for handling some specific kind of event in computer, we usually follow event-driven paradigm or event-callback.
The idea is there is an event loop that waits for a new event coming into the system. This case, keyboard is an event, the event loop then calls event-callback. What event-callback does is it compares the value of input with some conditions then do some other tasks (it might change some state of the program or notify users).
The idea is keep CPU busy by either 2 ways.
event-driven : do other tasks while waiting for a new event
multithreading: multiple threads in the system. This approach has the disadvantage is at data-race
Have fun

std::cin working for small number of lines, but not larger ones

I'm writing a part of a program that takes a list of filenames provided by the user and stores them in a vector. So far, testing with small numbers of filenames (I've done up to 11) works fine and the program continues, but trying to import 70 or more filenames makes it so that the program will not continue.
Here is my code:
//******************************* Get List of Filenames ********************************
cin.ignore(1000, '\n');
cout << "Please paste all of the Scans files here: ";
vector<string> filenameList;
string filenameString;
//can change the quit value to anything you want
while (cin >> filenameString && filenameString != "b")
{
filenameList.push_back(filenameString);
}
cout << "The filenames entered are:\n";
for (int i = 0; i < filenameList.size(); i++)
{
cout << filenameList[i] << endl;
}
cout << endl << filenameList.size();
The error is that the program hangs and looks like it is still waiting for input, but does not do anything else, even after entering the exit code "b".
We are copying and pasting the filenames directly into the console and then using cin to put them into a vector.
Here is an example of a list of the filenames that we are using:
MSS279_S10_B112_F8_p001.jpg
MSS279_S10_B112_F8_p002.jpg
MSS279_S10_B112_F8_p003.jpg
MSS279_S10_B112_F8_p004.jpg
MSS279_S10_B112_F8_p005.jpg
MSS279_S10_B112_F8_p006.jpg
MSS279_S10_B112_F8_p007.jpg
MSS279_S10_B112_F8_p008.jpg
MSS279_S10_B112_F8_p009.jpg
MSS279_S10_B112_F8_p010.jpg
MSS279_S10_B112_F8_p011.jpg
MSS279_S10_B112_F8_p012.jpg
MSS279_S10_B112_F8_p013.jpg
MSS279_S10_B112_F8_p014.jpg
MSS279_S10_B112_F8_p015.jpg
MSS279_S10_B112_F8_p016.jpg
MSS279_S10_B112_F8_p017.jpg
MSS279_S10_B112_F8_p018.jpg
MSS279_S10_B112_F8_p019.jpg
MSS279_S10_B112_F8_p020.jpg
MSS279_S10_B112_F8_p021.jpg
MSS279_S10_B112_F8_p022.jpg
MSS279_S10_B112_F8_p023.jpg
MSS279_S10_B112_F8_p024.jpg
MSS279_S10_B112_F8_p025.jpg
MSS279_S10_B112_F8_p026.jpg
MSS279_S10_B112_F8_p027.jpg
MSS279_S10_B112_F8_p028.jpg
MSS279_S10_B112_F8_p029.jpg
MSS279_S10_B112_F8_p030.jpg
MSS279_S10_B112_F8_p031.jpg
MSS279_S10_B112_F8_p032.jpg
MSS279_S10_B112_F8_p033.jpg
MSS279_S10_B112_F8_p034.jpg
MSS279_S10_B112_F8_p035.jpg
MSS279_S10_B112_F8_p036.jpg
MSS279_S10_B112_F8_p037.jpg
MSS279_S10_B112_F8_p038.jpg
MSS279_S10_B112_F8_p039.jpg
MSS279_S10_B112_F8_p040.jpg
MSS279_S10_B112_F8_p041.jpg
MSS279_S10_B112_F8_p042.jpg
MSS279_S10_B112_F8_p043.jpg
MSS279_S10_B112_F8_p044.jpg
MSS279_S10_B112_F8_p045.jpg
MSS279_S10_B112_F8_p046.jpg
MSS279_S10_B112_F8_p047.jpg
MSS279_S10_B112_F8_p048.jpg
MSS279_S10_B112_F8_p049.jpg
MSS279_S10_B112_F8_p050.jpg
MSS279_S10_B112_F8_p051.jpg
MSS279_S10_B112_F8_p052.jpg
MSS279_S10_B112_F8_p053.jpg
MSS279_S10_B112_F8_p054.jpg
MSS279_S10_B112_F8_p055.jpg
MSS279_S10_B112_F8_p056.jpg
MSS279_S10_B112_F8_p057.jpg
MSS279_S10_B112_F8_p058.jpg
MSS279_S10_B112_F8_p059.jpg
MSS279_S10_B112_F8_p060.jpg
MSS279_S10_B112_F8_p061.jpg
MSS279_S10_B112_F8_p062.jpg
MSS279_S10_B112_F8_p063.jpg
MSS279_S10_B112_F8_p064.jpg
MSS279_S10_B112_F8_p065.jpg
MSS279_S10_B112_F8_p066.jpg
MSS279_S10_B112_F8_p067.jpg
MSS279_S10_B112_F8_p068.jpg
MSS279_S10_B112_F8_p069.jpg
MSS279_S10_B112_F8_p070.jpg
MSS279_S10_B112_F8_p071.jpg
MSS279_S10_B112_F8_p072.jpg
MSS279_S10_B112_F8_p073.jpg
MSS279_S10_B112_F8_p074.jpg
MSS279_S10_B112_F8_p075.jpg
MSS279_S10_B112_F8_p076.jpg
Try separating cin >> filenameString && filenameString != "b" into two lines of code. Maybe put the second part inside the loop, in an if statement like this: if (filenameString != "b") break; I have a suspicion that assigning to filenameString and then comparing it in the same line may not always behave as you think it should.
I never figured out why, but I think there is a limit to the number of characters Xcode could import for some reason (somewhere around 1000 characters).
I switched over to Visual Studio on a PC and everything worked out perfectly...

Visual C++ using Console: Char/String compatibility issues with while loop

cout << "Would you like to make another transaction? (y/n)" << endl;
cin >> repeat_transaction;
static_cast<char>(repeat_transaction);
while (repeat_transaction != 'y' && repeat_transaction != 'n')
{
cout << "Invalid selection: Please enter y or n";
cin >> repeat_transaction;
static_cast<char>(repeat_transaction);
}
During the Invalid selection loop, I once accidentally pressed "mn". I noticed the console read out Invalid selection..., So, it did in fact finish and re-enter the while loop. However, after this the console terminated the program. If you enter a single character 'a' or 'y' or 'n' it acts just as it should. Ending or not ending. This was before I attempted to use static_cast to force the truncation of the user input.
Since you managed to get this program to compile I can only assume that repeat_transaction was specified as a char and not a std::string.
When you use cin to get a character it only gets one character but it doesn't flush the buffer. I believe you understand this issue since you wrote This was before I attempted to use static_cast to force the truncation of the user input. . You can attempt to use cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); instead of static_cast<char>(repeat_transaction); after each call to cin >> repeat_transaction; . There are downsides to this. If you enter 'mn' it will work as expected. It reads the m which is not y or n and then flushes the extra characters until it finds end of line \n. If you do nm, n will match and the m will be thrown away. So in that case it will accept nm as valid and exit the loop.
There are other ways that may be easier and give you the effect closer to what you are looking for. Instead of reading a character at a time you can read an entire line into a string using getline (See the C++ documentation for more information). You can then check if the length of the string is not equal to 1 character. If it's not length 1 then it is invalid input. If it is 1 then you want to check for y and n. Although basic (and not overly complex) this code would do a reasonable job:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string repeat_transaction;
cout << "Would you like to make another transaction? (y/n)" << endl;
getline(cin, repeat_transaction);
while (repeat_transaction.length() != 1 || (repeat_transaction != "y" && repeat_transaction != "n"))
{
cout << "Invalid selection: Please enter y or n";
getline(cin, repeat_transaction);
}
return 0;
}
I said reasonable job since one deficiency you might see is that you want to trim white spaces from the beginning and end. If someone enters n or y with a space or tab in front it will be seen as invalid (whitespace at the end would be similar). This may not be an issue for you, but I thought I would mention it.
On a final note, you may have noticed I used using namespace std;. I did so to match what was in the original question. However, this is normally considered bad practice and should be avoided. These StackOverflow answers try to explain the issues. It is better to not do it and prepend all standard library references with std::. For example string would be std::string, cin would be std::cin etc.

Keep looping until user enters a blank line?

So I've run into the following problem. My goal is to create a loop that keeps taking user input over and over until the user doesn't enter anything into 'cin >>', leaves the line blank, and simply presses the ENTER key to move on, at which point the program is supposed to break out of the loop and continue on with the rest of program execution. Something like this:
do {
cout << "\nEnter a name: ";
cin >> input1;
if (input1.empty())
{
break;
}
else
{
user_name = input1;
}
} while (!input1.empty());
As you can see, I've already tried using the empty() function, but that didn't work, the program simply stays in the loop and doesn't break out, no matter how many times I press enter. It just keeps prompting me to enter a name. I've also tried using something like
if (input1 == "")
but that doesnt work either. Can anyone help? How do I break out of this loop?
UPDATE: OK guys, I've tried your recommendations, and it worked! Thank you so much! Unfortunately, although the getline function works, it has also created a new problem for me. Basically, in the first initial loop, the program prompts for a name, I type in a name, and the name is stored in user_name. However, in the SECOND loop, the program doesn't even give me the chance to enter any input, it simply prints "Enter a name: ", and then instantly exits out of the loop, and continues on with the rest of program execution. Why is this happening?
Use this getline(std::cin, input1):
while (getline(std::cin, input1))
{
if (input1.empty())
break;
username =input1;
std::cout << input1 << std::endl << "Enter Input : ";
}
Use std::getline(cin, input1); instead to read a line from the console.
Using cin directly reads exactly one word from stdin. If the user does not input anything, no word has been given and cin does not return yet (your empty check is not even executed).
After you use std::getline you can leave your empty-check as-is:
std::getline(cin, input1);
if(input1.empty())
break;
BTW: In C++ you should also check if the underlying stream has run into an error. So check the return code of cin or getline. This can be done with the following code:
if(!std::getline(cin, input1))
// I/O error
In general, looping until an empty line is entered would be:
while ( std::getline( line ) && !line.empty() ) ...
If you need a prompt: the prompt is part of the input logic, and
should be implemented as such:
std::string
getlineWithPrompt( std::string const& prompt )
{
std::cout << prompt;
std::string results;
return std::getline( std::cin, results )
? results
: std::string();
}
You then do something like:
std::string line = getlineWithPrompt( "prompt for first line" );
while ( !line.empty() ) {
// ...
getlineWithPrompt( "prompt for further line" );
}
(This is actually somewhat simplified, as it treats hard errors
on input, end of file, and empty lines identical, which is
rarely the right thing in professional software. But for
learning purposes, it should be sufficient.)
Cin won't read the whitespace that you call an empty line. Getline may do this, but I am not entirely sure. You could define an end character that the user would type and check for that. Gets would also work, it will just set the starting character to 0x0. Be careful with gets(), it is prone to allow buffer overflows.
This works as well:
char line[128];
do
{
cout << "Enter something: ";
gets(line);
} while (strcmp(&line[0], "\0") != 0);
#JamesKanze
So something like this to exit the while loop?
string str = "foo";
while (str == "foo"){
getline(cin, str);
}
str = "foo";

functionality of cin in c++

I'm a bit confused by the results of the following function:
int main() {
string command;
while(1) {
cin >> command;
if(command == "end")
return 0;
else
cout << "Could you repeat the command?" << endl;
}
return 0;
}
First of all - the output line ("could you...") repeats once for each individual word in the input (stored in command). So far as I can see, it should only be possible for it to happen once for each instance of the loop.
Also, when the line 'if(command == "end")' is changed to 'if(command == "that's all")' it never triggers. A little testing suggested that all of the whitespace was removed from the command.
Could someone explain to me what's going on here?
Thanks
The formatted input operator >>() reads space separated tokens from input. If you want to read whole lines, use the getline() function:
string command;
getline( cin, command );
Most (possibly all) operating systems buffer input. When you type a string of words and then hit [enter] it is only at the time you hit enter that the input is usually passed to your program. Thus that is when it will start reading the input and separating it out into individual words (because as Neil mentions, the >> reads words, not lines). Thus your program goes through the loop multiple times (once per word you had in the line) even though you only hit enter once.
So, you are correct when you think it should only display "could you..." once per loop. That is what is happening.
Likewise, you'll never have a command that contains more than one word because of the space delimiter. As mentioned, use getline() to retrieve the entire text for the line you entered.