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);
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.
#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" ;
}
#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;
}
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 ;).