2

I need to make a program where a user inputs a string of numbers.
It then outputs the numbers grouped so that each grouping, separated by a space, is larger than the last when you put the numbers together.
If the remaining digits together equal a sum smaller than the previous grouping they are omitted.
Ex.:
314159265 would output 3 14 15 92
Here: 3 < 14 <15 < 92 but 65 was omitted because it is less than 92

or
9876543210 would output 9 87 654 3210
Here: 9 < 87 < 654 < 3210

It has to do this 5 times with strings 1-20 characters long.

My code works for shorter strings, ie the ones above, but when they are longer than around 12 characters the end output messes up.

Ex.: 98765432101234567898 outputs 9 87 654 3211 12333 instead of 9 81 654 3210 12345 67898 Here: 9 < 87 < 654 < 3211 < 12333 it should output 9 < 87 < 654 < 3210 < 12345 < 67898

I have no idea why it doesn't work with larger strings and any help would be greatly appreciated.

#include<iostream>
#include<iomanip>
#include<math.h>
#include<string>
#include<stdlib.h>

using namespace std;

void input (string &a,string num[20]){
    string numfinal,temp;
    cout<<"Enter the string of numbers: ";
    getline(cin, a);
    int length=a.length();
    for(int i=0;i<length;i++){
        num[i]=a.substr(i,1);
    }

    for(int r=0;r<length;r++){
        int n=atoi(temp.c_str());
        int o=atoi(num[r].c_str());
        int p=temp.length();
        if((length-r<=p)&&(o<n)){
        }
        else if((o>n)||(r==0)){
            temp=num[r];
            numfinal=numfinal+temp+" ";
        }
        else if((o<n)||(o=n)){
            int w=n;
            temp=num[r]+num[r+1];
            n=atoi(temp.c_str());
            if(n<w){
                int a=1;
                int q=r+2;
                while(n<w){
                    temp=temp+num[q];
                    n=atoi(temp.c_str());
                    p++;
                    a++;
                }
                numfinal=numfinal+temp+" ";
                r=r+a;
            }
            else{
                numfinal=numfinal+temp+" ";
                r++;
            }
        }
    }
    cout<<numfinal<<endl;
}

int main(){
    string a;
    string num[20];
    for(int r=0;r<5;r++){
        input(a,num);
    }
    return 0;
}
4
  • Is it possible that the first example, 314159265, could produce the output: 31 41 59 265? It seems like there could be many outputs for a single input. And if so, how would you choose which is the 'correct' output? Commented Feb 8, 2017 at 21:47
  • atoi has a number of failure cases you are not checking. stoi or strtol may be better choices. Commented Feb 8, 2017 at 21:53
  • @Obicere The first digit is supposed to stand by itself. Sorry i didn't include that. Commented Feb 8, 2017 at 21:57
  • I tried 3141592653589793238 no zeroes and it still screwed up the end, outputting 3 14 15 92 653 5899 9322 . I also tried stoi and it gives the "Not declared in this scope" error code Commented Feb 8, 2017 at 22:06

2 Answers 2

1

This code works. But do not forget that write a simple code and use the new style of C++ programming.

#include <vector>
using namespace std;
vector<string>  Input( )
{
    string a;
    cin >> a;
    vector<string> num;
    string current("-1");
    string str;
    for(auto c : a)
    {
        str.append(1, c);
        if (stoi(str) > stoi(current) )
        {
            num.push_back(str);
            current = str;
            cout << str << " ";
            str = "";
        }
    }
    cout << endl;
    return num;
}

int main() {
    for (int i = 0; i<5; i++) {
        vector<string> num;
        num = Input();
    }
    return 0;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the help but I have to use old c++ and can't really follow what's happening in this code, I don't know what auto is or push_back(str) does.
You can use a simple array instead of vector and use char instead of auto. Easy!!!
0

Looks like you are missing a q++ in your while loop:

while(n < w) {
   temp = temp + num[q];
   n = atoi(temp.c_str());
   p++;
   a++;
   q++;
}

This is why the fourth third digit was being copied over.

The maximal munch algorithm might give you some inspiration on a cleaner way to solve this problem. It is meant for tokenizing but it may help. Good luck!

4 Comments

Thank you, the q++ helped, now it works fiine until the strings are over 6 groupings long, then it just doesnt output any thing over the sixth grouping.
Yes, it seg faults because your num array is too small. Increasing the array size is a quick and dirty fix but do consider using vectors to avoid this issue. Also, just a suggestion: using more descriptive variable names will aid both you and anyone in debugging.
Sorry, but I'm not following. I didn't make a num array it is a string? Increasing the string size will have no effect.
Well actually it is an array of strings (which probably isn't necessary since you could just use a single string). As I said, a quick fix would be to just increase the size (I bumped it to 40 to check), but this really isn't a good solution. See Sam's solution for a well thought out method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.