2

I am trying to limit user input into alphabet only, then sort all the character in ascending order.

build messages error: no matching function for call to 'std::__cxx11::basic_string::basic_string(char&)'

This is my header

#include <iostream>
#include <string.h>
#include <conio.h>
#include <stdio.h>
#include <regex>

should i convert the char into string then convert back to char for my following code ?

string Sortstr (str[mlength]);
sort(Sortstr.begin(), Sortstr.end());

getting this 2 line error.

int mlength = 100;
int main() {
    char str[mlength];
    int length;

    cout << "Please enter a c-string: ";
    cin.getline(str,mlength,'\n');
    regex pass1("^[a-zA-Z]+");
    while(!regex_match(str,pass1)) {
         cout<<"Error"<<endl;
         cout << "Please enter a c-string: ";
         cin.getline(str,mlength,'\n');
    }
    string Sortstr (str);
    sort(str, str + strlen(str));
}
7
  • string Sortstr (str[mlength]); is undefined behavior, due to indexing the array out of bounds, not to mention, that it is non-standard C++ to use VLAs )variable length arrays) in the first place (mlength is not constexpr). Commented Sep 19, 2018 at 9:54
  • 1
    Why are you using a char array to begin with instead of a std::string? Commented Sep 19, 2018 at 9:54
  • 1
    string Sortstr (str[mlength]); ==> string Sortstr(str);. And you are using non-standard VLAs in C++. Avoid that, and just use a std::string and std::getline properly. Commented Sep 19, 2018 at 9:55
  • Also not that variable-length arrays are not really a part of C++, though some compilers have it as a non-portable extension. Commented Sep 19, 2018 at 10:01
  • my idea is sorting user input into ascending order for example , bcda / dcba /cdba into abcd then start my permutation (arrange it into all possible combination) and the output will always sort start from A-Z Commented Sep 19, 2018 at 10:09

2 Answers 2

3

Why not just sort str?

sort(str, str + strlen(str));

There's no reason you can't sort an array directly. Just pass pointers to the first and one-past-the-end elements of your array to sort. In this case adding strlen gets a pointer to the effective end of your array.

Sign up to request clarification or add additional context in comments.

6 Comments

@Someprogrammerdude Thanks, fixed now, I didn't look at the original code closely enough.
changing the sort(Sortstr.begin(), Sortstr.end()); into sort(str, str + strlen(str));
the function will sort ACbdD into ACDbd instead of AbdCD , why the lowercase not in the front
it work fine if all input are the same all in lowercase / uppercase
@qing because that is the order defined by the character set you are using. If you want a different order you must write a comparison function that gives you the order you want and pass it as the third parameter to std::sort
|
2

In this line

string Sortstr (str[mlength]);

you are using the index operator on a char array which gives you one single char. So, you are passing one single char to the string constructor. This constructor does not exist, hence the error. Even if it existed, you do not want to pass one single char but the entire char array.

What you want is this:

string Sortstr (str);

Comments

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.