How do I correctly use "getline(cin, input);" - c++

I am writing a program that is to convert text that a user gives. I have tried this method by itself in a test program and it works perfectly; however when I try to implement it into the larger program, the user cannot give the program an input to store. The relevant code is as follows:
int main()
{
string str = "NULL";
int mappings = 0;
readMappings(mappings);
receiveInput(str);
displayInput(mappings, str);
return 0;
}
void readMappings(int &count)
{
ifstream readMappings; // Creates the function "readMappings"
string filename; // Generates the filename variable to search for mapping document
cout << "Enter the filename of the mapping document: ";
cin >> filename; // User enters filename
readMappings.open(filename); // "readMappings" function opens the given mappings document
while (!readMappings.is_open())
{
cout << "Unsble to open file. Please enter a valid filename: "; // If the user enters an invaled filename, the program will ask again
cin >> filename;
readMappings.open(filename);
}
if (readMappings.good()) // Mapping document is successfully opened
{
readMappings >> count; // Reads first line
}
readMappings.close(); // If everything fails in this function, the document will close
}
void receiveInput(string &input)
{
char correctness;
do {
cout << "\nPlease enter the text you would like to be converted to NATO:\n";
getline(cin, input);
cout << "You are about to convert: \"" << input << "\".\nIs this correct? (Y/N)" << endl;
cin >> correctness;
} while (correctness == 'N' || correctness =='n');
}
I thought it may have been the program waiting for another input from the user so I added a variable I assumed it would already fill, but it did not fix my solution. In my opinion, the problem is in the receiveInput function, but I could be wrong. Thanks in advance for any assistance.
Also, I am using function prototypes with correct reference variables.

I see two problems:
1) You're not checking for an EOF condition, after invoking std::getline().
2) You are mixing together both std::getline and the >> operator. Now, there's actually nothing technically wrong with that, but both std::getline and the >> operator have very nuanced semantics, when it comes to error checking and input consuming, that you need to get 100% right, in order to correctly use them together.
Just replace your usage of the >> operator with std::getline, so you're using std::getline exclusively, and make sure to check for fail() or eof(). You will find plenty of examples of how to correctly check for end of file and failure conditions, with std::getline, here on stackoverflow.com.

Related

I am doing file input/output, and I am attempting to locate a string of characters within a json file and replace them with another string

I am currently writing a program that automates a game I play. I am doing this as a side project and I am currently stuck on a file input/output issue. All the text information stored inside the json file gets fully deleted once I finish running the program. I am trying to find certain phrase in a json file and then replace it with a different one. I made multiple functions to help assist me in doing this. I am utilizing one function to find the line specified within the file, and then another line to replace the string I located with a string I made, based on splicing using a mixture of hard-coded text and a user inputted string. I am aware that I am using cin >> rather than getline and using strings in c++ but I promise you that is not the issue at hand here. The problem is that my entire file's content is being deleted and I am not quite sure why. I will link my code below with a picture of the output and hopefully someone can help me out here because I am genuinely confused, especially since this seems like such a rudimentary task. I took out segments of the code because the entire project is pretty extensive, if there is anything missing down below or anything is unclear please let me know in the comments.
string FindFullLine(std::ifstream& file, unsigned int num){
std::string s;
for (int i = 1; i <= num; i++)
std::getline(file, s);
return s;
}
bool replace(std::string& str, const std::string& from, const std::string& to) {
size_t start_pos = str.find(from);
if(start_pos == std::string::npos)
return false;
str.replace(start_pos, from.length(), to);
return true;
}
^^ separate functions that are being used in this small piece of code below
string champo = " \"mainChamp\": ";
string apo = "\",";
string champ, ban, gamemode, newchamp;
cout << "What champion would you like to pick? " << endl;
cin >> champ; //getline doesnt work; cin is not causing the issue
cout << "\nWhat champion would you like to ban? " << endl;
cin >> ban;
cout << "\nWhat gamemode would you like to pick? " << endl;
cin >> gamemode;
newchamp = " \"mainChamp\": \"";
newchamp += champ;
newchamp.append(apo);
ifstream yuumi ("C:\\forfuncoding\\bot\\Yuumi Bot Extension Fix\\bot\\config.json"); //opening file stream for input
ofstream yuumi1 ("C:\\forfuncoding\\bot\\Yuumi Bot Extension Fix\\bot\\config.json"); //opening file stream for output
if(!yuumi || !yuumi1){
cout << "Error opening up files! " << endl;
exit(0);
}
string s = FindFullLine(yuumi, 2);
string temps;
string strTemp;
temps = s; //temporary val to store s
cout << s;
replace(s, s , newchamp); //first parameter is where new string is stored, second param is the old string and third param is what old string is being replaced by new string
cout << s;
while(yuumi >> strTemp)
{
if(strTemp == temps){
cout << s;
strTemp = s;
}
yuumi1 << strTemp;
}
yuumi.close(); //closes file for output
yuumi1.close(); //closes file for input
exit(0); //just for testing purposes
I have tried changing many values by passing them by reference/not by reference, I have tried using fstream with ios::out and ios::in, I have tried using getline, I have tried moving the logic around; making sure all the variables are scoped correctly. At first glance I thought the issue was in the replace function since it returns a bool but after further inspection it also works as is. Originally, the FindFullLine function was made for fstream, but I changed it to ifstream in the parameters, I don't believe that would change much however. I also tried getting rid of the ifstream and ofstream.close(); functions. I have tried practically everything I can think of.Down below is a screenshot of the format I was attempting to copy.
format of text in json file i was copying
I believe I formatted everything correctly in my code in order to match exactly what is happening in the text file. But when I run the code, everything disappears and the text file is blank. Linked down below is the output that I get, there is a cout << statement after the replace function is ran to ensure that everything is working as intended, and from the output's point of view, everything is working correctly.
output from console

