Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strings as input parameters to colsole apps

Status
Not open for further replies.

nhoellei

Programmer
Feb 1, 2002
13
US
I have a program that prompts a user for a string and then depending on the size of the string outputs varients of the inputted string with added or missing characters.

i.e.

int main() {

char cText[100];
cout << &quot;Enter the Text: &quot;;
cin >> cText; ( cText would be ABC )
// process cText to output
// sABCdef

I want to be able to setup the program to use strings passed as input parameters as well as prompting the user:

i.e.
int main(int argc,char* argv[]) {

int iStrCount = 0;

if(argc != 1) {
char cText[100];
cout << &quot;Enter the Text: &quot;;
cin >> cText; ( cText = ABC )
//Process cText
//output sABCdef
}
else
// process argv[]
// it outputs sABC**********def
// not actually the * char, but the a strange buffer character
}

What is happening is when I process argv[], I get the string as well as the buffer. Is there a way ( I'm sure there is ) to trim the &quot;¡§d&quot; within argv[] ?

(I have a feeling it's because I'm not defining an initial size to argv[].)

Is there a different way to do this?

Regards.


 
>> (I have a feeling it's because I'm not defining an initial size to argv[].)

Nope. That's not it.

> Is there a different way to do this?

Sure but you didn't post the way you are doing it. Your processing of the argv array needs some work.

-pete
 
Do I need to process argv into a pre-defined char array? Would something like this work?

cText = substr(0,strlen(argv));

or

for(int j = 0;j < strlen(argv); j++)
{
cText[j] = argv[j];
}

I keep getting invalid cast compile errors if I try these.
 
for(int j = 0;j < [red]argc[/red]; j++)
{
cText[j] = argv[j]; [red]// error[/red]
}

However since &quot;argv&quot; and &quot;cText&quot; are different types you will recieve a compile error. You are not understanding your pointers yet. Keep at it. It takes most people a little time before they start to get pointers.

-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top