why does scanf() turn none into two float - c++

I'm a newbie on c++ and I am encountering some problems. The question to be solved is
having 5 inputs, the inputs can include integers and the string "none", try to sum up the integers in the inputs and the result should be a double.
Here's what I have done: I put five inputs in the array and use scanf() to turn them into double, but when I enter none and use printf to see the array, I found out that it turns none into two float, does scanf has any restrictions on converting strings to double?
Here's the code :
int main()
{
double sc[5];
for (int i = 0; i < 5; i++) {
sc[i] = 3;
scanf("%lf", &sc[i]);
printf("%lf\n", sc[i]);
}
}
and the output is:
34
34.000000
none
3.000000
3.000000
56
56.000000
89
89.000000

Calling scanf("%lf", &something) will populate something if and only if the input is a valid floating point number. The string none is not such a valid number.
The reason you're getting two 3 values is probably because nan is a valid floating point number, so scanf finds the n and says "Hah, this will be a nan". But then it finds, much to its disappointment, the o and decides it's no longer valid.
But it has already read the no from the input stream. It then goes back and does the same thing with the ne (a).
After that, you start giving it valid numbers again so it carries on.
If you want to be able to handle strings and numbers, you're going to have to use the least restrictive (string) and then decide. Something like this (though scanf("%s") is dangerous for real code, it's probably okay for classwork):
The following code does that and (as you should) checks the return value from scanf/sscanf to ensure it was correctly scanned:
#include <stdio.h>
#include <string.h>
int main() {
static char bigstring[1000];
double sc[5];
for (int i = 0; i < 5; i++) {
sc[i] = 3;
if (scanf("%s", bigstring) != 1) {
puts("*** Could not get input");
return 1;
}
if (strcmp("none", bigstring) == 0) {
puts("Got 'none', converting to -999");
sc[i] = -999;
} else if (sscanf(bigstring, "%lf", &sc[i]) != 1) {
printf("*** Could not convert input '%s' to float", bigstring);
return 1;
} else {
printf("Got %f\n", sc[i]);
}
}
puts("\nThe five entered numbers were:");
for (int i = 0; i < 5; i++) {
printf(" %lf\n", sc[i]);
}
}
Running that works properly with basic test data:
pax:~$ printf "34\nnone\n56\n89\n111\n" | ./qq
Got 34.000000
Got 'none', converting to -999
Got 56.000000
Got 89.000000
Got 111.000000
The five entered numbers were:
34.000000
-999.000000
56.000000
89.000000
111.000000
(a) Interestingly, it appears this only happens with real user input, not piping data through the program as per my printf statement.
The ideal behaviour, in my opinion, would be to leave the input stream pointing at the invalid data. In other words, unless the exact text is valid (like nan), the pointer should not move at all. However, there's a footnote in the C standard that allows for this behaviour if, for example, the input stream is not seekable:
fscanf pushes back at most one input character onto the input stream.
So, while it may be able to back up further than that on a pipeline, that may not be the case when dealing with terminal input.
When I enter x as input, it appears it does push that back, because every single following array element is populated with 3, meaning it's reading that x continuously. Anything starting with an n seems to consume that and the following character only.

Related

Unable to get output for multiplication

#include<iostream.h>
int main()
{
int n[50],p=0;
double g=1;
char c;
cout<<"\n press a for addition";
cout<<"\n press m for multiplication";
cin>>c;
if(c=='a')
{
for(int i=0;n[i]=!'=',i<50;i++)
{
cin>>n[i];
p+=n[i];
}
cout<<p;
}
else if(c=='m')
{
for(int j=0;n[j]=!'=',j<50;j++)
{
cin>>n[j];
g*=n[j];
}
cout<<g;
}
else cout<<"wrong input";
}
I have wrote this code, it works perfectly fine when addition part is used but when multiplication is done it isn't giving the final product. Whenever I click '=' it doesn't give any final product during multiplication but while addition the same logic works fine.
You have multiple problems:
The array n isn't initialized so its contents will be indeterminate, and using indeterminate values lead to undefined behavior.
Fortunately (for you) the loop "condition" expression n[j]=!'=',j<50 doesn't do what you probably think it does... The part n[j]=!'=' is actually equal to n[j] = !'='. That is you assign the result of !'=' to n[j].
And about the loop "condition", the result of n[j]=!'=',j<50 is the result of j<50 only, because that's how the comma operator works.
With cin>>n[j] you can't read arbitrary characters, as n[j] is an int and you only read int values. The only way to get a '=' from the input is if the user inputs the value 61 which happens to be the ASCII encoded value for '='.
Your loop "condition" also have another flaw, because you increase j before you check n[j], so in the condition n[j] will always be an element of the array that you haven't read into.
And building on the previous point, because the value of j will be wrong in the "condition" you will go out of bounds of the array which also leads to undefined behavior.

