Trouble using strings in user-made functions

Hello. I'm currently writing my own function that accepts a string and then prints it to the screen one character at a time to make it look like it is being typed rather than showing the whole string at once.

The problem is, I don't seem to be able to use a function that passes the value of a string using apstring. I'm not sure if there's a different method to go about doing this or not. Forgive me, it's probably quite a simple question. I just started learning C++ this semester (although I have past programming experience), and I'm still getting used to it.

Here's the code I'm using:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// steptext.cpp

#include <iostream.h>
#include "apstring.cpp"

void type(apstring message);

int main()
{
  type("This text is typing itself.");
  
  system("PAUSE");
  return 0;
}

void type(apstring message)
{
  int i, l;
  
  l = message.length();
  
  for ( i = 0; i <= l; i++ )
  {
    cout << message[i];
  }
  cout << endl;
}


I'm pretty sure the problem is when I try to pass the value of a string using apstring into the function, as it seems to work if I change the string to an integer data type. Also, I'm using Bloodshed Dev-C++, if that is of any concern to you guys.

Thanks for the help, it's greatly appreciated. :D
i <= l will go out of bounds when i == l

Other than that, what is an apstring?
I see at least one error :
- you read one character too much in your loop. if the first character is at position ., the last is at length-1, but the ending conditions will also try to read the character at length (or do you do this to get the '\0'? it's useless in that case)

And some bad things that might bring problems :
- don't include a cpp file, include only h files
- pass values to functions by address or reference if they are not small types
-i think <iostream> is more recommended than <iostream.h>

And finally, what is not working, or how?
Thanks for the help, guys. I changed the 'i <= l' to 'i < l', and it worked. Unfortunately, the loop happens too fast to make it look as if it is typing itself. I have another idea on how to make it work, though.

And, as for the issues you addressed, bartoli--my complier, Bloodshed Dev-C++, doesn't work with header files unless you include the .cpp file as well. I should probably include both the .cpp and the .h to be safe, but meh.

I may try passing by reference in the future. Thanks.

Again, my compiler doesn't work unless you include the extension for the header file. <iostream> may be more recommended, but I gotta do what works with my complier DX

Thanks again for the help, guys.
In the directory of include files for most compilers there is an "iostream" header file with no extension, and then an "iostream.h" file which has the .h file extension. They are different, and many of the other standard headers work this way too. If your compiler only has the .h versions and does not have the newer extensionless versions, either download them or update your compiler... ;)
Topic archived. No new replies allowed.