String Program Help **URGENT**

Hi
I need help breaking up a string of numbers into an ascending order and deletes extraneous numbers.

Ex.
Input: 913457
Output should be: 9 13 45
(7 is deleted)

Thanks for ur help

This is what i have so far...
The last loop that breaks up the string doesn't work.
#include <iostream>
#include <iomanip>
#include <stdlib.h>
#include <string>

using namespace std;

int main() {

string num, num1, num2, nume;
int numb2, numb1;
int nume1;
int x, y, z;
int a=0;

cout<<"Enter a string of numbers: ";
getline(cin,num);

x = -1;

num1 = num.substr(0,1);

numb1 = atoi(num1.c_str());

cout<<numb1<<endl;

num2 = num.substr(1,z-1);

numb2 = atoi(num2.c_str());

cout<<numb2<<endl;

z = num2.length();

y=3;

if(num1[0]<=num2[0]){

num1.insert(1," ");
cout<<num1;
}

for(int i=0; i<z; i++){

if(num2[x++]<num2[a++]){

cout<<num2[x]<<" ";

}

else if(num2[x++]>=num2[a++]){

nume=num2[a]+num2[a+1];

nume1=atoi(nume.c_str());

cout<<nume1<<" ";

}
}

return 0;
}
Last edited on
You haven't explained the algorithm for splitting up the input string.

Why is the expected output "9 13 45" and not "9 1 3 4 5 7", or "91 34 57", or some other combination?

How are we supposed to know if your code is correct if we don't know what it is supposed to do?

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
@AbstractionAnon Oh, the purpose is that if the following number(s) is/are greater than the previous number there is a space between them. And then the extra numbers at the end would be deleted. But the first number of the input is always going to be by itself.
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
#include <iostream>
#include <string>
#include <sstream>

int main()
{
    std::cout << "enter a sequence of integers separated by spaces:\n" ;
    std::string input ;
    std::getline( std::cin, input ) ;

    std::istringstream stm(input) ; // input string stream that reads from the the string

    int last_number_added ; // the last number added to the ascending sequence

    stm >> last_number_added ; // read in the first number
    // start the ascending sequence with this number
    std::string ascending_seq = std::to_string(last_number_added) ;

    int current ; // the number that was currently read
    while( stm >> current ) // for each number read from the input stream
    {
        // if this number is greater than the last number added to the ascending sequence
        if( current > last_number_added )
        {
            ascending_seq += ' ' + std::to_string(current) ; // append it to the result
            last_number_added = current ; // this is now the number that was last appended
        }
    }

    // if the entire input was consumed, print out the ascending sequence
    if( stm.eof() ) std::cout << ascending_seq << '\n' ;
    else std::cerr << "badly formed input\n" ;
}
This has no error checking on the input.
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
#include <iostream>
#include <string>

using std::cin;
using std::cout;
using std::string;
int
main()
{
    string str;
    while (getline(cin, str)) {
	int highestNum = str[0]-'0';
	int cur= 0;		// current number
	cout << str << ": " << highestNum;

	// For each digit after the first
	for (size_t i = 1; i<str.size(); ++i) {
	    cur = cur * 10 + str[i] - '0'; // add next digit to cur
	    if (cur > highestNum) {
		cout << ' ' << cur;
		highestNum = cur;
		cur = 0;
	    }
	}
	cout << '\n';
    }
    return 0;
}

PLEASE -- do not do homework assignments for people. There are better ways to show off how smart you are.
I don't think the poster is going to hand in one of the given solutions. They have tried to create some code of their own and I hope that they will be able to translate some of the good ideas from the solutions into their own code. A good teacher would call on them to explain anyway.

I have to confess that I didn't understand the original problem until I read @dhayden's solution, so I'm quite relieved somebody managed to clarify it!

PanGalactic, I generally agree with you, but nobody ever said this was homework. I mostly posted my solution because I had a different interpretation of the problem from JLBorges.

LastChance, thanks but I'm not sure if I've interpreted the problem correctly either ;).
Topic archived. No new replies allowed.