Getline doesn't work when push Enter - getline

I have a very strange problem. Here is my code:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string user_name;
cout << "what is your name?" << endl;
getline(cin, user_name, '\n');
cout << "hello, " << user_name << ", how are you today?" << endl;
}
This code doesn't end when I push Enter at all; therefore there is no way to complete the input. The output will stop like this and wait:
what is your name?
However if I change '\n' to 'p' or whatever char, it will finish the input when the specific char is input. For instance:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
string user_name;
cout << "what is your name?" << endl;
getline(cin, user_name, '\p');
cout << "hello, " << user_name << ", how are you today?" << endl;
}
Screen:
what is your name?
hello, Frank, how are you today?
RUN SUCCESSFUL (total time: 2s)
PS:
I am using NetBeans IDE 8.0 and Windows 8 Pro x64

New Line character depends on the operating system.
Reminder - \r\n or \n\r?
I would leave out the third parameter of the getline call all together. This will use getline's default behavior and terminate the read on the enter key.
EDIT: After reading you are still having problems, I decided to run some tests on your code.
First, to confirm that the default behavior works without specifying the third parameter to the getline method.
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
while(true){
string user_name;
cout << "what is your name?" << endl;
getline(cin, user_name);
cout << "hello, " << user_name << ", how are you today?" << endl;
}
}
The output:
what is your name?
WhiteboardDev
hello, WhiteboardDev, how are you today?
what is your name?
WhiteboardDev
hello, WhiteboardDev, how are you today?
what is your name?
WhiteboardDev
hello, WhiteboardDev, how are you today?
what is your name?
Second, to test manually specifying the escape sequence '\n' i did this:
#include <iostream>
#include <string>
using namespace std;
int main(void)
{
while(true){
string user_name;
cout << "what is your name?" << endl;
getline(cin, user_name, '\n');
cout << "hello, " << user_name << ", how are you today?" << endl;
}
}
And the output:
what is your name?
WhiteboardDev
hello, WhiteboardDev, how are you today?
what is your name?
WhiteBoardDev
hello, WhiteBoardDev, how are you today?
what is your name?
WhiteboardDev
hello, WhiteboardDev, how are you today?
what is your name?
compiled on g++ w/osx so i didnt bother testing the '\r\n' case. This brings up a good point about cross platform compatibility. If you want your code to work in multiple environments try to not specify anything platform specific in your code (aka the EOL sequence) and leave out that third parameter entirely.

Now I have solved my question with the help of WhiteboardDev. In my coding environment, I should use '\r' and during the input I need push Enter and Space, two keys in order to complete the it.

Related

Operating backwords?

In this code you will see it asks for a password. Please note that there is no real password system setup. I am new to c++ and I literally just learned about strings. I understand them but I am not getting good results. It is operating backwords? I do not understand string completely so I do not understand what is happening. I tried googling it but I couldn't find anything.
When I flip username with password it works just fine. So why is it running backwords?
#include <iostream>
#include <string>
std::string password()
{
std::cout << "Please enter your password. : ";
std::string password;
std::getline(std::cin, password);
return password;
}
std::string username()
{
std::cout << "Please enter your username. : ";
std::string username;
std::getline(std::cin, username);
return username;
}
int main()
{
std::cout << "Welcome to the test login! Please enter your username and password to enter. \n \n \n";
std::cout << username() << password() << "\n \n \n";
int noEnd{ 0 };
std::cin >> noEnd;
return 0;
}
Notice if I swap password with username it works.
std::cout << "Welcome to the test login! Please enter your username and password to enter. \n \n \n";
std::cout << password() << username() << "\n \n \n";
But this does not make sense to me. I am knew to c++ and just learned the basics of strings. Theoretically if I have the code like,
std::cout << "Welcome to the test login! Please enter your username and password to enter. \n \n \n";
std::cout << username() << password() << "\n \n \n";
username() should go first but, password() does instead?
Sorry I am knew please don't use like weird functions and things because I will probably not understand them . . . Yet.
Your compiler is evaluating std::cout << password() << username() << "\n \n \n" from right to left.
According to ccpreference
Order of evaluation of the operands of almost all C++ operators
(including the order of evaluation of function arguments in a
function-call expression and the order of evaluation of the
subexpressions within any expression) is unspecified. The compiler can
evaluate operands in any order, and may choose another order when the
same expression is evaluated again.
The best option is to write code in a way that does not depend on the order it is evaluated.
std::string u = username();
std::string p = password();
std::cout << u << p << "\n \n \n";

