| | |
Floating point numbers
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
>just wondering if you can split up a string of numbers?
Of course:
Of course:
C++ Syntax (Toggle Plain Text)
#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; }
New members chased away this month: 4
>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:
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:
C++ Syntax (Toggle Plain Text)
#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; }
New members chased away this month: 4
•
•
Join Date: Dec 2004
Posts: 12
Reputation:
Solved Threads: 0
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
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
•
•
Join Date: Dec 2004
Posts: 12
Reputation:
Solved Threads: 0
•
•
•
•
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]
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:
becomes
C++ Syntax (Toggle Plain Text)
char string; const char *source ; source = &string;
C++ Syntax (Toggle Plain Text)
char string[8]; // Make it a string const char *source ; source = string; // & is no longer needed
New members chased away this month: 4
•
•
Join Date: Dec 2004
Posts: 12
Reputation:
Solved Threads: 0
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
could anyone recommend any?
Naure >> Thanks for the code and assiatnce along with all the rest who posted...really appricated
•
•
Join Date: Dec 2004
Posts: 12
Reputation:
Solved Threads: 0
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
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
![]() |
Similar Threads
- Floating point numbers (Assembly)
- Assembly floating point (Assembly)
- Fixed to Floating point conversions (C)
- Floating point numbers (C)
- Dynamic Array, Writing to CSV, Floating Point ?'s (C)
- IEEE Floating-point fromat (C)
Other Threads in the C++ Forum
- Previous Thread: memory
- Next Thread: Borland C++ vr 4.5
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamiccharacterarray email encryption error file format forms fstream function functions game givemetehcodez graph homeworkhelp iamthwee ifstream input int java lib library lines list loop looping loops map math matrix memory newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg search simple sorting spoonfeeding string strings struct temperature template templates text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






