Maximum int to string

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
#include <string>
#include <list>
#include <iostream>

using namespace std;


string maximum(list<int> nums)
{
    list<int>::iterator pos;
    pos = nums.begin();
    int m;
    if (pos != nums.end())
    {
        m = (*pos);
        while (pos != nums.end())
        {
            if (m < (*pos))
                m = (*pos);
            pos++;
        }
    }
    return m;
}

int main()
{
    list<int> nums;
    nums.push_back(2);
    nums.push_back(1);
    nums.push_back(3);
    nums.push_back(0);
    nums.push_back(2);
    
    cout << "The maximum element in the list is " << maximum(nums) 
    << "\n";
    
    return 0;
}


Line 23 is giving me a conversion problem and I am not understanding how to fix, can anyone please help me? Thank you in advance.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
string maximum(list<int> nums)
{
    list<int>::iterator pos;
    pos = nums.begin();
    int m;
    if (pos != nums.end())
    {
        m = (*pos);
        while (pos != nums.end())
        {
            if (m < (*pos))
                m = (*pos);
            pos++;
        }
    }
    return m;
}


Note the int variable you are creating?

Return type must be same as in the "prototype". This time it is string instead of int.

in other words: if you create function to return int you do it.
so changing string maximum to int maximum fixed it I believe. Was that the correct way to fix it?
Depends, did you want your function to return an int, or a string? If you wanted an int, then yes, that was the proper way to fix it, if not, you need to make your function a string again, and figure out what you want to return as a string.
Thank you for the help guys.
Yes, that was the correct way.

The compiler does not know how to convert m into a string to return from the function, because there is no way defined to directly assign an integer to a string -- they are two different things.

1
2
3
4
5
6
string s = "72";
int n = 12;

// doesn't work:
s = n;        // can't assign int to string
n = s;        // can't assign string to int 


cout takes the int returned from the function and converts it into a string for you automatically. That's because the STL stream classes (like cout) are specifically designed to transform things to and from strings.

1
2
3
4
5
6
7
8
9
string s = "72";
int n = 12;

// works fine:
cout << "my favorite number is "       << s << endl;        // (1)
cout << "my other favorite number is " << n << endl;        // (2)

// (1) s is already a string, prints "my favorite number is 72"
// (2) n is automatically converted to a string, prints "my other favorite number is 12" 

Hope this helps.
If you're using a modern C++ compiler*, supporting the C++11 standard, there's std::to_string(int) which you can use.

http://en.cppreference.com/w/cpp/string/basic_string/to_string

* GCC 4.6+, MSVC 10+ etc.
Technically, 4.6 doesn't support C++11, only C++0x. However, most of the standards in C++11 are in 4.6. Check out gcc's website for information on what's supported with each version:
http://gcc.gnu.org/projects/cxx0x.html

I don't know much about MSVC++, but I did read that MSVC10 still only supports C++0x. I know, what's the difference? But with so much added into the new standard, saying that C++0x and C++11 are the same isn't 100% true. A few compilers implement C++0x but don't have all of the same features as each other. But C++11 support is right around the corner for everyone. I personally am waiting on MinGW to release 4.7
I personally am waiting on MinGW to release 4.7

There ya go baby, there ya go (Sarah Palin reference, so don't get offended):
http://nuwen.net/mingw.html
No offense taken. But I think you're my second favorite person on this forum right now.
Volatile Pulse wrote:
I don't know much about MSVC++, but I did read that MSVC10 still only supports C++0x. I know, what's the difference? But with so much added into the new standard, saying that C++0x and C++11 are the same isn't 100% true. A few compilers implement C++0x but don't have all of the same features as each other.
C++0x was the name used to refer to the new standard when it was not finished. They didn't know what year it would be finished so they used an x to be replaced when the year was known. It took more time than expected so the 0 also had to change in the end. In GCC 4.7 the flag -std=c++0x is a synonym for -std=c++11. No compiler, as far as I know, support all of C++11 yet.
Last edited on
Topic archived. No new replies allowed.