-
String Help
Hey everyone,
This is my first post so go easy on me. I am pretty much a beginner when it comes to C++ so please assume that I know nothing when you reply :D
Okay so here is the question, I am trying to create a simple function using Dev C++
for a dos base program.
Basically what I want the function to end up with is when I type something similar to typeit("This is my text"); it will display the characters on the screen one by one (like on the matrix).
I thought I had this worked out and when I compile the prog I get truckloads or errors..javascript:smilie(':confused:')
confused Can you guys please look at the code below and let me know what I am doing wrong (in english) :)
void typeit(string type_text){
double x= type_text.size();
char ticker[x]=type_text;
double i=0
while (i != x/4){wait(.3); cout ticker[x];i++;}
while (i != x/4*2){wait(.2); cout ticker[x];i++;}
while (i != x/4*3){wait(.1); cout ticker[x];i++;}
while (i != x){wait(.2); cout ticker[x];i++;}
wait(2);
system("cls");
}
Any suggestions
:confused:
-
>double x= type_text.size();
>char ticker[x]=type_text;
Why are you declaring x as type double? an int would be better. Also, im not sure if you can declare an array size with a doube value, although the compiler will probably do a typecast automatically.
Im not too familiar with the string class but I think how you initialized ticker is invalid. Why not just use your 'type_text' variable in place of your ticker.
>double i=0
something's missing;;;
>if (i != x/4)
well, this is no good. since 'i' will only be a whole number and x/4 could evaluate to a fraction, you will need to use '>' or '<' instead of '!='
>cout ticker[x];
should be cout << ticker[x];
check the syntax of the cin/cout objects
-
Thanks clownpimp some of those errors were stupid typos and cleared up alot of compiler errors but the main problem was the string not being recognised by the compiler
ie typeit(string type_text)
and type_text.size();
And yes I have included string.h, so what can I do to fix this...?
The errors are sayin stuff like string undeclared and type_text undeclared...........
Other suggestions....
-
What compiler are you using? Instead of including string.h include string like the following:
Code:
#include<string>
using std::string;
string.h is the old C-style string header, while string is the new standard header, and to recognize the string class you must either use the using std::string, using namespace std, or qualify each declaration of a string with std::string string_name;
-
with a bit of fiddling around I have got it to work :D thanks so much for all your help!