C++ :: Float And Double Data Types - Cannot Store Infinite Numbers

Sep 6, 2013

I was working on float and double data types and to see the results i wrote this program:

#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main() {
ofstream outputF("output.txt");
double a = 1;
double outcome;

[Code] ....

Well I understand the part it cannot store infinite numbers. but if you take a look at the output for example (since it is too long i just added some of the outputs)

//---------------------
for the value of : 001
1
//---------------------
for the value of : 002
0.5
//---------------------
for the value of : 003
0.3333333333333333148

[Code] ....

if you look carefully at the value "5" and "10" results. it is awkwardly abnormal. which is something i couldnt understand. also it is the same with value "20", "25", "40", "50" and so on.

View 5 Replies


ADVERTISEMENT

C :: Putting Float Numbers Into Double Array

Jan 29, 2014

So I have a double array, where I'm inputting float numbers to certain points in an array. Sometimes, the numbers that are printed out are completely different from what I put in.Here is the part of the code:

Code: .

while( token != NULL ) {
num = atof(token);
test[j][i] = num;
printf( "
%s, i is %d, j is %d
", token,i,j );
printf( "number is %f
value test of i,j is %f

[code]....

Why the float num prints out fine, but when put into an array becomes garbage?I'm taking string values from a csv file and turning them into floats, but no problems seem to crop up there.I reset i when appropriate and increment j when needed, so I don't think my problems are from incorrect array values (though they might be)

View 2 Replies View Related

C++ :: Possible To Use Data Type Double Or Float For Array?

Mar 9, 2014

Can you use data type double or float for an array? ie

double n[];
or
float a;
float m[a];

My code wont accept me changing the data type..will on accept int data type. I get the following error when I try to change the array to double or float..

3310E:C++vector.cpp[Error] invalid types 'double [1000][double]' for array subscript

View 4 Replies View Related

C++ :: Read Float Numbers From Keyboard And Store Them Into Array Of 10 Elements

Jan 10, 2013

I have to complete a project that i want to read float numbers from keyboard and store them into an array of 10 elements.! Every time that a number stored into array i want to compare with previous one if they have +-10 difference .. I want to keep only 10 elements into my array so every time that i give value a[0] replace a[1], a[1] replace a[2],a[2] replace a[3]. . . .and a[10] deleted.. So when all elements of the array are similar with +-10 values print out the array.!

View 1 Replies View Related

C++ :: Infinite Loop If Enter Float Number

Dec 4, 2014

I am unable to find why my code is going into infinite loop as below. This works perfectly fine if I keep entering just the integer values but if I enter a float number it ends up in an infinite loop

int main() {
int x;
while(1){
cin>>x;
cout <<x;
}
return 0;
}

View 3 Replies View Related

C/C++ :: How To Split A String Into Various Types (integer / Float / Char)

Nov 28, 2012

I'm reading lines from a text file in C++ which contains integer + string + float number(like 3,67 with comma) + string in this order. I need the float number to sort the lines but I couldn't manage to separate the data into the types I can use so far. I tried different kind of functions and the best I could do was such a code;

void main (){
     ifstream records;
     records.open("records.txt");  
    int id;
    string line;
    char name[100];
    float gpa;

[Code] ....

This fails at reading the floating number which has comma in it and then last string is read as string starting with the comma and rest of the number. An output example is:

698 John 3 ,67

It doesn't read last string on the line as well. I understand that part but simply I need another read but what I want exactly is to separate one line using "tab" as a seperator into proper data types and then using the numbers as integers, and the grades as floating numbers. How Can I do this?

View 5 Replies View Related

C :: Program Works With Float But Not Double?

Sep 8, 2014

I tried writing a program for finding the area of a circle, and I'm wondering why it only works when i define it as 'float', but not as a double?

Code:
#include <stdio.h>
#define pi 3.14159
double
main(void)

[Code] .....

View 3 Replies View Related

C++ :: How To Template Float / Double Constants

Nov 19, 2014

I have a templated class that can either use float or double as type.

My question is now: What do I do with constant numbers in the code?

Let's assume I have a multiplication by 0.5:
In the case of float type, I want: 0.5f
In the case of double type, I want: 0.5

One answer would be to check for every constant the type and doing an if/else, but this is very annoying with lots of constants.

The code using these constants infer their type automatically by the assignment, that's why I have to take care of the constants.

View 1 Replies View Related

C++ :: Float / Double To String Conversion

Oct 22, 2013

I want a code that can convert floating/double value into string/char array(char arr[]) and also it can be run on Boreland C++ 5.02

Here I've a code but it doesn't show all the numbers. Instead, it's showing in exponential form which I don't want!!

int main() {
char* str = new char[30];
float flt = 2.4567F;
sprintf(str, "%.4g", flt );
cout<<str<<endl; //Exponential form even after 6 digits without decimal
return 0;
}

View 5 Replies View Related

C++ :: Cout Double Or Float Without Additional 0?

Jun 30, 2013

I m working calculating stuff in files, input and output data, etc..., the question is the following: I output double numbers with:

myFIle << fixed << setprecision(10) << double;

The problem i got is that when a numer is like 193123.2 it prints like 193123.200000..., so finally, ¿how can i print it with any additional 0 that i need?.

View 1 Replies View Related

C/C++ :: Built In Formula For Calculating The Range Of Float And Double?

Oct 26, 2013

I want to get the direct formula for calculating the range of double and float datatypes in c,if any.

View 1 Replies View Related

C++ :: Class To Store Nodes Of Tree - Converting Between Types

Jun 4, 2013

I'm writing a class to store a tree I have a small issue.

I have a class which stores a node of the tree, something like this:

template<class T>
class node {
private:
(stuff regarding parent node and child nodes);
T __data;
public:
// more stuff (constructors and utility functions) here
};

And I want to I want to be able to do the conversion T(node<T>) implicitly. For example, I want to be able to do this:

node<int> myNode;
myNode=5;
myNode+=10;
myNode*=9;
int x=int(myNode/4);
cout << x+myNode;

And this:

class myClass {
int x;
int y;
void print () {cout << x << ' ' << y << endl;}

[Code] ....

How could I achieve this?

View 2 Replies View Related

C++ :: Looping - Input Numbers Until User Types 0 Then Output Product Of Non Zero Numbers

May 1, 2014

How to do the problem below using loop?

Input numbers until the user types a 0, then output the product of the non 0 numbers: e

E.g., if the user types 2 4 6 0, the program should output 48

View 3 Replies View Related

C/C++ :: Addition Of Infinite Numbers Using String

Feb 26, 2013

How exactly did u accept it as a string? i mean did u use ascii values or anything? and what was your code for keeping track of carry?

View 1 Replies View Related

C++ :: Type Conversion - Float Or Double Variable Into Unsigned Char Variable And Back

May 10, 2013

I would like to convert a float or double variable into unsigned char variable and back.

float number_float = 23.453f;
unsigned char* number_char = (unsigned char*)malloc(sizeof(float));
number_char = reinterpret_cast<unsigned char*> (&number_float);
float* number_float_0 = reinterpret_cast<float*>(&number_char);

I am not getting the same value back.. why?

View 2 Replies View Related

C++ :: Find Prime Numbers Between Given Pair Of Numbers And Store Them Into Array?

Apr 18, 2014

Find all the prime numbers between a given pair of numbers. Numbers should be read in from an input file called "numbers.txt" and find all the prime numbers between them. Store the prime numbers in an array, then sort the array from greatest to least. Display the array before and after the sort.

I'm stuck on how to put the prime numbers into an array.

The input file has the numbers 1 & 100.

Here's what I have so far.

#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream fin;
fin.open("numbers.txt");

[Code] .....

View 1 Replies View Related

C/C++ :: Incrementing Float Numbers

Sep 25, 2014

I need to make a program in which the output must be like:

I=0 J=1
I=0 J=2
I=0 J=3
I=0.2 J=1.2
I=0.2 J=2.2
I=0.2 J=3.2
.....
I=2 J=?
I=2 J=?
I=2 J=?

View 5 Replies View Related

C :: Store Data In Binary File Then Use It To Find Inverse Of Matrix Form Of Data

Dec 6, 2013

I need to find inverse of a matrix data in binary file should be used to form matrix.its not finding inverse correctly rest is working good.

Code:
#include <stdio.h>
void createbin();
void display();
void inverse();
int main()
{
createbin();
display();
inverse();

[Code] ....

View 4 Replies View Related

C++ :: Using New On Primitive Data Types

Dec 12, 2014

int* count;
count = new int(1); // what???

Is this on the heap?? do i have to delete it now?

So is 'new' on a primitive data type just a way for me to allocate primitive data types (int, char, etc.) on the heap instead of the stack?

And, out of curiosity, can you do that in Java?

View 4 Replies View Related

C++ :: How To Convert Data Types

Jan 9, 2015

How to convert these data types? i have an array of bytes in unsigned char[] array, and need to convert to const void* pointer.

View 3 Replies View Related

C++ :: Object That Contains 2 Types Of Data

May 24, 2014

How do you create an object (like in the title) something more simple than a struct? I wanna know that cuz I'm writing a function that could return a boolean and an integer at the same time.

View 2 Replies View Related

C++ :: Add Data To Text Files Which Are Required To Store 3D Scan Data

Jul 10, 2013

I have written the following code to add data to text files which are required to store 3D scan data (I have to calculate the y-coordinate). My code seems to work except that it stops working when I want to create more than ten text files i.e. the directory I am trying to store them in will not hold any more than ten text files. Code is shown below.

#include <string>
#include <locale>
#include <iomanip>
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

[Code] ....

View 1 Replies View Related

C++ :: What Are The Implications Of Declaring Data Types But Never Using Them

Mar 26, 2013

What are the possible problems if I declare a bunch of data types and never use them? Do they take up a lot of memory? Will they slow run time? If it is an array do I have to delete it at the end of the program? What if the array is defined inside a class and never used? Do I still have to delete it?

i.e.

Code: class declarearrays{
public:
double **darray;
double **darray2;
void function1();//function that initializes darray
void function2();//function that initializes darray2 with different parameters, may not be used.
};

View 1 Replies View Related

C++ :: Map Storing Multiple Data Types

Jul 11, 2014

I am trying to create a generic map container which shell contain data of different datatypes. I already found a solution in this forum here:

[URL]...

Introducing a container with a Base Class as content type and inserting objectes of Derived Class types from that Base Class suites my implementation very well. But it is not really working. I implemented it this way:

class MyType {
public:
MyType() {}
virtual ~MyType() {}
}; template <class PT> class ParseType : public MyType

[Code]...

Then I insert one element.

// index being an object of type Parser<string>
ParseType<string>* test = new ParseType<string>( index );
// and index.val(0) = "-n"
iMap.insert( pair< string, MyType* >( index.id(0), test ) );

Now I think I should be able to call

const string key("-n");
iMap.at(key)->content->val(n);
Or
iMap.at(key)->get_val(n);

But neither one compiles with the error that "class MyType" (as the map container is pointing to a MyClass object) has no member/member function "content"/"get_val".

I also tried introducing member and member function "content" and "get_val" in "class MyType", which I thought should be hidden while constructing ParseType<string>. This would compile but it does not call the member "content or member function "get_val" from class ParseType.

A second try was to remove templatization of "class ParseType" and introduce a specific, let's say, "class ParseString" for objects of type Parser<string>. But the problems remain the same either the compiler complains due to missing member/member function or retreiving the map content will not call the derived class member/member function.

View 4 Replies View Related

C++ :: Handling Data Types As Inputs

Apr 10, 2013

I'm fairly new to C++ programming. I wanted to accept a datatype as an input. I've seen something similar while working with the <queue> library.

When defining a new object of queue

For example:

queue<int> Queue;

How is <int> handled ??

View 1 Replies View Related

C++ :: Accept Multiple Data Types

Jun 17, 2014

Basically I do not want to use a menu, instead just accept either an float or a single character. Then send the data to the appropriate spot based on the user input. I have been unable to convert the char to a float, and even if I did the char would probably only accept the first digit, say user enters '15' it would only read the '1'. I've tried strings instead of char but then unable to use the isalpha function. Do I need a char[] and then iterate through to get the numeric data? Then how do i make '1' and '5' become 15. There is probably a solution. I've also tried to use a loop waiting for the correct data while(!(cin >> letter)) which works but how do I get out if the user enters number.

#include <iostream>
#include <cctype>
#include <string>
#include <cstdlib>
using namespace std;

[code].....

View 3 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved