By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,599 Members | 1,493 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,599 IT Pros & Developers. It's quick & easy.

Passing array to functions thru pointers

P: 19
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(lenght);

validata= getstring(&lenght);

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 !!!!!
Nov 15 '06 #1
Share this Question
Share on Google+
3 Replies


sivadhas2006
100+
P: 142
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(lenght);

validata= getstring(&lenght);

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.

Expand|Select|Wrap|Line Numbers
  1. // function to check valid data:
  2. bool getstring (char * a_pszName)
  3.  
To pass the parameter
Expand|Select|Wrap|Line Numbers
  1. validata = getstring(lenght);
  2.  
Can I know what u r trying to do?

Regards,
M.Sivadhas.
Nov 15 '06 #2

P: 19
I got it thanks!!!
Nov 19 '06 #3

sivadhas2006
100+
P: 142
I got it thanks!!!
Hi,

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

Regards,
M.Sivadhas.
Nov 20 '06 #4

Post your reply

Sign in to post your reply or Sign up for a free account.