there's an extra newline before text ofstream c++

I'm trying to copy user input text into a file and its doing that its just also adding a newline before the text starts any ideas how to make it not do that?
getline(cin, userInput);
while (userInput != endWrite) {
storyTime << userInput << endl;
getline(cin, userInput);
}
storyTime.close();
return 0;
}
Your code is incomplete, so it's impossible to be absolutely certain what may be going on--at first glance, my immediate guess would be that what you're seeing may result from some code you didn't quote. For one obvious possibility you might be asking the user for the name of the file where you're going to write the output:
std::string outputName;
std::cout << "Enter output file name: ";
std::cin >> outputName;
std::ofstream storyTime(outputName);
//...
In this case, the std::cin >> outputName; reads the filename--but you had to press the enter key to get it to read that, and that press of the enter key will leave a new-line in the input, so when you start the loop afterwards, it'll be read as a newline preceding the text the user enters afterwards.
Aside
Other than that, I'd normally try to keep the code somewhat simpler:
while (std::getline(std::cin, userInput)) {
storytime << userInput << '\n';
}
As a really general rule of thumb, I'd advise that a formatted read from a text file (using either std::getline or some operator>>) be as the condition of an if, while, or whatever. Doing so habitually makes it much easier to write input loops that process files correctly.
Demo
Just for what it's worth, here's some working code that doesn't insert an extra new-line:
#include <iostream>
#include <string>
#include <fstream>
int main() {
std::string userInput;
std::string filename;
std::cout << "Please enter file name: ";
std::getline(std::cin, filename);
std::ofstream output{filename};
while (std::getline(std::cin, userInput)) {
output << userInput << '\n';
}
}

Odd behavior in C++ program

I am writing a program that I allow the user to specify an input file to open and when I test with incorrect file names, the program is behaving very weird and it seems to have something to do with the input buffer, but I don't know where to begin other than using getline() instead of cin >> but I have already tried that.
here is the code that I think may be the problem:
bool openfile(ifstream&);
string userInput();
int main()
{
// ...
while (!openfile(inputFile))
openfile(inputFile);
string input = userInput();
// ...
}
bool openfile(ifstream &inputFile)
{
string filename;
cout << "Please enter the name of the file or type quit to exit the program: ";
cin >> filename;
cout << endl;
if (filename == "quit")
exit(4);
else
inputFile.open(filename);
if (!inputFile)
{
cout << "The file \"" << filename << "\" could not be opened or does not exist.\n";
return false;
}
return true;
}
string userInput()
{
string englishSentence;
cout << "Please enter a sentence or type quit to exit the program: \n";
getline(cin, englishSentence);
if (englishSentence == "quit")
exit(4);
return englishSentence;
}
Those are the two functions that read any input. openfile() is called first as you can see. Any help is greatly appreciated. Let me know if you suspect something else in my code and I will paste it.
while (!openfile(inputFile))
openfile(inputFile);
What this does is attempt to open the file twice each iteration, as long as the first attempt fails. Also, you need to make sure that inputFile is closed before attempting to open it again since it appears that you're reusing the same file object repeatedly.
Certainly first try something like:
while (!openfile(inputFile))
;
You can just do:
while (!openfile(inputFile));
Since the way you have it it would request an input filename twice if it fails the first time.
Basically to outline the problem:
Begin Loop
Request Filename
Invalid Filename
Request Replacement Filename
Return to start of the loop (Checks again)
Some problems I see from your code:
int main(); { ... } does not define the main function. You need to drop the semicolon, or it won't even compile.
while (!openfile(inputFile)) openfile(inputFile); repeats openfile(inputFile) unnecessarily. If the first one (in the condition) fails and the second one (in the body) succeeds, a third call will be made (in the condition) to check if the loop should continue. What you probably want is just while (!openfile(inputFile)) { }.
You open a file in openfile and never use it in the subsequent userInput.