How to extract numbers correctly from an input file in command line argument

I am attempting use ifstream to extract two numbers from a file in argv[1], named "inputFile", and the extracting operator seems to be extracting the bits of code rather than the numbers needed.
inputFile.txt was put into the command line operator by right clicking the project, going to properties -> debugging -> command arguments -> typing inputFile.txt into command arguments in visual studio 2017.
The file inputFile.txt is as below:
1 2
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main(int argc, char *argv[])
{
//Test opening file
cout << "Input file: " << argv[1] << endl;
ifstream in(argv[1]);
if (!in)
{
cerr << "Unable to open " << argv[1] << " for input";
return 1;
}
//extract numbers
int num1;
int num2;
in >> num1 >> num2;
cout << num1 << endl << num2 << endl;
in.close();
return 0;
}
I expect the int num1 to hold 1, and the int num2 to hold 2, but instead each variable holds the number -858993460.
Are you sure the file you are reading has got the data you expect? This code works fine for me, compiling with Visual Studio 2005. But, If I change the contents of the file, writing not numbers (for example if I write: a b), num1 and num2 ends with -858993460.

How to get sub-strings from a string using two delimiters (; and .) in C++

I have a user input of this kind:
A+B*C;
A*(B+c);
C+F.
Now I want to parse this input and get all sub-strings until the semi-colons and stop scanning when I run into a period symbol.
What is the simplest way to achieve this ?
Also, I have freedom to take input from a file or from the console. Which one would be the easiest to implement ?
I have a method to read from the console as such :
cout << "Enter input: " << endl;
char delimiter = '.';
string inputStr;
getline(cin,inputStr,delimiter);
cout << inputStr;
But if I enter the above mentioned sample input I read only until before period symbol is recieved. So while finding sub strings, what stopping criteria or flag should I take ?
TIA
EDIT:1
Code so far:
#include <string>
#include <iostream>
using namespace std;
int main()
{
cout << "Enter input: " << endl;
char delimiter = '.';
string inputStr;
getline(cin,inputStr,delimiter);
cout << inputStr;
string deli = ';';
size_t pos = 0;
string token;
while ((pos = inputStr.find(deli)) != std::string::npos) {
token = inputStr.substr(0, pos);
std::cout << token << std::endl;
inputStr.erase(0, pos + deli.length());
}
std::cout << inputStr << std::endl;
};
ANSWER:
I was wrongly initializing the string deli. I had to do this :
string deli = ";". Instead of single-quotes, I had to use double-quotes, because it a string and not a character! Final working solution here: https://repl.it/EPyC/2
Use getline with delim ; for all lines. Check for dot(.) for last line.
string line;
while(getline(cin, line, ';')){
if(line.back() == '.')
line.pop_back();
cout << line <<endl;
}

Display chinese characters in console application

I have the following console app in Visual Studio
#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
wcout << "displayStringsTest_data" << endl;
wcout << L"列举" << endl;
wcout << "Done" << endl;
return 0;
}
But the Chinese characters are output as question marks
Does anyone know how I can get them output in the correct form
Thanks
Even though you are sending correct Chinese symbols to console.
Windows setting should be changed so that it supports those language specific characters.
You need to change PC language to Chinese from "Region and Language" section from control panel, which is by default to US English.

c++ programming

I am writing a program that calculates and prints parking charges i am using get.line for the user to enter, how do i use "find" to separate the line?
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
int main ()
{
string enter_date;
string enter_time;
string exit_date;
string exit_time;
int calculatecharge;
int num;
cout << "Please enter the date and time the car is entering "<< endl
<< "the parking garage in the following format: YY/MM/DD hh:mm"<< endl;
getline (cin,enter_date);
getline (cin,enter_time);
cout<< "Please enter the date and time the car is exiting "<< endl
<< "the parking garage in the following format: YY/MM/DD hh:mm"<< endl;
getline (cin,exit_date);
getline (cin,exit_time);
find(' ')
cout<<"Parking fee due: "<< num << endl;
return 0;
}
If you are using getline() twice, try the below code.
getline (cin, enter_date, ' ');
getline (cin, enter_time);

Resources