954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Floating point numbers

i got my program to do stuff, but i wondered if i can take it to the next level...

when a user enters a number of 1458.125478 i got it to do abs.... google is soo good!! but now im wondering if you could display just + or - depending on what is entered. I tried creating an int for the sign to go into but it just doesnt want to know!

then i thought about a string so i went down that route not knowing that you cant combine a string and a float together!

so i had another idea

[php]

int main(){
float n; //n is the number entered

cout << " please input your number with the sign";
cin >> n; // prints everything

cout << n<

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 
but now im wondering if you could display just + or - depending on what is entered.

If it's less than zero, print'-'; otherwise, print '+'.

is it possible to put the float which is entered into an array?

Of course.

#include <iostream>

int main()
{
   float value[3];
   int i;
   for ( i = 0; i < 3; ++i )
   {
      std::cout << "Please input your number with the sign: ";
      std::cin  >> value[i]; // put user-entered value into an array element
      if ( value[i] < 0 )
      {
         std::cout << "-" << '\n';
      }
      else
      {
         std::cout << "+" << '\n';
      }
   }
   return 0;
}

/* my input/output
Please input your number with the sign: 1458.125478
+
Please input your number with the sign: -2.3
-
Please input your number with the sign: 0
+
*/
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

float value[3];
int i;
for ( i = 0; i < 3; ++i )
{
cout << "Please input your number with the sign: ";
cin >> value[i]; // put user-entered value into an array element
if ( value[i] < 0 )

could you explain that bit a little more so i can try and follow whats going on. From my interpration

you got a float thats an array - true?
you print out the array using the loop i

but one thing i dont get is how do you stop it printing the rest out ? just the sign

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

>could you explain that bit a little more so i can try and follow whats going on.

float value[3]; // an array of 3 floats
   int i; // a loop counter
   for ( i = 0; i < 3; ++i ) // loop 3 times


I can't imagine you're having trouble with that.

cin >> value[i]; // put user-entered value into an array element


Is this comment confusing?

>you print out the array using the loop i

No. I print a plus or minus. I do so depending on whether the value entered is less than zero.

if ( value[i] < 0 )
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

ok im going back to my book! I should of known how to do that and i cant belive it was that easy... :(

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

Intresting topic we have here, I'm wondering if theres a away to just print out the values after the decimal point.

This is what I thought -

If you entered it into a string you'd have some for loop that finds the decimal point and then from their print everything out google has been unsucessful at finding any information regarding this matter

happyHour
Newbie Poster
12 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

Intresting topic we have here, I'm wondering if theres a away to just print out the values after the decimal point.

This is what I thought -

If you entered it into a string you'd have some for loop that finds the decimal point and then from their print everything out google has been unsucessful at finding any information regarding this matter

crude but effective idea would be to change the string to an int. Why well with my little depth of c++ itsall i can think of ... But think about it for a moment you convert it to an int ... What can an int hold?? 4 bytes of real numbers... Whole real numbers! so in feect you'd loose your decimal point. All you have to do then is to take away the number left in the string with the one you created - Hence leaving you with the decimal parts.. .Note that you won't get the 0. xxxxxx Not sure how that works ...

Nor could i code this without research , have a go if your still struggling the forum is always here ;)

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

>I'm wondering if theres a away to just print out the values after the decimal point.
There's a library function in called modf that breaks up a floating-point value into its constituent pieces. Alternatively, you could copy the value to an int (which truncates the fractional part) and subtract that from the original floating-point value. Either way, the result is the fractional part of the floating-point value, and you still have access to the whole part.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

I have a feeling that in the iostream classes there is a formatting option to set the precision and decimal places. I have never used them myself so i couldnt tell you what they were but a google search should reveal it.

I think it was something along the lines of

cout.flags( options ... )
or
cout << options... << text


If you cant find it try searching for a hexadecimal number tutorial as hex is a formatting flag which tells cout to display numbers in hexadecimal.... as far as i remember... :)

1o0oBhP
Posting Pro in Training
445 posts since Dec 2004
Reputation Points: 16
Solved Threads: 6
 

Thanks for the replies, just wondering if you can split up a string of numbers? ... say all the numbers before the decimal point...into string1
and all the others after the decimal point into string 2?

happyHour
Newbie Poster
12 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

>just wondering if you can split up a string of numbers?
Of course:

#include <iostream>
#include <string>

using namespace std;

int main()
{
  string source = "123.456";
  string a, b;

  a = source.substr(0, source.find('.'));
  b = source.substr(source.find('.') + 1);

  cout<< a <<'\n'<< b <<endl;
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

umm, we havent been taught that way, is they not a for loop i could use to tell it to stop... ie

for (int i = 0; i !='.'; i++ )

i know that does work but something amounst those lines?

happyHour
Newbie Poster
12 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

>umm, we havent been taught that way
So!? There are plenty of ways to do what you want that you probably haven't been taught. I'm here to teach, so what's the problem? If you don't try to broaden your horizons you'll never get anywhere, and if you stick to what you've been taught then you'll be writing C++ with the least useful (and flexible, and powerful) subset of features imaginable.

>i know that does work but something amounst those lines?
Yes, it does work. But since you don't want to learn anything new, you might as well use it:

#include <iostream>

using namespace std;

int main()
{
  const char *source = "123.456";
  char a[4], b[4];
  int i = 0, j;

  for (j = 0; j < 3 && source[i] != '.'; j++)
    a[j] = source[i++];
  a[j] = '\0';
  ++i;
  for (j = 0; j < 3 && source[i] != '\0'; j++)
    b[j] = source[i++];
  b[j] = '\0';

  cout<< a <<'\n'<< b <<endl;
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Cool, that looks good, but if a user wanted to enter data into the array, since you've used a const pointer to an array of characters it wouldnt work would it? but upon taking out the const and adding cin >> *source that still doesnt work... I'm back in the text book looking for a visual aid...

here what i cam up with on the top of my head...

[php]

char thesting[8];
cin << the string


char *value
*value = ( thestring)

[/php]

not sure if that works though

happyHour
Newbie Poster
12 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

if it doesnt try value = &thestring. that is the way to reference a pointer

1o0oBhP
Posting Pro in Training
445 posts since Dec 2004
Reputation Points: 16
Solved Threads: 6
 
if it doesnt try value = &thestring. that is the way to reference a pointer


I get a load of garbage which is already in memory with this code:

[php]
char string;
const char *source ;

source = &string;

cin >> string;

char a[4], b[4];
int i = 0, j;

for (j = 0; j < 3 && source[i] != '.'; j++)
a[j] = source[i++];
a[j] = '\0';
++i;
for (j = 0; j < 3 && source[i] != '\0'; j++)
b[j] = source[i++];
b[j] = '\0';

cout<< a <<'\n'<<"0."<< b <

happyHour
Newbie Poster
12 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

string isn't a string, it's a character. Because a C-style string is required to be terminated by '\0', and cin's >> operator won't add that when extracting to a character, your loop will go until it finds one. The end result is that you're invoking undefined behavior by accessing memory you don't own. The solution is a quick and simple change:

char string; 
const char *source ; 

source = &string;

becomes

char string[8];     // Make it a string
const char *source ; 

source = string;     // & is no longer needed
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

intresting, is they anyway i can deeper understand these pointers? I get everything else i've been taught but hate to use pointers since i don't know how to use them... A nice tutorial with examples would be nice to read...

could anyone recommend any?

Naure >> Thanks for the code and assiatnce along with all the rest who posted...really appricated :D

happyHour
Newbie Poster
12 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

>could anyone recommend any?
Clikey .

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

upon trying to make the code more powerful ....

int n = 0;
if ( string[n] < 0 )
{
cout << "The sign is: -" << '\n';
}
else
{
cout << "The sign is: +" << '\n';
}

i included that however fo some reason its always a +!! so i thought if we could keep this float value[3]; and change it to an string.... just googling that now

happyHour
Newbie Poster
12 posts since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You