-2

I am making this program to check the alphabetic and numeric characters of a C-type string. I am using C-type strings because it is for an assignment, otherwise I would opt to use std::string.

How do I declare the function? In my case, I want str, SAlpha and SNum, to be stored in the function as s, alpha, num. That's why I am using references, but I don't understand how to declare it without giving me an error saying undefined.

I have been searching, but I am new to functions, and don't understand them quite well. That's why I'm asking.

Below is the code:

#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;

void seperate(char (&s)[], char (&alpha)[], char (&num)[]);

int main() {
    char str[100];
    char SAlpha[100];
    char SNum[100];

    cout << "Insert a string: ";
    cin.getline(str,100);

    strcpy(SAlpha, str);
    strcpy(SNum,str);

    cout << "Alphabetic characters " << endl;

    for (int i = 0; i < strlen(SAlpha); i++) {
        if (isalpha(SAlpha[i])) {
            cout << " " << SAlpha[i];
        }
    }
    cout << endl;
    cout << "Numeric characters " << endl;
    for (int i = 0; i < strlen(SNum);i++) {
        if (isdigit(SNum[i])) {
            cout << " " << SNum[i];
        }
    }

    seperate(str, SAlpha, SNum);    //UNDEFINED FUNCTION
    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Eternal23
  • 3
  • 5
  • 1
    Where are you **defining** `seperate` [sic]? – Davis Herring Nov 23 '22 at 22:21
  • Be cautious here. When you pass an array, it automatically decays to a pointer and is passed by reference (just not *a* reference). Make sure you fully understand what the instructor is requiring you to do. – user4581301 Nov 23 '22 at 22:21
  • Which version of C++ standard you can use? C++20 has some nice features which can address your problems perfectly. – Marek R Nov 23 '22 at 22:26
  • [std::copy_if](https://en.cppreference.com/w/cpp/algorithm/copy) would make very short work of this task. – paddy Nov 23 '22 at 22:29
  • @user4581301 oh i didn't know that my bad. Is it better to use pointers in this case or not ? – Eternal23 Nov 23 '22 at 22:30
  • @MarekR i am using C++ 14 version actually. – Eternal23 Nov 23 '22 at 22:31
  • 2
    This code contains to much C style code. You should use C++ features: https://godbolt.org/z/3TaKjz6cb also you didn't provide implementation of `seperate` that is why error is reported. – Marek R Nov 23 '22 at 22:36
  • Example using `copy_if`: https://godbolt.org/z/8E1zs3f5n – paddy Nov 23 '22 at 22:40
  • @MarekR it is for a school assignment that tells what to use and what not thats why there is too much C style code, otherwise i would try using more C++ appropriate code. – Eternal23 Nov 23 '22 at 22:48
  • @Eternal23 link for your teacher: https://youtu.be/YnWhqhNdYyk – Marek R Nov 23 '22 at 22:54
  • Another excellent summary is here: https://youtu.be/dQw4w9WgXcQ – paddy Nov 23 '22 at 23:08
  • @paddy you got me lol. So man is there any other way u can declare the function in my case without using copy if ? – Eternal23 Nov 23 '22 at 23:20
  • Yes. Others gave you examples of using loops. You used loops yourself. – paddy Nov 23 '22 at 23:21
  • @paddy there was one more example but he was using std::string which im not allowed to use. Other than that i don't see any other example – Eternal23 Nov 23 '22 at 23:33
  • I think the problem is you're expecting exact code hand-outs, instead of looking at the _technique_, thinking about it and trying to write it yourself using a similar approach. The function signature I showed you should be fine. And you just need to build your strings using loops instead of the `copy_if` calls. I intentionally used a function that you probably aren't allowed to use, so that you will still need to do some work for your assignment. I imagine the example using `std::string` was offered on the same premise. This is why they are called _examples_. – paddy Nov 23 '22 at 23:36
  • @paddy Okay i understand you are right thank you for taking your time and answering! – Eternal23 Nov 23 '22 at 23:41
  • 1
    You're welcome. Note that what may seem like "unhelpfulness" is usually well-intentioned. People in general want to help guide you in the right direction, but outright giving solutions is not gonna help you learn. At least half of programming is about troubleshooting, experimentation and investigation. It's not just about writing lines of code. The sooner you become comfortable with this process, the sooner you will learn to figure things out on your own. That is critical in this profession. There is not really any shortcuts to this kind of learning. – paddy Nov 23 '22 at 23:46
  • @user4581301 "*When you pass an array, it automatically decays to a pointer*" - not in this case, since the function takes in the arrays by reference, so there is no decay involved. However, because references are being used, the actual array sizes must be specified, eg: `void seperate(char (&s)[100], char (&alpha)[100], char (&num)[100]);` You can't take a reference to an array of unspecified size. – Remy Lebeau Nov 24 '22 at 02:02
  • @RemyLebeau Agreed. My concern was more the instructor might be expecting `void seperate(char s[], char alpha[], char num[]);` where the arrays are being passed by reference via pointers. – user4581301 Nov 24 '22 at 02:10
  • @RemyLebeau i have tried inserting size 100 to arrays but it still says undefined it removes the error but when it runs it still is undefined. – Eternal23 Nov 24 '22 at 08:25
  • @Eternal23 because, as DavisHerring stated, you have only **declared** the function but you have not **defined** (ie implemented) it. Where is the actual code for the function's body? It is missing in the code you showed. – Remy Lebeau Nov 24 '22 at 16:21
  • @RemyLebeau im sorry i don't understand functions well. Should i define the function and write the code after defining is that how it is correctly defined ? And have only cout outside of the define function – Eternal23 Nov 24 '22 at 19:13
  • @Eternal23 `void seperate(char (&s)[100], char (&alpha)[100], char (&num)[100]);` (note the semicolon) is a **declaration**. It tells the compiler that the function *exists*, somewhere. `main()` is actually *calling* the function, so the function must be **implemented** somewhere that the linker can find it. You need code that looks like this: `void seperate(char (&s)[100], char (&alpha)[100], char (&num)[100]) { ... }` (note the braces) where `...` is the actual code of the function. That implementation is missing, you need to add it. Look at how `main()` has code between its `{ }` braces – Remy Lebeau Nov 24 '22 at 21:33
  • @Eternal23 If you can't understand that about functions, then stop what you are doing and go get yourself some [good C++ books](https://stackoverflow.com/questions/388242/), they will cover this topic in detail. – Remy Lebeau Nov 24 '22 at 21:35

1 Answers1

0

You are getting an "undefined" error because you have only declared the seperate() function but have not implemented it yet, eg:

#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;

// THIS IS JUST A DECLARATION!!!
void seperate(char (&s)[100], char (&alpha)[100], char (&num)[100]);

int main() {
    char str[100];
    char SAlpha[100];
    char SNum[100];

    cout << "Insert a string: ";
    cin.getline(str,100);

    strcpy(SAlpha, str);
    strcpy(SNum,str);

    cout << "Alphabetic characters " << endl;

    for (int i = 0; i < strlen(SAlpha); i++) {
        if (isalpha(SAlpha[i])) {
            cout << " " << SAlpha[i];
        }
    }
    cout << endl;
    cout << "Numeric characters " << endl;
    for (int i = 0; i < strlen(SNum);i++) {
        if (isdigit(SNum[i])) {
            cout << " " << SNum[i];
        }
    }

    seperate(str, SAlpha, SNum); // <-- OK TO CALL SINCE THE FUNCTION IS DECLARED ABOVE...

    return 0;
}

// ADD THIS DEFINITION!!!
void seperate(char (&s)[100], char (&alpha)[100], char (&num)[100])
{
    // do something here...
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770