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!

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

cin >> n; // prints everything

cout << n<<endl;

return 0;

}``````

is it possible to put the float which is entered into an array? that way i can chose what is printed out on the screen?

... unless anyone else has other ideas which i welcome :D

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; …``````

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

``````float value; // 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 …

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 …

>I'm wondering if theres a away to just print out the values after the decimal point.
There's a library function in <cmath> 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 …

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 …

## All 23 Replies

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;
int i;
for ( i = 0; i < 3; ++i )
{
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
+
-
+
*/``````

float value;
int i;
for ( i = 0; i < 3; ++i )
{
cin >> value; // put user-entered value into an array element
if ( value < 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

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

``````float value; // 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 )``

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

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

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 ;)

>I'm wondering if theres a away to just print out the values after the decimal point.
There's a library function in <cmath> 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.

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... :)

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?

>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;
}``````

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?

>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, b;
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;
}``````

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...

``````char thesting;
cin << the string

char *value
*value = ( thestring)``````

not sure if that works though

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

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:

``````char string;
const char *source ;

source = &string;

cin >> string;

char a, b;
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;``````

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;     // Make it a string
const char *source ;

source = string;     // & is no longer needed``````

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

>could anyone recommend any?
Clikey.

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; and change it to an string.... just googling that now

>if ( string[n] < 0 )
This test is wrong because a printable character will not be less than 0. What are you trying to do to make the code "more powerful"?

basically if a user enteres a - followed by the numbers then the statment will look for the sign and says that the sign is a -.

if its not a minus then the number enetered must be a plus

Well, in that case you need to test for '-':

``````if (string == '-')
cout<<"Negative value"<<endl;
else
cout<<"Positive value"<<endl;``````

basically if a user enteres a - followed by the numbers then the statment will look for the sign and says that the sign is a -.

if its not a minus then the number enetered must be a plus

so let the user enter a number!!!!

``````float x;

cin >> x;

if(x < 0)
cout << "You entered a negative number\n";
else
cout << "You entered a positive number\n";

cout << "The real part: " << int(x) <<"\n";
cout << "The fractional part: " << float(x - int(x)) << "\n";``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.