Homework Assignement. Please, help

Hi, i am new to c++ programming , i have to write a program for homework. It has to include a function with a prototype int max_min9(int n). n is a three-digit number. The function`s aim is to return the value of mx - mn. mx is the largest integer that can be formed using
the digits of n and mn is the smallest integer that can be formed using the digits of n. I think I am having problems with the conversion from string to int. Here is my code. Where is my mistake?

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
41
42
43
44
45
46
47
48
49
50
#include<iostream>
#include<cmath>
#include<string>
#include<sstream>
using namespace std;

int max_min9(int n)
{
	
	
	int a, b, c, d, e;
	
	c = n%10;
	n = n/10;
	b = n%10;
	n = n/10;
	a = a%10;
	int w = (a + b + c) - (d + e);
	
	
	
	
        string s1 =  s1 + char(int(max(max(a,b), c)) + '0') +  char(w + '0') + char(int(min(min(a, b), c)) + '0' );
	string s2 =  s2 + char(int(min(min(a, b), c)) + '0' ) +  char(w + '0') + char(int(max(max(a,b), c)) + '0');
	int result1;
	int result2;
	stringstream(s1) >> result1;
	stringstream(s2) >> result2;
	return(result1 - result2); 


	
	
	
	
	
}

int main()
{
	int n;
	cout << "Enter a number:";
	cin >> n;
	
	
	cout << max_min9(n);
	return 0;

}
  
Last edited on
What if you would go directly into a string, and sort it both ascending and descending?
Topic archived. No new replies allowed.