Store int to char

How to store int/double value in a char?
Do you actually mean a single char? Or do you mean a C-style string, i.e. an array of chars?
Example input values:
0.5 is the input value for P1
1 is the input value for P2
0.3 is the input value for P3

//Sort ascending order
My Program Output:
0.3
0.5
1

What I need to output is:
P3
P1
P2

How would I do it? Thanks in advance.
Last edited on
Characters essentially are integers. You could easily assigned an integer value to a char, assuming they're the same size. This can get a little hazy between different compilers and system architectures.

1
2
int my_int = 12345;
char32_t my_char = my_int;


Why you'd want to is another matter.

I'd imagine the same thing would happen to a double, providing the char was big enough. Though I think anything after the decimal point would be truncated.

Again, I can't see why you'd really want to do something like this.

Edit: This may be outdated. I was typed this before the above replied appeared.
Last edited on
I need to sort it in ascending order by its value however the variable of the value must be the output just like my example above.
You could just store it in a double and output like that anyway? Does the same thing?

EDIT:
Also, storing in a string would give problems too, such as 10 coming before 2.
Last edited on
From the description you've given, I don't see why you need to store anything in a char. You just need to read the user input as a number, sort the numbers, and then output the numbers.

Have a look at this turorial:

http://www.cplusplus.com/doc/tutorial/basic_io/
Oh, I see what you mean now. You want to associated the value with its ID. You could usually use a std::map for that sort of associating, but it can get a little messy to sort (maps by their nature are sorted by keys, not values).

You could create a struct and keep a vector of that struct, adding the appropriate elements as they're entered.

1
2
3
4
5
6
7
struct Foo
{
   std::string id;
   double value;
};

std::vector<Foo> my_foos;


Alternatively, you could use a vector of pairs, like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>

bool myComp( std::pair<std::string, double> lhs, std::pair<std::string, double> rhs ) 
{ 
    return lhs.second < rhs.second; 
}

int main( int argc, char* argv[] )
{
    std::vector<std::pair<std::string, double>> input_map;
    unsigned int count = 1;
    double input;

    std::cout << "Enter number: (Enter 0 to stop)";

    while( std::cin >> input && input != 0 )
    {
        std::cin.ignore( 256, '\n' );
        std::cin.clear();

        std::ostringstream oss;
        oss << "P" << count;

        input_map.push_back( std::pair<std::string, double>( oss.str(), input ) );
        std::cout << "Enter next number: ";

        ++count;
    }

    std::sort( input_map.begin(), input_map.end(), myComp );

    std::cout << "Ascending order:\n";

    for( const auto &i : input_map )
        std::cout << i.first << " : " << i.second << std::endl;
}
Topic archived. No new replies allowed.