Passing array to functions thru pointers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Theadmin77
    New Member
    • Nov 2006
    • 19

    Passing array to functions thru pointers

    Ok , i am a baby in the world of C ++ ...
    I have to write a code where takes string from user ,check for error (thru a function ) and return it to the main function ,everything using pointers . I am really stuck on it (it does not take my pointers to function )

    What i am doing wrong ?? This is it:

    #include <iostream>
    #include <cstdlib>

    using namespace std;

    bool getstring (char*);

    void main ()

    {
    char lenght[100];
    int leng;
    bool validata;

    do
    {
    cout <<"Enter your name in lowercase :";
    cin.getline (lenght,100);

    leng=strlen(len ght);

    validata= getstring(&leng ht);

    if (validata)
    {
    cout <<"Enter your last name in lower case: \n";
    cin.getline (lastname,100);

    }
    else
    {
    cout <<"Wrong input ...Try Again !!! \n";
    }


    }while (!validata);

    }

    // function to check valid data:


    bool getstring (char buff[], *lenght)
    {
    int i;
    char buf[100];
    int *lenght;
    bool noerrors = true;


    cin.getline(buf ,100);

    i=0;

    while ((i< strlen(buf)) && noerrors)

    {
    if ((buf[i]<'a') || (buf [i]>'z'))
    {
    noerrors=false;
    }
    *lenght=strlen( buf);

    i++;
    }

    return noerrors;
    }


    Any help will be really appreciated !!!!!
  • sivadhas2006
    New Member
    • Nov 2006
    • 142

    #2
    Originally posted by Theadmin77
    Ok , i am a baby in the world of C ++ ...
    I have to write a code where takes string from user ,check for error (thru a function ) and return it to the main function ,everything using pointers . I am really stuck on it (it does not take my pointers to function )

    What i am doing wrong ?? This is it:

    #include <iostream>
    #include <cstdlib>

    using namespace std;

    bool getstring (char*);

    void main ()

    {
    char lenght[100];
    int leng;
    bool validata;

    do
    {
    cout <<"Enter your name in lowercase :";
    cin.getline (lenght,100);

    leng=strlen(len ght);

    validata= getstring(&leng ht);

    if (validata)
    {
    cout <<"Enter your last name in lower case: \n";
    cin.getline (lastname,100);

    }
    else
    {
    cout <<"Wrong input ...Try Again !!! \n";
    }


    }while (!validata);

    }

    // function to check valid data:


    bool getstring (char buff[], *lenght)
    {
    int i;
    char buf[100];
    int *lenght;
    bool noerrors = true;


    cin.getline(buf ,100);

    i=0;

    while ((i< strlen(buf)) && noerrors)

    {
    if ((buf[i]<'a') || (buf [i]>'z'))
    {
    noerrors=false;
    }
    *lenght=strlen( buf);

    i++;
    }

    return noerrors;
    }


    Any help will be really appreciated !!!!!
    Hi,

    The function prototype will be like this.

    Code:
    // function to check valid data:
    bool getstring (char * a_pszName)
    To pass the parameter
    Code:
    validata = getstring(lenght);
    Can I know what u r trying to do?

    Regards,
    M.Sivadhas.

    Comment

    • Theadmin77
      New Member
      • Nov 2006
      • 19

      #3
      I got it thanks!!!

      Comment

      • sivadhas2006
        New Member
        • Nov 2006
        • 142

        #4
        Originally posted by Theadmin77
        I got it thanks!!!
        Hi,

        Welcome to http://www.thescripts. com/.

        Regards,
        M.Sivadhas.

        Comment

        Working...