I'm a noob who just started learning C++ 2 days ago, but I'm having difficulty making my letters appear consecutively. I'm trying to make my code shorter but also keep the Sleep() function. Here is my first code using Sleep() function:
constchar* str = "hello world!";//an array of characters.
for(constchar* p = str; *p != 0; p++){//a for loop which goes through every char
Sleep(100);
cout << *p;
}
I am very new to c++ myself but I think I have seen the sort of thing you are trying to do done.
The first thing you will need to do is put your text into a variable.
1 2
char * myText;
myText = "HELLOWORLD!";
I believe you can then access characters in the character array by using myText[x] - where x is a number indicating the position of the character in the string..
Once you have mastered the above, a 'for loop' might be a good idea to iterate through them with much less code.
I may be wrong, but I believe the above should work.
1 2 3 4 5 6 7 8 9
char * myText;
myText = "HELLOWORLD!";
// != '\0' is not required, but more readable
for(int i = 0; myText[i] != '\0'; i++) {
cout << myText[i];
sleep(100);
}
This is rough code and not tested but hopefully gives an idea what I am talking about.
Hope it helps a bit. If you cant make it work then post back and I will run it through a compiler and check it. At this point was just hoping to point you in the right direction.
James259, your code works too.
Just so that you know, compilers for some reason allow line 2, but really, the type of string literals is constchar*. For example if you wrote myText[0] = 'Y'; on line 3, your program should crash.
If you want to create a non constant char array from a string literal, write char myText[] = "hello";
What do you want to do with these codes?
A. Print every line with a for loop like before.
You'd have hard time doing this. The lines are all different, the gaps aren't uniform, some lines contain integers and input.
B. Print every line with gaps between letters as if it was typed by a human.
That is more simple. You'll have to use a stringstream though.
Create a stringstream object, fill it with stuff (using operator <<), then do the lines of code James posted where string myText = my_stringstream.str();. Don't forget to use functions.
isn't the sleep value provided in seconds?? Ouch! 5000 seconds. I have to be wrong there. thats like 1 hours 20 minutes.
I was reading through the above and wondered if what iHAKKER was trying to achieve was simply to have all those sentences typed out character by character. (rather than line by line)
Maybe I read it wrong but if this is what you intended then it should be relatively simple to do. You just need to wrap up most of your existing code into a function and then pass each line to the function as a parameter.. You could then call the function for each line above and add in extra sleep statements in between the lines if you wanted.
If you are wanting to make it look more like its a person typing it.. maybe randomising some of the delays between characters might be good too.