I need to fetch user input from std::cin into a variable of type double. This is in the context of complex numbers, so it often happens to have a number input as 5i or 5.4565i.
Consider the following code in the main() function:
while (true) {
double c;
std::cin >> c;
std::cout << c << "\n\n";
}
Here's what happens:
In: 0.45
Out: 0.45
// OK
In: 5 0.45
Out: 5
0.45
// OK
In: 0.45i
Expected out: 0.45
Acutal out: 0
0
0...
I'm guessing this is because It's not recognizing 0.45i as a double. So how can I correctly fetch the value 0.45 from 0.45i, ignoring the trailing i?
Read a string first and then convert to double,
$ cat ans.cpp
#include <cstdlib>
#include <iostream>
int main()
{
std::string str;
double dbl;
while (std::cin >> str) {
dbl = std::strtod(str.data(), NULL);
std::cout << dbl << std::endl;
}
}
First you read each white space separated string into str. The strtod function will try to get as many as characters to form a floating point literate, including hex float. It returns the double parsed from this part of the string. The second parameter can be a char * pointer, which point to one pass the last character that is parsed. It is useful if you do not want to simply discard the trailing characters. It is ignored if it is null.
You can test the state of the input string. If input has failed, just get the offending token in a string and proceed. For example:
double d;
for(;;) {
std::string dummy;
std::cout << "Input :";
std::cin >> d;
if (std::cin.fail()) { // was input numeric?
std::cin.clear();
std::cin >> dummy;
if (dummy == "END") break; // out of the infinite loop...
std::cout << "Non numeric: >" << dummy << "<" << std::endl;
}
else {
std::cout << "Numeric: " << d << std::endl;
}
}
Try reading std::string and then parsing it manually.
One way could be you take input as a string and then analyse it character by character using a for loop. And then convert it into the double while ignoring the i.You could divide the string into three parts i.e integer part, decimal part and i.
And for separating these you could use conditions like if(c[i]=='.') and if(c[i]=='i').
Related
I am trying to make strings into doubles using the values that I obtained from a .txt file.
The doubles I am obtaining have no decimals. I believe that this is because, in the .txt, the decimals of the numbers are separated by a comma instead of a dot. But I don't know how to solve that.
This is a simplification of my code:
#include <iostream>
#include <fstream> // read text file
#include <stdlib.h> // strtod, atof
int main() {
std::ifstream readWindData("winddata.txt");
// check that document has been opened correctly
if (!readWindData.is_open()) {
std::cout << "Wind data could not be opened." << std::endl;
return 1;
}
// skip headers of the table
std::string firstLine;
std::getline(readWindData, firstLine);
int numberOfRows = 0; // variable to count rows of the table
// initialise strings that separate each value
std::string time, string u10st, u40st, u60st, u80st, u100st,
u116st, u160st, dir10st, dir60st, dir100st, timeDecst;
// initialise doubles
double u10, u40, u60, u80, u100, u116, u160, dir10, dir60, dir100, timeDec;
std::string nextLine;
// Read strings and turn it into doubles line by line until end
while (readWindData >> time >> u10st >> u40st >> u60st >> u80st >> u100st
>> u116st >> u160st >> dir10st >> dir60st >> dir100st >> timeDecst) {
// try two different functions to turn strings into doubles:
u10 = strtod(u10st.c_str(), NULL);
u40 = atof(u40st.c_str());
// ensure numbers are displaying all their decimals
std::cout.precision(std::numeric_limits<double>::digits10 + 1);
// see what I am getting
std::cout << time << " " << u10st << " " << u10 << " " << u40 << "\n";
std::getline(readWindData, nextLine); // this line skips some crap on the side of some rows
numberOfRows++; // counts rows
}
std::cout << "Number of rows = " << numberOfRows << "\n";
readWindData.close();
return 0;
}
these are three lines of the file:
time (hour) u10(m/s)u40(m/s)u60 (m/s)u80(m/s)u100(m/s)u116(Um/s)u160(m/s)dir10 dir60 dir100 time decimal hours
00:00 4,25636 7,18414 8,56345 9,75567 10,9667 12,1298 13,8083 110,616 131,652 141,809 0 midnight
00:10 4,54607 7,40763 8,62832 9,91782 11,2024 12,2694 14,1229 114,551 133,624 142,565 0,166666667
And these are those lines outputted with the above code:
(reminder, I std::cout time (string), u10st (string), u10 (double), u40 (double)).
00:00 4,25636 4 7
00:10 4,54607 4 7
Any ideas on how to read that 4,25636 string into a double 4.25636? The file is too long to modify.
To avoid messing around with the global locale you can just replace , with . before calling strtod()
std::replace(u10st.begin(), u10st.end(), ',', '.');
u10 = strtod(u10st.c_str(), nullptr);
But in the program shown above you can also read from ifstream directly into double using operator >>, if you use a locale where comma is a decimal separator.
readWindData.imbue(std::locale("de_DE.UTF-8")); // or fr, nl, ru, etc.
double u10, u40, u60, u80, u100, u116, u160, dir10, dir60, dir100, timeDec;
while (readWindData >> time >> u10 >> u40 >> u60 >> u80 >> u100
>> u116 >> u160 >> dir10 >> dir60 >> dir100 >> timeDec) {
Live demo
The floating point numbers are using comma decimal separators, and you are expecting periods.
This is an indication that the data was serialized with a different locale than yours.
To address this, set your locale before parsing the numbers (and restore afterwards).
For example, here is some code that parses the number "12,34" with a default period separator, and then we set the locale to Denmark and try again:
const char* number = "12,34";
double parsed_number = 0;
parsed_number = std::strtod(number, nullptr);
std::cout << "parsed number in default locale: " << parsed_number << std::endl;
std::cout << "Setting locale to Denmark (comma decimal delimiter)" << std::endl;
std::locale::global(std::locale("en_DK.utf8"));
parsed_number = std::strtod(number, nullptr);
std::cout << "parsed number in Denmark locale: " << parsed_number << std::endl;
//restore default locale
std::locale::global(std::locale(""));
Output:
parsed number in default locale: 12
Setting locale to Denmark (comma decimal delimiter)
parsed number in Denmark locale: 12.34
Live Demo
Ask around to see who serialized this data, and get the correct locale.
You can find available locales in *nix systems with locale -a
On Windows it appears to be a little more difficult
I was working on a HackerRank problem and I could not figure out why the C++ code round the double values when I am adding them and why it does not take in/print the entire string input it is given.
The code is supposed to take in an integer input (from one line), a double input (from another line), and a string input (also, from another line). Then it is supposed to print out the sum of the int input and 4, the sum of the double input and 4.0, and concatenate the string "HackerRank" to the beginning of the input string.
Here's the code I have:
#include <iostream>
#include <iomanip>
#include <limits>
int main(){
int i = 4;
double d = 4.0;
string s = "HackerRank";
// Declare second integer, double, and String variables.
// Read and save an integer, double, and String to your variables.
// Note: If you have trouble reading the entire string, please go back and review the Tutorial closely.
// Print the sum of both integer variables on a new line.
// Print the sum of the double variables on a new line.
// Concatenate and print the String variables on a new line
// The 's' variable above should be printed first.
int a;
double b;
string c;
cin >> a;
cin >> b;
cin >> c;
a = a + i;
b = b + d;
cout << a << endl;
cout << b << endl;
cout << "" + s + c;
return 0;
}
For the following input:
12
4.0
is the best place to learn and practice coding!
I got the output:
16
8
HackerRank is
When this is the expected output:
16
8.0
HackerRank is the best place to learn and practice coding!
The answer to your 2 questions:
Firstly, when you add a value of type int to a value of type float/string, the result will be of type int. This explains why you the output is 8 and not 8.0. This same rule applies to multiplication, division, and subtraction. Whenever an int is operated on by a float/double value or vice versa, the result is always of type int. Therefore, you should change the initialization of your d value to:
double d = 4.0; // Or float d = 4.0
By the way, you cannot add a decimal point to a value of type int and expect it to be a floating point/double value. The data type of the variable that stores the value must be defined/initialized with a certain data type.
Secondly, your code does not print the desired string as you are using the wrong function to get the string as input from the user. While cin is the norm to be used in input, it does not work so well with variables of type "string". The reason for this is because cin only accepts one word; one continuous int, floating point value, char, etc..., and it cannot accept an entire sentence with spaces in between because it just stops reading after it sees a space; that's the rules of cin. To bypass this problem, you'll need another function, and that is
getline(cin, variable_to_store_data);
instead of doing:
cin >> c;
Do this:
getline(cin, c);
This way, the entire sentence you inputted will be stored in the variable and not just the first word of the sentence. The getline does not ignore the words in a sentence that come after the first one; it reads all of the user input till the point he/she hits Enter, and it then stores the entire value in the variable (that's the second parameter).
By the way, if you want to output multiple things in one cout line, then do it using the following template:
cin << a << ... << z << endl; // endl is optional; depends on your needs
Avoid using the method you used above:
cout << "" + s + c;
Do it this way:
cout << "" << s << c; // Why do you have "" at the begninning? That prints nothing. You can take that off also.
On a side note, getline() also has a lot of other functions, such as reading lines from a file. Read more about it online; there are lots of resources available.
Hope this answers your question.
EDIT: To make the program work, you'll actually have to add another line to ignore the enter hit at the end of the cin >> b; command, because that saves as the string in c. Therefore, do this:
cin.ignore (std::numeric_limits<std::streamsize>::max(), '\n');
getline(cin, c);
The line I just added ignores the newline character hit at the end of the cin >> b command. This way, the compiler goes on to ask the user for the string to be stored in c. I've tried this code, and it works as it should.
Another thing, change your output statement to;
cout << "" << s << " " << c << "." << endl;
This makes the string easier to read, and it adds a space between variable s and variable c during output. Hope this helps!
This code will work...
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
int i = 4;
double d = 4.0;
string s = "HackerRank ";
int i2;
double d2;
string s2;
cin>>i2;
cin>>d2;
cin.ignore();
getline(cin, s2);
cout<<i2+i<<endl;
cout.precision(1);
cout << fixed << d+d2 << endl;
cout<<s+s2;
return 0;
}
You can use the function 'getline' to realize it.
And the following example is well done on VS2013.
#include <string>
#include <iostream>
using namespace std;
int main(){
string c;
getline(cin, c); // is there anybody ?
cout << "Hello, " + c; // Hello, is there anybody ?
system("pause");
return 1;
}
cin>>b;
use cin.ignore();
and then getline(cin, string_name);
This will read the complete string.
I am fairly new to programming and have to create a program which reads the prompt: "I have 8 dollars to spend." It then needs to print out with each word on a separate line, and then if any of the strings is numeric, it needs to be divided by 2. Therefore it should end up printing out as:
I
have
4
dollars
to
spend.
I have managed to do everything, except finding the numeric value and dividing it by 2. So far I have this:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string prompt;
string word;
cout << "Prompt: ";
getline(cin, prompt);
stringstream ss;
ss.str(prompt);
while (ss >> word)
{
cout << word << endl;
}
return 0;
}
After looking through various other posts, I cannot manage to get this to work. I'm assuming its an if/else statement within the while loop along the lines of, if numeric, set int num to num / 2 then cout << num << endl;, else cout << word << endl;, but I can't figure it out.
Thanks in advance.
You can use the stringstream class, which handles conversions between strings and other data types, to attempt to convert a given string to a number. If the attempt is successful, you know
The stringstream object allows you to treat a string as though it is a stream similar to cin or cout.
Incorporate this into your while loop, like so:
while (ss >> word)
{
int value = 0;
stringstream convert(word); //create a _stringstream_ from a string
//if *word* (and therefore *convert*) contains a numeric value,
//it can be read into an _int_
if(convert >> value) { //this will be false if the data in *convert* is not numeric
cout << value / 2 << endl;
}
else
cout << word << endl;
}
The strtol (C++11 version that works on std::string directly: std::stol) function is really good for testing whether a string holds a number, and if so, what the numeric value is.
Or you could continue using iostreams like you have been... try extracting a number (int or double variable), and if that fails, clear the error bit and read a string.
I dont have 50 rep so I cant comment, thats why I'm writing it as answer.
I think you can check it character by character, using Ascii value of each char, & if there are ascii values representing numbers between two spaces(two \n in this case as you've already seperated each word), then you have to divide the number by 2.
int i;
cin>>i;
cout<<i
when we entered Character i.e 'A' why it gives Zero output ?
Because A is not a numeric value suitable for storing in an integer, so it will leave your integer alone, as shown here:
#include <iostream>
int main (void) {
int i = 12345;
std::cin >> i;
std::cout << i << std::endl;
return 0;
}
When you run that code and enter A, it outputs 12345 as the value doesn't change.
If you want truly robust input, it's usually better to input lines as strings then convert them yourself.
"Mickey-mouse" programs or those where you have total control over the input can use the sort of input methods you're using, serious code should use more suitable methods.
If your intent is to convert an input character into its integer code, you can use something like:
#include <iostream>
int main (void) {
char c;
std::cin >> c;
std::cout << (int)c << std::endl;
return 0;
}
You should always check if the operation succeeded before continuing.
int i;
if (cin >> i)
cout << i;
else
cout << "Not a valid number!";
because the value 'A' is not stored in the variable i since it is a integer variable. i believe that is the reason the initial value 12345 is printed on the screen...
So I am currently learning C++ and decided to make a program that tests my skills I have learned so far. Now in my code I want to check if the value that the user enters is a double, if it is not a double I will put a if loop and ask them to reenter it. The problem I have is how do I go about checking what type of variable the user enters, ex- if a user enters a char or string, I can output an error message. Here is my code:
//cubes a user entered number
#include <iostream>
using namespace std;
double cube(double n); //function prototype
int main()
{
cout << "Enter the number you want to cube: "; //ask user to input number
double user;
cin >> user; //user entering the number
cout << "The cube of " << user << " is " << cube(user) << "." << endl; //displaying the cubed number
return 0;
}
double cube (double n) //function that cubes the number
{
return n*n*n; // cubing the number and returning it
}
Edit: I would have to say I just started and don't have the slightest of clue about your code, but I will check out your link. By the way, I haven't learned how to work with templates yet,I am learning about dealing with data, only Chapter 3 in my C++ Primer Plus 5th edition.
Safe C++ Way
You can define a function for this using std::istringstream:
#include <sstream>
bool is_double(std::string const& str) {
std::istringstream ss(str);
// always keep the scope of variables as close as possible. we see
// 'd' only within the following block.
{
double d;
ss >> d;
}
/* eat up trailing whitespace if there was a double read, and ensure
* there is no character left. the eof bit is set in the case that
* `std::ws` tried to read beyond the stream. */
return (ss && (ss >> std::ws).eof());
}
To assist you in figuring out what it does (some points are simplified):
Creation of a input-stringstream initialized with the string given
Reading a double value out of it using operator>>. This means skipping whitespace and trying to read a double.
If no double could be read, as in abc the stream sets the fail-bit. Note that cases like 3abc will succeed and will not set the fail-bit.
If the fail-bit is set, ss evaluates to a zero value, which means false.
If an double was read, we skip trailing whitespace. If we then are at the end of the stream (note that eof() will return true if we tried to read past the end. std::ws does exactly that), eof will return true. Note this check makes sure that 3abc will not pass our check.
If both cases, right and left of the && evaluate to true, we return true to the caller, signaling the given string is a double.
Similar, you check for int and other types. If you know how to work with templates, you know how to generalize this for other types as well. Incidentally, this is exactly what boost::lexical_cast provides to you. Check it out: http://www.boost.org/doc/libs/1_37_0/libs/conversion/lexical_cast.htm.
C Way One
This way has advantages (being fast) but also major disadvantages (can't generalized using a template, need to work with raw pointers):
#include <cstdlib>
#include <cctype>
bool is_double(std::string const& s) {
char * endptr;
std::strtod(s.c_str(), &endptr);
if(endptr != s.c_str()) // skip trailing whitespace
while(std::isspace(*endptr)) endptr++;
return (endptr != s.c_str() && *endptr == '\0');
}
strtod will set endptr to the last character processed. Which is in our case the terminating null character. If no conversion was performed, endptr is set to the value of the string given to strtod.
C Way Two
One might thing that std::sscanf does the trick. But it's easy to oversee something. Here is the correct way to do it:
#include <cstdio>
bool is_double(std::string const& s) {
int n;
double d;
return (std::sscanf(s.c_str(), "%lf %n", &d, &n) >= 1 &&
n == static_cast<int>(s.size()));
}
std::sscanf will return the items converted. Although the Standard specifies that %n is not included in that count, several sources contradict each other. It's the best to compare >= to get it right (see the manpage of sscanf). n will be set to the amount of the processed characters. It is compared to the size of the string. The space between the two format specifiers accounts for optional trailing whitespace.
Conclusion
If you are a beginner, read into std::stringstream and do it the C++ way. Best not mess with pointers until you feel good with the general concept of C++.
There is no suitable way to check if a string really contains a double within the standard library. You probably want to use Boost. The following solution is inspired by recipe 3.3 in C++ Cookbook:
#include <iostream>
#include <boost/lexical_cast.hpp>
using namespace std;
using namespace boost;
double cube(double n);
int main()
{
while(true)
{
cout << "Enter the number you want to cube: ";
string user;
cin >> user;
try
{
// The following instruction tries to parse a double from the 'user' string.
// If the parsing fails, it raises an exception of type bad_lexical_cast.
// If an exception is raised within a try{ } block, the execution proceeds
// with one of the following catch() blocks
double d = lexical_cast <double> (user);
cout << "The cube of " << d << " is " << cube(d) << "." << endl;
break;
}
catch(bad_lexical_cast &e)
{
// This code is executed if the lexical_cast raised an exception; We
// put an error message and continue with the loop
cout << "The inserted string was not a valid double!" << endl;
}
}
return 0;
}
double cube (double n)
{
return n*n*n;
}
sscanf can do what you want; it returns the number of arguments properly processed. This should get you started:
//cubes a user entered number
#include <iostream>
#include <cstdio>
using namespace std;
double cube(double n); //function prototype
int main()
{
cout << "Enter the number you want to cube: "; //ask user to input number
string user;
cin >> user; //user entering the number
// Convert the number to a double.
double value;
if(sscanf(user.c_str(), "%lf", &value) != 1)
{
cout << "Bad! " << user << " isn't a number!" << endl;
return 1;
}
cout << "The cube of " << user << " is " << cube(user) << "." << endl; //displaying the cubed number
return 0;
}
double cube (double n) //function that cubes the number
{
return n*n*n; // cubing the number and returning it
}
Other methods posted in other answers have their advantages and disadvantages. This one has issues with trailing characters and isn't "C++"-y.
I would have to say I just started and don't have the slightest of clue about your code, but I will check out your link. By the way, I haven't learned how to work with templates yet,I am learning about dealing with data, only Chapter 3 in my C++ Primer Plus 5th edition.
You can fall back on C and use strtod
You program reads in a string and then passes it to a function that attempts to convert the string into double.
bool is_double(const char* strIn, double& dblOut) {
char* lastConvert = NULL;
double d = strtod(strIn, &lastConvert);
if(lastConvert == strIn){
return false;
} else {
dblOut = d;
return true;
}
}