C++ :: Getline And Atof - Convert Read Value Into Floating Number
Aug 3, 2012
Having an issue with the following code. I read in a decimal value from a text file, use atof (or strtod, either gives the same error) to convert the read value into a floating point number. When putting the output of atof in a double, it works fine. However when putting the output of atof into a float, the decimal places get lost. Putting the result in a double, then into a float gives the same result. This only happens when using getline.
I've tried using temp arrays to put the result of pch in, sprintfs and such to try and work around it, but whenever a value originates from the getline command, the float value always loses decimal places. I've also tried varying the precision in the printf statement, but its always the same. When I write the float result to a file via FILE.write in binary mode, the result comes out without the decimal places when I read it back in later.
The simple solution is to not use float and use double, but there are a number of reasons I'm using float to begin with and don't want to change that.
Side note, this code works fine on a windows machine, the error is coming from using g++ on a mac.
Code:
printf("Reading from File
");
ifstream FILE(argv[2],ios::in);
char BU[128];
char *pch;
[Code] ....
Code output :
Reading from File
338769.0109
338769.010900
338769.000000
View 4 Replies
ADVERTISEMENT
Mar 10, 2013
I'm trying to convert 4 hex register into floating point value using IEEE 754 floating point format. My device will reply 4 register value. The problem is that it always reply for example 0x10 as 10 when i use getc() hence using char variable to store it is not ideal.
Code:
union {
char c[4];
float f;
} conv;
View 4 Replies
View Related
Dec 24, 2014
This was an exercise from a book (convert a character string into a floating point value). It seems to work with both negative and positive decimal numbers.
It basically get each digit from the string multiplies by 10 to hold the place, and then adds the next to the result, if that makes sense.
Then determines where the decimal and null character are to figure out what to multiply by (1/1000 or whatever) to determine where the decimal should go. The variable names dealing with this part of the program aren't accurate names right now.
My question is, the output, is always putting 6 zero's. So if the argument is .95, Ideally the output should say .95 and not .950000, even though the value is still correct.
I know there the %.2f to determine the amount of decimals, but the amount of decimals in these instances would be varying depending on the argument sent to the function.
So, if 600.158 was sent as an argument, only 3 decimals would be displayed, as opposed to two from the previous example. Is there a way to do this?
Code:
// Function to convert a string to an integer
#include <stdio.h>
double strToFloat (const char string[])
[Code].....
View 7 Replies
View Related
Mar 31, 2014
Can we change the floating point number format from scientific format to below example format ?
FROM TO
==========================
2.06374E-03 ---> 206370-8
-4.30311E-01 ---> -.430310
-4.28146E-04 ---> -42815-8
==========================
View 6 Replies
View Related
Apr 15, 2014
I wrote this program:
#include <iostream>
#include <ctime>
#include <ctype.h>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
[Code] ....
LETTER OR '.' ? s
LETTER OR '.' ? f
LETTER OR '.' ? b
LETTER OR '.' ? e
LETTER OR '.' ? a
LETTER OR '.' ? g
LETTER OR '.' ? .
VECTOR : [ SFBEAG ]
SENTENCE ?
But after this i can't write anything. Do you know why? I've never had problems with getline... when i used cin >> it read but only one word. Now it doesn't even let read...
View 1 Replies
View Related
Feb 19, 2013
In short I'm converting a floating point bilinear image resampling routine into one that only uses fixed point arithmetic. I've gotten rid of nearly all the floats now, in fact all but one and the results at the moment are in distinguishable from the floating point version. It's a maths issue really. Some pseudocode goes like this.
Code:
for( int xx=0; xx<ow; xx++ ) {
int_center = (ccx >> 16);
int temp = xx * 2;
for (j = int_center; j <= int_center + 1; j++)
[Code] ....
Where ccx is and integer error accumulator that gives me a scaled integer. Shifting down buy 16 gives me the relative pixel I need to be working on. The line just after where the inner loop begins is where I have the last remaining float. FILTER_FACTOR is essentially a percentage by which I scale the error accumulator to the correct amount.
For example.
ccx = 98303. Which is a value of 1.5 when shited down by 16 bits. Obviously I can shift it because it will round and I lose the precision. Lets say FILTER_FACTOR is 39321. Which is 60% of 1 (65535) So what I'd like to know is, is it possible to use the FILTER_FACTOR as an integer and do some fancy integer math to scale the result from (ccx - (j<<16)) by the representative amount that is FILTER_FACTOR. In this example 60%. Effectively getting 40% of (ccx - (j<<16)) At the moment FILTER_FACTOR is still a float and therefore 0.6, which of course works just fine.
View 4 Replies
View Related
May 8, 2014
Code:
#include<stdio.h>
#include<conio.h>
float square(float);
void main() { clrscr();
float a,b;
printf("ENter a Number");
scanf("%f",&a);
[Code] ....
In the above program, I am calculating the square of float number. But sometimes the number is entered as NAN and sometimes Output is NAN. What is NAN? I am entering floating point number, then y NAN is entered?
SEE the Image attached for the OUTPUT.
View 2 Replies
View Related
Mar 3, 2013
I am having trouble understanding the mantissa of a floating point number. I have divided up the floating point number into the sign bit, the exponent and the mantissa, I have found the exponent, but I am not sure what to do with the mantissa? From what I have gathered so far i divide the mantissa by ten until I get a number between 1 and 10. after that i convert the number into a decimal with everything after the decimal point (or radix) being a fractional number. But when I do that on paper I dont get my intended number. How do i put the exponent and mantissa together to make a decimal from my floating point?
ex. input is 00111010000111111111011000001000
sign is 0
exponent is 01110100 which is 64+32+16+4-127=-11
mantissa is 00111111111011000001000 which would be 1.11111111011000001
When i convert that i get 1.99756622314 i dont know what to do with the -11 exponent and the answer i want is 6.1e-4
View 1 Replies
View Related
Sep 25, 2014
i want to calculate age of a person so then i try to make an algorithm which is used in a function like (i know to use math.h for abs()) double age_calculator( int year, int month, int day);
Code:
double age_calculator( int year, int month, int day);
// p means present(current)
int pyear=(2014-year)*365;
int pmonth=(abs(9-month))*30;
int pday=abs(25-day);
return (pyear+pmonth+pday)/365.0;
how can i use current date, i mean i want to use date of present day instead of 2014,25 and 9. I don't know how i can use date of every day.For instance my function returns 14.74 for the parameters; 2000, 01, 01 (for the current day 25/09/2014)
View 6 Replies
View Related
May 31, 2013
I am trying to get text file and read only first 100 lines of the text content using c/c++. how can i do it?
is it something like string line;
getline(stream,line);
??
View 7 Replies
View Related
Jul 6, 2013
Code: Complete the program below which converts a binary number into a decimal number. Sample outputs are shown belowComplete the program below which converts a binary number into a decimal number. Sample outputs are shown below.
Sample Output 1:
8-bit Binary Number => 11111111
Decimal Number = 255
Sample Output 2:
8-bit Binary Number => 10101010
Decimal Number = 170
Sample Output 3:
8-bit Binary Number => 101010102
Number entered is not a binary number
#include <iostream>
using namespace std;
int main()
{
int num;
[code]....
View 2 Replies
View Related
Jul 5, 2013
Here's the part of the codes where I tried to use boolean expression:
Code:
#include <iostream>
using namespace std;
int main() {
int num;
cout << "8-bit Binary Number=";
cin >> num;
[Code] .....
How can I get started with the body?
View 7 Replies
View Related
Jul 5, 2013
Here's the part of the codes where I tried to use boolean expression:
#include <iostream>
using namespace std;
int main()
[Code].....
May I know that how can I get started with the body?
View 5 Replies
View Related
Feb 19, 2014
I am trying to rewrite a matlab code into c++. It reads a file and do the deresolution on it and convert it to 8 bit.
but the VC++ code does not work for all ranges of max_den and min_den. Specially when the there is a small difference between them. (I tried that range on Matlab, it works)
MATLAB Code:
fid = fopen(ORIGINAL_FILENAME,'r');
A = fread(fid,X*Y*Z,'int32=>int32')
D = uint8(255/double(Maximum_Density-Minimum_Density)*(A-Minimum_Density));
A = reshape(D,X,Y,Z);
fid=fopen(OUTPUT_FILENAME,'w');
[code].....
For example when actual range of data is [-3023,3072] and the max_den=-3000, min_den=1000, it works well. but when you choose max_den=-1000, min den=1000, it doesn't work properly.The raw file includes 3D data associated with a mhd file. When the range is not wide enough, the converted file becomes messy.
View 1 Replies
View Related
Dec 2, 2013
I defined
enum boundaryType_t {inside, inlet, outlet, wall, periodic};
now I have file written like this:
inside outlet
I want my program read the file and when encounter any enum type, instead of treating it as a string, I want the program store it in memory as a enum value.
How can I do that?
string s;
ifs>>s;
s // how to convert it to enum???
View 5 Replies
View Related
Mar 25, 2013
I am trying to read an array values from Tmin.txt file. I take array size from user viz. number of raw & column and then, read the following data from Tmin.txt file which look like this:
20 40 20 25 30
20 30 40 20 25
20 40 20 25 30
20 30 40 20 25
30 40 40 30 40
I am using getline, and then converting it to int by using atoi. But my code is not working.
Code :
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
[Code] ....
View 10 Replies
View Related
Nov 27, 2011
Code:
// declare child arrays
public string[,] childcolor;
public string[,] childgeneo;
// create dictionaries
public Dictionary<string, int> colors = new Dictionary<string, int>();
public string color; // name for dictionary
public Dictionary<string, string> geneo = new Dictionary<string, string>();
[Code]...
I'm getting "field' is used like a 'type' error on jw.childcolor which causes other parts to error out.
How do I fix this & why am I getting the error?
I want to convert the dictionaries to arrays to read & write sequentially.
View 4 Replies
View Related
May 15, 2013
I want to convert image to number.
View 12 Replies
View Related
Mar 6, 2015
How to convert e.g. number 75 to base 4 representation? The result should be 1023. I plan to use uint32_t
x = a3.43 + a2.42 + a1.41 + a0.40 = a3.64 + a2.16 + a1.4 + a0
View 9 Replies
View Related
Dec 7, 2014
I had an exercise that required me to convert a number to binary (base 2) which as simple enough.
Code:
#include <iostream>#include <iomanip>
#include <cmath>
using namespace std;
void Conversion (int n);
int main () {
[Code] .....
I now have a follow on exercise that requires me to convert to binary from ant base up to 10, i thought this would just be replacing the 2 with a variable obtained form the user, but i am having problems as within the function i am getting an error that i haven't passed enough arguments and i cant see why i get this. I did the following:
Code:
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
float Conversion (int n, int b);
[Code] ....
View 2 Replies
View Related
Aug 19, 2014
How do you convert a number float in a range of -10.0f to 17.0f to a eqivalent number in the range of 0.0f to 1.0f?The code does not work well. floaty is the float to change.
//change range to 0..1
diamond[x][y] = (floaty - minY) / (maxY - minY);
View 19 Replies
View Related
Dec 6, 2013
I receive a negative number but in big endian order. ntohl seems work for unsigned only.
Is there a method for me to translate it back to the original negative number from big endian?
View 2 Replies
View Related
Sep 17, 2014
#include "stdio.h"
#include <stdarg.h>
#include <math.h>
// Main Function
int main(void){
int number;
printf(" Please enter a number from 1-10? ");
scanf("%d", &number);
[Code] ....
I took the while statement out didn't want that in there.
View 2 Replies
View Related
Oct 12, 2013
i want to convert the Digits in words.I have already a code but in my code the value is coming like for ex:-540000(Five hundered and Fourty Thousand ).but i want Five Lakh and Fourty thousand.
View 4 Replies
View Related
Oct 14, 2014
I wanted to write a program to convert a number into a more readable format. It's like, if the input enters the number as 2361263 the output should be like 2,361,263. I went about this problem like extraction the number first and then if the count was equal to multiple of three i'd print ',' instead of the number.
But initially when i wrote the code for extracting a single digit from the number
Code:
#include<stdio.h>
void main() {
unsigned long long num=pow(2,50);
int count=0;
while(num!=0) {
int last_digit;
[Code]....
I know that I'd lose the number when i finish printing it, but still I only end up printing the entire number in reverse order.
As in if the input is 1234 the output is 4,321 which is not what i want.
One way of overcoming this problem is to store the values in an array and then reading then back from the end. But i wanted to know if there is a better solution than this? To extract the digits from the number in the same order as it is in the number
View 5 Replies
View Related
Mar 27, 2013
I nead to write a program that convert an octal number to decimal number, I thought I did it right but it doesn't work.. I have to use in the first for loop as it is because it is part of the instructions (student homework).
#include <iostream>
#include <math.h>
using namespace std;
void main() {
double numOfDig, num, newNum;
[Code] ....
View 1 Replies
View Related