C++ Reversing Number using to_string, reverse, stoi combo giving runtime error Instance Out of Range

Stuck trying to resolve a runtime error due to an instance out of range exception. I'm taking the int into a string, reversing the string and returning it to int using stoi. Simple test numbers reverse perfectly but bigger numbers are out of range. Not sure where in the code to adjust for the out of range exception. Super stuck, please help.
int reverse(int x) {
bool negFlag = false;
if(x < 0)
{
negFlag = true;
}
string xString = std::to_string(abs(x));
std::reverse(xString.begin(), xString.end());
int xNum = std::stoi(xString);
return (negFlag == true)? -xNum: xNum;
}
Here is the error returned:
terminate called after throwing an instance of 'std::out_of_range'
what(): stoi
Last executed input: 1534236469
The smaller number work well.
Your input: 123
Output: 321
Expected: 321
Is there a \0 at the end of my string that is throwing everything off in the conversion? New to these C++ methods. Thanks so much. Would really love to nail these methods down and be able to use them with ease.
Vital Info:
Note: Assume we are dealing with an environment which could only store
integers within the 32-bit signed integer range:
[-231 to +231 -1]
For the purpose of this problem, assume that your function returns 0 when
the reversed integer overflows.
-->> Not sure how to formulate an if statement that will return 0; when the 32-bit is exceeded.
The reverse of 1,534,236,469 is 9,646,324,351. The maximum value that a typical 32bit int can hold is 2,147,483,647, so it doesn't fit into it.
You need to have your function return something larger, e.g. long long (which is at least 64bit) and use the appropriate conversion function for it, e.g.:
long long reverse(int x) {
//...
long long xNum = std::stoll(xString);
//...
}
Whether you want to change the input type to something larger as well, depends on the maximum input value that your function is supposed to be able to process.
After your edit:
You can catch the conversion error and return 0 in that case (requires #include<stdexcept>):
try {
int xNum = std::stoi(xString);
// Maybe insert additional range test according to assignment (?)
return (negFlag == true)? -xNum: xNum;
} catch(const std::out_of_range&) {
return 0;
}
Assuming int to be 32bit two's-complement (which I understand the assignment is trying to suggest with the value range, which is presumable supposed to be [-2**31, 2**31-1]):
Also note that your initial call to abs has undefined behavior if x == -2**31, because 2**31 is not representable in the 32bit int. So you need to first make a special case for that before you call abs to avoid undefined behavior.
Similarly you need to consider the case where the result of the function should be -2**31. But you may notice that this case is irrelevant because its corresponding input value would already fall out-of-range of int.
The input range is bigger than an int can hold. Change it to long long and it should work.
long long reverse(long long x) {
bool negFlag = false;
if(x < 0)
{
negFlag = true;
}
string xString = std::to_string(abs(x));
std::reverse(xString.begin(), xString.end());
long long xNum = std::stoll(xString);
return (negFlag == true)? -xNum: xNum;
}
Note that the return type needs to be changed as well.

Arithmetic Error When Converting String to Double

I'm writing a function to convert a user provided string into a double. It works quite well for certain values, but fails for others. For example
string_to_double("123.45") = 123.45
string_to_double(12345) = 12345
but
string_to_double(123.4567) = 123.457
I'm fairly certain that this is some kind of round off error, but I'm not using approximations nor am I using very small or large values. My question is two-fold why am I getting these strange results and how can I change my code to get more accurate results? I'm also doing this as a personal challenge, so suggestions to use methods such as std::stod are not helpful. I believe the problem occurs in the second for-loop, but I felt it was wise to include the entire method because if I missed something it isn't that much extra code to read.
My Code
template <class T>
double numerical_descriptive_measures<T>::string_to_double(std::string user_input)
{
double numeric_value = 0;//Stores numeric value of string. Return value.
int user_input_size = user_input.size();
int power = 0;
/*This loop is for the characteristic portion of the input
once this loop finishes, we know what to multiply the
characterstic portion by(e.g. 1234 = 1*10^3 + 2*10^2 + 3*10^1 + 4)
*/
for(int i = 0;i < user_input_size;i++)
{
if(user_input[i] == '.')
break;
else
power++;
}
/*This loop is for the mantissa. If this portion is zero,
the loop doesn't execute because i will be greater than
user_input_size.*/
for(int i = 0;i < user_input_size;i++)
{
if(user_input[i] != '.')
{
numeric_value += ((double)user_input[i] - 48.0)*pow(10,power-i-1);
}
else
{
double power = -1.0;
for(int j = i+1;j < user_input_size;j++)
{
numeric_value += ((double)user_input[j] - 48.0)*pow(10.0,power);
power = power-1.0;
}
break;
}
}
return numeric_value;
}
The problem is not that you are producing the wrong floating point value, the problem is that you are printing it with insufficient precision:
std::cout<<data<<std::endl
This will only print about six digits of precision. You can use std::setprecision or other methods to print more.
Your code is not producing an incorrect value for "123.4567" but it will produce incorrect values in general. For example, string_to_double("0.0012") produces (on Visual Studio 2015)
0.0012000000000000001117161918529063768801279366016387939453125
but the correct answer is
0.00119999999999999989487575735580549007863737642765045166015625
(You would have to print them to 17 significant digits to tell the difference.)
The problem is that you can't use floating-point to convert to floating-point -- it does not have enough precision in general.
(I've written a lot about this on my site; for example, see http://www.exploringbinary.com/quick-and-dirty-decimal-to-floating-point-conversion/ and http://www.exploringbinary.com/decimal-to-floating-point-needs-arbitrary-precision/ .)

ARRAYS DEBUGGING incorrect outputs, complex algorithm

I made this algorithm, i was debugging it to see why it wasnt working, but then i started getting weird stuff while printing arrays at the end of each cycle to see where the problem first occurred.
At a first glance, it seemed my while cycles didn't take into consideration the last array value, but i dunno...
all info about algorithm and everything is in the source.
What i'd like to understand is, primarily, the answer to this question:
Why does the output change sometimes?? If i run the program, 60-70% of the time i get answer 14 (which should be wrong), but some other times i get weird stuff as the result...why??
how can i debug the code if i keep getting different results....plus, if i compile for release and not debug (running codeblocks under latest gcc available in debian sid here), i get most of the times 9 as result.
CODE:
#include <iostream>
#include <vector>
/*void print_array
{
std::cout<<" ( ";
for (int i = 0; i < n; i++) { std::cout<<array[i]<<" "; }
std::cout<<")"<<std::endl;
}*/
///this algorithm must take an array of elements and return the maximum achievable sum
///within any of the sub-arrays (or sub-segments) of the array (the sum must be composed of adjacent numbers within the array)
///it will squeeze the array ...(...positive numbers...)(...negative numbers...)(...positive numbers...)...
///into ...(positive number)(negative number)(positive number)...
///then it will 'remove' any negative numbers in case it would be convienent so that the sum between 2 positive numbers
///separated by 1 negative number would result in the highest achievable number, like this:
// -- (3,-4,4) if u do 'remove' the negative number in order to unite the positive ones, i will get 3-4+4=3. So it would
// be better not to remove the negative number, and let 4 be the highest number achievable, without any sums
// -- (3,-1,4) in this case removing -1 will result in 3-1+4=6, 6 is bigger than both 3 and 4, so it would be convienent to remove the
// negative number and sum all of the three up into one number
///so what this step does is shrink the array furthermore if it is possible to 'remove' any negatives in a smart way
///i also make it reiterate for as long as there is no more shrinking available, because if you think about it not always
///can the pc know if, after a shrinking has occured, there are more shrinkings to be done
///then, lastly, it will calculate which of the positive numbers left is highest, and it will choose that as remaining maximum sum :)
///expected result for the array of input, s[], would be (i think), 7
int main() {
const int n=4;
int s[n+1]={3,-2,4,-4,6};
int k[n+1]={0};
///PRINT ARRAY, FOR DEBUG
std::cout<<" ( ";
for (int i = 0; i <= n; i++) { std::cout<<k[i]<<" "; }
std::cout<<")"<<std::endl;
int i=0, j=0;
// step 1: compress negative and postive subsegments of array s[] into single numbers within array k[]
/*while (i<=n)
{
while (s[i]>=0)
{
k[j]+=s[i]; ++i;
}
++j;
while (s[i]<0)
{
k[j]+=s[i]; ++i;
}
++j;
}*/
while (i<=n)
{
while (s[i]>=0)
{
if (i>n) break;
k[j]+=s[i]; ++i;
}
++j;
while (s[i]<0)
{
if (i>n) break;
k[j]+=s[i]; ++i;
}
++j;
}
std::cout<<"STEP 1 : ";
///PRINT ARRAY, FOR DEBUG
std::cout<<" ( ";
for (int i = 0; i <= n; i++) { std::cout<<k[i]<<" "; }
std::cout<<")"<<std::endl;
j=0;
// step 2: remove negative numbers when handy
std::cout<<"checked WRONG! "<<unsigned(k[3])<<std::endl;
int p=1;
while (p!=0)
{
p=0;
while (j<=n)
{
std::cout<<"checked right! "<<unsigned(k[j+1])<<std::endl;
if (k[j]<=0) { ++j; continue;}
if ( k[j]>unsigned(k[j+1]) && k[j+2]>unsigned(k[j+1]) )
{
std::cout<<"checked right!"<<std::endl;
k[j+2]=k[j]+k[j+1]+k[j+2];
k[j]=0; k[j+1]=0;
++p;
}
j+=2;
}
}
std::cout<<"STEP 2 : ";
///PRINT ARRAY, FOR DEBUG
std::cout<<" ( ";
for (int i = 0; i <= n; i++) { std::cout<<k[i]<<" "; }
std::cout<<")"<<std::endl;
j=0; i=0; //i will now use "i" and "p" variables for completely different purposes, as not to waste memory
// i will be final value that algorithm needed to find
// p will be a value to put within i if it is the biggest number found yet, it will keep changing as i go through the array....
// step 3: check which positive number is bigger: IT IS THE MAX ACHIEVABLE SUM!!
while (j<=n)
{
if(k[j]<=0) { ++j; continue; }
p=k[j]; if (p>i) { std::swap(p,i); }
j+=2;
}
std::cout<<std::endl<<"MAX ACHIEVABLE SUM WITHIN SUBSEGMENTS OF ARRAY : "<<i<<std::endl;
return 0;
}
might there be problems because im not using vectors??
Thanks for your help!
EDIT: i found both my algorithm bugs!
one is the one mentioned by user m24p, found in step 1 of the algorithm, which i fixed with a kinda-ugly get-around which ill get to cleaning up later...
the other is found in step2. it seems that in the while expression check, where i check something against unsigned values of the array, what is really checked is that something agains unsigned values of some weird numbers.
i tested it, with simple cout output:
IF i do unsigned(k[anyindexofk]) and the value contained in that spot is a positive number, i get the positive number of course which is unsigned
IF that number is negative though, the value won't be simply unsigned, but look very different, like i stepped over the array or something...i get this number "4294967292" when im instead expecting -2 to return as 2 or -4 to be 4.
(that number is for -4, -2 gives 4294967294)
I edited the sources with my new stuff, thanks for the help!
EDIT 2: nvm i resolved with std::abs() using cmath libs of c++
would there have been any other ways without using abs?
In your code, you have:
while (s[i]>=0)
{
k[j]+=s[i]; ++i;
}
Where s is initialized like so
int s[n+1]={3,-2,4,-4,6};
This is one obvious bug. Your while loop will overstep the array and hit garbage data that may or may not be zeroed out. Nothing stops i from being bigger than n+1. Clean up your code so that you don't overstep arrays, and then try debugging it. Also, your question is needs to be much more specific for me to feel comfortable answering your question, but fixing bugs like the one I pointed out should make it easier to stop running into inconsistent, undefined behavior and start focusing on your algorithm. I would love to answer the question but I just can't parse what you're specifically asking or what's going wrong.

Fastest way to determine whether a string contains a real or integer value

I'm trying to write a function that is able to determine whether a string contains a real or an integer value.
This is the simplest solution I could think of:
int containsStringAnInt(char* strg){
for (int i =0; i < strlen(strg); i++) {if (strg[i]=='.') return 0;}
return 1;
}
But this solution is really slow when the string is long... Any optimization suggestions?
Any help would really be appreciated!
What's the syntax of your real numbers?
1e-6 is valid C++ for a literal, but will be passed as integer by your test.
Is your string hundreds of characters long? Otherwise, don't care about any possible performance issues.
The only inefficiency is that you are using strlen() in a bad way, which means a lot of iterations over the string (inside strlen). For a simpler solution, with the same time complexity (O(n)), but probably slightly faster, use strchr().
You are using strlen, which means you are not worried about unicode. In that case why to use strlen or strchr, just check for '\0' (Null char)
int containsStringAnInt(char* strg){
for (int i =0;strg[i]!='\0'; i++) {
if (strg[i]=='.') return 0;}
return 1; }
Only one parsing through the string, than parsing through the string in each iteration of the loop.
Your function does not take into account exponential notation of reals (1E7, 1E-7 are both doubles)
Use strtol() to try to convert the string to integer first; it will also return the first position in the string where the parsing failed (this will be '.' if the number is real). If the parsing stopped at '.', use strtod() to try to convert to double. Again, the function will return the position in the string where the parsing stopped.
Don't worry about performance, until you have profiled the program. Otherwise, for fastest possible code, construct a regular expression that describes acceptable syntax of numbers, and hand-convert it first into a FSM, then into highly optimized code.
So the standard note first, please don't worry about performance too much if not profiled yet :)
I'm not sure about the manual loop and checking for a dot. Two issues
Depending on the locale, the dot can actually be a "," too (here in Germany that's the case :)
As others noted, there is the issue with numbers like 1e7
Previously I had a version using sscanf here. But measuring performance showed that sscanf is is significantly slower for bigger data-sets. So I'll show the faster solution first (Well, it's also a whole more simple. I had several bugs in the sscanf version until I got it working, while the strto[ld] version worked the first try):
enum {
REAL,
INTEGER,
NEITHER_NOR
};
int what(char const* strg){
char *endp;
strtol(strg, &endp, 10);
if(*strg && !*endp)
return INTEGER;
strtod(strg, &endp);
if(*strg && !*endp)
return REAL;
return NEITHER_NOR;
}
Just for fun, here is the version using sscanf:
int what(char const* strg) {
// test for int
{
int d; // converted value
int n = 0; // number of chars read
int rd = std::sscanf(strg, "%d %n", &d, &n);
if(!strg[n] && rd == 1) {
return INTEGER;
}
}
// test for double
{
double v; // converted value
int n = 0; // number of chars read
int rd = std::sscanf(strg, "%lf %n", &v, &n);
if(!strg[n] && rd == 1) {
return REAL;
}
}
return NEITHER_NOR;
}
I think that should work. Have fun.
Test was done by converting test strings (small ones) randomly 10000000 times in a loop:
6.6s for sscanf
1.7s for strto[dl]
0.5s for manual looping until "."
Clear win for strto[ld], considering it will parse numbers correctly I will praise it as the winner over manual looping. Anyway, 1.2s/10000000 = 0.00000012 difference roughly for one conversion isn't all that much in the end.
Strlen walks the string to find the length of the string.
You are calling strlen with every pass of the loop. Hence, you are walking the string way many more times than necessary. This tiny change should give you a huge performance improvement:
int containsStringAnInt(char* strg){
int len = strlen(strg);
for (int i =0; i < len; i++) {if (strg[i]=='.') return 0;}
return 1;
}
Note that all I did was find the length of the string once, at the start of the function, and refer to that value repeatedly in the loop.
Please let us know what kind of performance improvement this gets you.
#Aaron, with your way also you are traversing the string twice. Once within strlen, and once again in for loop.
Best way for ASCII string traversing in for loop is to check for Null char in the loop it self. Have a look at my answer, that parses the string only once within for loop, and may be partial parsing if it finds a '.' prior to end. that way if a string is like 0.01xxx (anotther 100 chars), you need not to go till end to find the length.
#include <stdlib.h>
int containsStringAnInt(char* strg){
if (atof(strg) == atoi(strg))
return 1;
return 0;
}