1

I have a program like this. And I tried to cout the data a[1] but it does not work even though I can still cout when using for loop. Help me fix this please, thanks. And my vectors have is not null. I don't know why. Here is my code:

#include <bits/stdc++.h>
using namespace std;
string s, s1, s2;
vector <string> a;
bool cmp(string x, string y){
        if(x.size()!=y.size()) return x.size() < y.size();
        else{
            for(int i=0; i<x.size(); i++){
                if(x[i]!=y[i]) return x[i]<y[i];
            }
        }

}
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    cin >> s;
    s=s+' ';
    int i=0;
    while(i<s.length()){
        if(isdigit(s[i])){
            //a.push_back(s2);
            //s2="";
            s1+=s[i];
            i++;
        }
        else{
            //s2+=s[i];
            a.push_back(s1);
            s1="";
            i++;
        }
    }
    ;
    sort(a.begin(), a.end(), cmp);
    cout << a[1];
    //for(int x=0; x<a.size(); x++) cout << a[x];
    //int j = 0;
    cout << s[0];
    for(int x=1; x<s.size()-1; x++){
        /*if(isdigit(s[x])&&isdigit(s[x-1])==false){
            cout << a.front();
        }*/
        if(isdigit(s[x])==false) {cout << s[x];}
    }

    return 0;
}

  • 3
    Side notes: (1) [#include ](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) (2) [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – wohlstad Mar 20 '23 at 14:32
  • Please [edit] and show a simple example of input and expected output. – Jabberwocky Mar 20 '23 at 14:47
  • 2
    `#include ` --> No. These are the headers: `#include ` -- `#include ` -- `#include ` -- `#include ` -- `#include `. Get used to typing these names in, as doing this will be a way to learn by remembering the headers required to get a program to compile. That `bits` header teaches nothing except lazy programming (and not only that, the header is not standard C++). – PaulMcKenzie Mar 20 '23 at 14:50
  • Looks like you may be learning C++ from competition sites. The goal of code written for competitions is to win. It doesn't have to look nice, play nice or follow good coding standard. It's not meant to be read by anything but a compiler, not intended to be run more than once under ideal circumstances, and is most definitely not intended for use as educational materials. You could learn C++ this way, but it would be a very slow process. I strongly recommend learning from [a few good books](https://stackoverflow.com/q/388242/4581301) and perhaps competing afterward. – user4581301 Mar 20 '23 at 15:05
  • You should add flags `-Wall -Wextra -Werror` to your compiler. In debug mode you should add also `-fsanitize=address,undefined`. This way compiler will catch 99% of errors made by beginners. – Marek R Mar 20 '23 at 16:29

1 Answers1

3

You forgot to return something from your cmp() function when the two strings have the same length and no corresponding elements differ.

bool cmp(string x, string y){
        if(x.size()!=y.size()) return x.size() < y.size();
        else{
            for(int i=0; i<x.size(); i++){
                if(x[i]!=y[i]) return x[i]<y[i];
            }
        }

        return false; // <-- add this
}

Also check your input, keeping in mind that >> operator with cin stops reading at a whitespace. For example, you can give data separated with commas like 3,2,1,0.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
  • Thanks. However, it still not works when I cout a[1] – Hà Anh Tiến Mar 20 '23 at 14:34
  • @HàAnhTiến [It looks working for me.](https://wandbox.org/permlink/LIv7z5OtceqBvxay) – MikeCAT Mar 20 '23 at 14:35
  • Same for me. I use this command for(int x=0; x – Hà Anh Tiến Mar 20 '23 at 14:36
  • @HàAnhTiến *Thanks. However, it still not works when I cout a[1]* -- The original code you posted had the error that was pointed out in this answer. Your original code had undefined behavior to begin with. Any further issues, you should open a different question with the updated code. – PaulMcKenzie Mar 20 '23 at 14:47
  • 1
    @HàAnhTiến having multiple bugs in the code is unpleasant and makes debugging much harder. For example, I've seen programmers discard correct solutions for problem A because it did not also solve problem B. To make sure a bug is squished, you need to isolate the mistake from any other mistakes. One of the best ways to do that is to write small amounts of code, just the few lines needed to accomplish one, easily testable task, and then prove those few lines work before adding more code. This sounds slow, but debugging takes much longer than writing, so you almost always come out ahead. – user4581301 Mar 20 '23 at 15:01