- Sep 4, 2000
- 482
- 0
- 0
well I guess, it seems that passing strings is a no no in C++, but can I pass arrays at all period? The compiler (g++) seems to spit chunks when I try to do a function that returns an array. Here is my little proggie:
I was just playing with string stuff and wanted to get a string that was the actual length of the input, not a whole 64 or 32 characters or whatever.
Do I *have* to write this using pointers instead?
//
#include <stdio.h>
#include <iostream.h>
#include <strings.h>
// supposed to take a string as input, and return a string
// with the return string only as long as it needs to be, using
// spaces and null as the delimiter.
char[] chopString(char input[]){
int trueLength=0;
int inputLength = strlen(input);
// Get the length of the string up to the first space or null char
for (int i = 0; i < inputLength; i ++){
if ((input== 0) || (input==32)){ // if space or null char
break;
}
else trueLength++;
}
// declare the array result
char result[trueLength];
// read the characters up to the true length into the dest string
for (int i = 0; i < trueLength; i ++){
result=input;
}
// return the dest string?
return result;
}
int main(int nNumberofArgs, char* pszArgs[])
{
//cout << "This is a test\n"
// << "This is only a test\n";
// print out the original string and the chopped string and their lengths
char szString1[32]; cout << " x is : " << x;
cout << " Give me a String: ";
cin.getline(szString1,32);
cout << "The String was";
cout << szString1 << "\n";
int length = strlen(szString1);
cout << "The length was : " << length;
cout << "CHopping the String \n";
szString1 = chopString(szString1);
cout << "The String now is :";
cout << szString1 << "\n";
int length = strlen(szString1);
cout << "The length now is : " << length;
return 0;
}
Here is what the compiler spits out, it doesn't seem to like the char [] thingee:
-----------------
-------------------------------------------------------------------
hoff0103@superman (~/cstuff) % g++ stringTest.cpp
stringTest.cpp:15: parse error before `['
stringTest.cpp:17: `input' was not declared in this scope
stringTest.cpp:20: parse error before `for'
stringTest.cpp:20: parse error before `;'
stringTest.cpp:20: syntax error before `++'
stringTest.cpp:27: `trueLength' was not declared in this scope
stringTest.cpp:31: parse error before `for'
stringTest.cpp:31: `trueLength' was not declared in this scope
stringTest.cpp:31: parse error before `;'
stringTest.cpp:31: syntax error before `++'
stringTest.cpp: In function `int main(int, char **)':
stringTest.cpp:47: `x' undeclared (first use this function)
stringTest.cpp:47: (Each undeclared identifier is reported only once
stringTest.cpp:47: for each function it appears in.)
stringTest.cpp:56: implicit declaration of function `int chopString(...)'
stringTest.cpp:56: incompatible types in assignment of `int' to `char[32]'
stringTest.cpp:59: redeclaration of `int length'
stringTest.cpp:52: `int length' previously declared here
hoff0103@superman (~/cstuff) % exit
I was just playing with string stuff and wanted to get a string that was the actual length of the input, not a whole 64 or 32 characters or whatever.
Do I *have* to write this using pointers instead?
//
#include <stdio.h>
#include <iostream.h>
#include <strings.h>
// supposed to take a string as input, and return a string
// with the return string only as long as it needs to be, using
// spaces and null as the delimiter.
char[] chopString(char input[]){
int trueLength=0;
int inputLength = strlen(input);
// Get the length of the string up to the first space or null char
for (int i = 0; i < inputLength; i ++){
if ((input== 0) || (input==32)){ // if space or null char
break;
}
else trueLength++;
}
// declare the array result
char result[trueLength];
// read the characters up to the true length into the dest string
for (int i = 0; i < trueLength; i ++){
result=input;
}
// return the dest string?
return result;
}
int main(int nNumberofArgs, char* pszArgs[])
{
//cout << "This is a test\n"
// << "This is only a test\n";
// print out the original string and the chopped string and their lengths
char szString1[32]; cout << " x is : " << x;
cout << " Give me a String: ";
cin.getline(szString1,32);
cout << "The String was";
cout << szString1 << "\n";
int length = strlen(szString1);
cout << "The length was : " << length;
cout << "CHopping the String \n";
szString1 = chopString(szString1);
cout << "The String now is :";
cout << szString1 << "\n";
int length = strlen(szString1);
cout << "The length now is : " << length;
return 0;
}
Here is what the compiler spits out, it doesn't seem to like the char [] thingee:
-----------------
-------------------------------------------------------------------
hoff0103@superman (~/cstuff) % g++ stringTest.cpp
stringTest.cpp:15: parse error before `['
stringTest.cpp:17: `input' was not declared in this scope
stringTest.cpp:20: parse error before `for'
stringTest.cpp:20: parse error before `;'
stringTest.cpp:20: syntax error before `++'
stringTest.cpp:27: `trueLength' was not declared in this scope
stringTest.cpp:31: parse error before `for'
stringTest.cpp:31: `trueLength' was not declared in this scope
stringTest.cpp:31: parse error before `;'
stringTest.cpp:31: syntax error before `++'
stringTest.cpp: In function `int main(int, char **)':
stringTest.cpp:47: `x' undeclared (first use this function)
stringTest.cpp:47: (Each undeclared identifier is reported only once
stringTest.cpp:47: for each function it appears in.)
stringTest.cpp:56: implicit declaration of function `int chopString(...)'
stringTest.cpp:56: incompatible types in assignment of `int' to `char[32]'
stringTest.cpp:59: redeclaration of `int length'
stringTest.cpp:52: `int length' previously declared here
hoff0103@superman (~/cstuff) % exit