Floating point numbers

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 7,847
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Floating point numbers

 
0
  #11
Jan 1st, 2005
>just wondering if you can split up a string of numbers?
Of course:
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. string source = "123.456";
  9. string a, b;
  10.  
  11. a = source.substr(0, source.find('.'));
  12. b = source.substr(source.find('.') + 1);
  13.  
  14. cout<< a <<'\n'<< b <<endl;
  15. }
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 12
Reputation: happyHour is an unknown quantity at this point 
Solved Threads: 0
happyHour happyHour is offline Offline
Newbie Poster

Re: Floating point numbers

 
0
  #12
Jan 1st, 2005
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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,847
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Floating point numbers

 
0
  #13
Jan 1st, 2005
>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:
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. const char *source = "123.456";
  8. char a[4], b[4];
  9. int i = 0, j;
  10.  
  11. for (j = 0; j < 3 && source[i] != '.'; j++)
  12. a[j] = source[i++];
  13. a[j] = '\0';
  14. ++i;
  15. for (j = 0; j < 3 && source[i] != '\0'; j++)
  16. b[j] = source[i++];
  17. b[j] = '\0';
  18.  
  19. cout<< a <<'\n'<< b <<endl;
  20. }
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 12
Reputation: happyHour is an unknown quantity at this point 
Solved Threads: 0
happyHour happyHour is offline Offline
Newbie Poster

Re: Floating point numbers

 
0
  #14
Jan 1st, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: Floating point numbers

 
0
  #15
Jan 1st, 2005
if it doesnt try value = &thestring. that is the way to reference a pointer
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 12
Reputation: happyHour is an unknown quantity at this point 
Solved Threads: 0
happyHour happyHour is offline Offline
Newbie Poster

Re: Floating point numbers

 
0
  #16
Jan 1st, 2005
Originally Posted by 1o0oBhP
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 <<endl;

return 0;
[/php]
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,847
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Floating point numbers

 
0
  #17
Jan 1st, 2005
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:
  1. char string;
  2. const char *source ;
  3.  
  4. source = &string;
becomes
  1. char string[8]; // Make it a string
  2. const char *source ;
  3.  
  4. source = string; // & is no longer needed
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 12
Reputation: happyHour is an unknown quantity at this point 
Solved Threads: 0
happyHour happyHour is offline Offline
Newbie Poster

Re: Floating point numbers

 
0
  #18
Jan 1st, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,847
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 753
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Floating point numbers

 
0
  #19
Jan 1st, 2005
>could anyone recommend any?
Clikey.
New members chased away this month: 4
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 12
Reputation: happyHour is an unknown quantity at this point 
Solved Threads: 0
happyHour happyHour is offline Offline
Newbie Poster

Re: Floating point numbers

 
0
  #20
Jan 1st, 2005
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC