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
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>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
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
>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
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>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
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>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
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
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
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>could anyone recommend any?
Clikey .
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401