0

I'm checking the string for alphanumeric characters and want to build a file system.

I've tried using the Fnameisaln recursively but it is giving me an invalid pointer, Aborted(core dumped error).

On debugging- "Program received Signal SIGABRT, aborted"

Maybe I can't modify the member string twice? Is there a rule like that?

 class File
 {
 public:
     std::string n;

     File(char str[]) {
         n = Fnameisaln(str);
     }

     std::string Fnameisaln(char a[]);
 };

 std::string File::Fnameisaln(char b[]) {
     int i = 0;
     bool isaln = false;  
     while(b[i]!='\0'){
           if(isalnum(b[i]))  {
                       isaln=true;
                        i++;
                        std::cout<< "\nWorking\n";
          }
          else {
                      isaln=false;
                      std::cout << "Warning!\n";            
                      break;
         }
 }// (while loop for checking if alphanumeric)

     if(isaln) {
         std::string s = b;
         std::cout << "\n W2 \n"; // to check if working
         return s;
     }
     else {
         std::cout << "Please enter file name again-\n";
         std::cin >> b;
         n.assign(Fnameisaln(b));  // n=Fnameisaln(b) also tried
     }
}

Expected - Checks Filename for alphanumeric characters and returns only when true.

Actual - Not returning when alphanumeric (still warns when I enter a character like '&' in the name)

  • Fix indentation and show your complete code. – BEPP Oct 04 '19 at 04:36
  • `Fnameisaln` does not always return a value. Your compiler should at least warn you about this, and more importantly it should be an error. – paddy Oct 04 '19 at 04:39
  • Why are you using character arrays and `std::string`? You should pass `std::string` (by reference) to your functions instead of character arrays. – Thomas Matthews Oct 04 '19 at 04:39
  • Please show the complete code. The error may not be where you think it is -- it may be in code you snipped out (especially when you have a while loop that's scanning over characters, which is ripe for segfaults). Have you put it in a debugger? A debugger would tell you where the problem is in about 5 seconds flat. – Cort Ammon Oct 04 '19 at 04:40
  • Search your favorite C++ reference for "isalpha". – Thomas Matthews Oct 04 '19 at 04:41
  • 1
    Your class seems like overkill. Too much stuff, when a simple string for the filename will work. – Thomas Matthews Oct 04 '19 at 04:42
  • Possible duplicate https://stackoverflow.com/questions/57842756 – n. m. could be an AI Oct 04 '19 at 04:44
  • @ThomasMatthews Trying that let's see. – Dipesh Malhotra Oct 04 '19 at 04:56
  • @paddy thing is I only want to return when name is alphnumeric. Won't that recursively return to 1st? (I'm just guessing) – Dipesh Malhotra Oct 04 '19 at 04:57
  • @CortAmmon at the return statement – Dipesh Malhotra Oct 04 '19 at 04:58
  • No, because it doesn't actually _return_ a value. Sure, some other call down the stack might, but not the original call. By the way, your while loop testing for alphanumeric characters is basically implementing `std::all_of`. – paddy Oct 04 '19 at 04:58
  • @paddy thanks man didn't know that! Solved, wasn't always returning, you right :) <3 Stack Overflow – Dipesh Malhotra Oct 04 '19 at 05:04
  • @ThomasMatthews what would passing std::string (reference) change? – Dipesh Malhotra Oct 04 '19 at 05:20
  • @DipeshMalhotra Arrays lose information, such as capacity and quantity of valid items. With `std::string`, you don't need to use `strlen` to find the length. There are other benefits as well. As for passing by reference, parameters are passed by copy to the function unless you pass by pointer or reference. IMHO, anything larger than the processor's register should be passed by reference (like `std::string` and arrays). – Thomas Matthews Oct 04 '19 at 14:13

1 Answers1

0

Problems with this code -

  • Wasn't always returning.
  • Was using redundant char arrays with strings.
  • Too many variables used.
  • Wasn't using reference to string.

Working solution -

  class File{
            public:
            std::string n;
            File(std::string n){
                            this->n=Fnameisaln(n);
            };
           std::string Fnameisaln(std::string& n);
           void printn();
  };

       void File::printn(){
                       std::cout << this->n << "\n";
        } 
        std::string File::Fnameisaln(std::string& b){
                 int i=0;
                 bool isaln=false;
                 while(b[i]!='\0'){
                                if(isalnum(b[i])) {
                                           isaln=true;
                                            i++;
                                 }

                              else  {
                                 isaln=false;
                                 std::cout << "Warning!\nOnly letters and numbers allowed!\n";
                                 break;
                              }
               }

              if(isaln) {
                      return b;
               }

               else {
                      std::cout << "Please enter file name again-\n";
                      std::cin >> b;
                      n=Fnameisaln(b);
                      return n;
               }
       }