Program crashes when trying to access file, fstream --C++

I am making a user database. When I try to open the "dataBase.txt" which holds all of the users and passwords, the console pops up (which should happen since it is a console application) but it says that the program has already complete. When I close it, my computer tell me that the program had crashed. The function is saved in a class.
After some debugging the code seems to crash at ifstream fin("dataBase.txt");
No error is returned from the compiler.
The code for the function called is:
void User_Psw::UserCheck()
{
// read from the database
ifstream fin("dataBase.txt");
while (!fin.eof())
{
fin >> Usernames[sizeOfDatabase] >> Password[sizeOfDatabase];
sizeOfDatabase++; //The Number of lines in .txt
}
// rest of the program
cout << "Username: ";
cin >> username;
getNameIndex();
cout << "Password: ";
cin >> password;
if(!PasswordMatches())
{
cout << "Access denied";
}
}
I can add more code snippets if asked.
Don't use fin.eof() to control a loop. That function is only useful after a read has failed.
A likely reason for the crash, however, is that you are assigning to Usernames[sizeOfDatabase], which may be beyond Usernames.capacity(). The canonical way to append and item to a std::vector is to call push_back().
Since your containers are std::vector<std::string>, this is a better approach...
std::string username, password;
while (fin >> username >> password)
{
Usernames.push_back(username);
Passwords.push_back(password);
++sizeOfDatabase;
}
Of course, if you want to know the number of usernames or passwords once the file has been read, you can call Usernames.size() (which should be the same as Passwords.size()); this may obviate the need to keep sizeOfDatabase.
Personally, I would use a single container for usernames and (salted, hashed) passwords, rather than two separate containers; either std::map<std::string, std::string> or perhaps std::unordered_map<std::string, std::string>, making the password lookup nice and quick per username.
I think you should start by adding this to the constructor of your ifstream as you haven't specified you want the file opened for input:
ifstream fin( "dataBase.txt", ifstream::in );
if( !fin.good() )
{
cout << "Failed to open database file." << endl;
return;
}
See http://www.cplusplus.com/reference/fstream/ifstream/ifstream/ for additional literature.

cin.getline not executing c++

For some weird reason my input line cin.getline(oneLine, 80); is completely ignored when I put it in this else if block. I can't understand why because when I move it somewhere else in the program, it works.
else if (choice == "user-id")
{
cout << endl << "Enter a full name e.g. John Smith ";
char oneLine[80];
cin.getline(oneLine, 80);
cout << oneLine;
}
Here's the rest of my code. I'm new to C++ so I'm sure a lot of my conventions may be questionable at best.
int main( )
{
while (true)
{
int pause;
string choice = "proceed";
string nameGiven;
string userIdGiven;
string result;
using namespace std ;
while ((choice != "name") && (choice != "user-id"))
{
cout << "Would you like to search for a name or user-id? ";
cin >> choice;
if ((choice != "name") && (choice != "user-id"))
cout <<"Please enter a valid choice (name or user-id)" << endl;
}
if (choice == "name")
{
string dataType = "int";
while (true)
{
cout << endl << "Enter a valid user id (4 digit maximum) ";
cin >> userIdGiven;
if (valid(userIdGiven))
break;
else
cout << endl << "Not a valid number. " << endl;
continue;
}
result = findData(userIdGiven, dataType);
cout << "name: " << result;
}
else if (choice == "user-id")
{
cout << endl << "Enter a full name e.g. John Smith ";
char oneLine[80];
std::getline(oneLine, 80);
cout << oneLine;
}
string ans;
cout << endl << "Would you like to play again? (yes/no) " << endl;
cin >> ans;
if ( (ans == "yes") || (ans == "Yes") || (ans == "Y") || (ans == "y") )
continue;
else
break;
cin >> pause;
}
return 0;
}
Your std::cin object is in a bad state (std::cin.good() == false) from a previous input operation. For example, you might have tried to read a number, but there were only nun-numeric characters in the input buffer.
Always check for input success before continuing using a std::istream.
Note: Don't use the old input functions operating with char*, as they are more complicated and less safe to use than the new ones operating on std::string. In your case, use std::getline(std::istream&, std::string&, char = '\n').
twsaef's comment's substantively correct... you're streaming a string into choice, which consumes the characters up until but excluding the next whitespace character - you're probably typing a newline to terminate your input, so it's left in the buffer. Then you use getline which sees that newline and reads an empty string.
Easiest solution is to call getline() to read the initial string too, then check if choice is "name\n" or "user-id\n". Better - write a "trim" function to remove the whitespace from the line before comparison (boost string library has this already). Otherwise, you could use read and ignore characters from std::cin until you get a '\n'. Or even read a line then put in into a stringstream and read a string from there.... Lots of choices.
And, please check your stream state! Try to use:
if (std::cin >> x)
// x was parsed from stream... use it
else
// print an error so you know where things failed!
FWIW, I guessed what the problem would be (it's stupidly common) before seeing the update, and chuckled to myself at the other guesses (although they make very good points even if they missed the OP's problem).
The line of code is working correctly and as advertised. It doesn't happen to be working the way you want it to.
When you read from std::cin, that does not pause the program and wait for input. What causes the pause is the lack of sufficient input data for the read operation.
Input is fed to your program a line at a time. Remember, the console window is a program, too. It is responsible for translating the user's key-presses into text characters (bytes, really), handling things like the backspace key, and gathering it all up into lines.
So say you read an int with operator>>, and then read a line with getline. The program will not see an int until the user hits the Return key, because that triggers the console to feed a line of input to your program.
operator>> will skip leading whitespace, read the integer, and leave trailing whitespace alone. Newline characters are whitespace. There is a newline character in the input (at the end of the line, obviously).
getline() will not skip any leading whitespace, and read until the next newline. The very next character happens to be a newline, so getline() happily reads an empty line and the program proceeds with that.
So how do you fix that? Chances are, if you're reading all your input from cin, that you want the program to pause every time you come to a reading operation. The way to do that is to ensure that there is never any available data at that point, and the way to do that is to read everything that's available - i.e., the whole line - every time that you read something.
So, always read a full line from cin. As noted by wilx, please use the free function std::getline for this. Do not use the .getline member function of the stream. Use std::string to represent strings of text. That's what it's there for. It will make your life much, much easier. That also means that if you're expecting an integer and the user types "76 trombones", you get rid of the "trombones" data (and can decide whether you want to just throw it away, or yell at the user and make him re-enter a number without any funny commentary).
But then what? You just have a string, where you may have wanted an int. Fortunately, there is a simple solution for that. We can treat the string as a source of stream data, using the standard library class std::stringstream. We just construct a stringstream from the string, and then use it just like std::cin - i.e. we can read from it with operator>>, check the stream state to see if reading was successful, etc.
As noted by sbi, always check whether reading succeeded! If you try to read an int and the stream contains text like "hi mom", then (a) the int variable will not be altered (so if it was uninitialized it is still uninitialized, a very dangerous state to be in), and (b) the stream will go into a "failed" state and will not read any more until you clear it, and (c) even if you clear it, the data will still be there, which can trigger an infinite loop if you're not careful.
Fortunately, with the separate stringstream, we avoid all kinds of complications. If reading fails, then all those things happen to the stringstream object - not to std::cin. The getline operation will always succeed on std::cin unless perhaps the user explicitly indicates an end-of-file (control-D character on Linux, or control-Z on Windows). We can easily check if the stringstream is in the failed state, loop and just create another one - the old one will automatically get cleaned up.
We can even make a helper function like:
template <typename T>
// Attempt to read into to_read, and return whether successful.
bool read_primitive_from_input(std::istream& input, T& to_read) {
std::string line;
std::getline(std::cin, line);
std::istringstream iss(line);
return iss >> to_read;
}
std::stringstream is provided by the standard library header <sstream>. std::string comes from <string>, of course.