Making letters appear consecutively?

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:

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
28
29
30
31
32
#include <iostream.h>
#include <windows.h>

int main()
{
    cout << "\n";
    cout << "\n";
    cout << "H";
Sleep(100);   
    cout << "E";
Sleep(100);   
    cout << "L";
Sleep(100);    
    cout << "L";
Sleep(100);    
    cout << "O ";
Sleep(100);    
    cout << "W";
Sleep(100);    
    cout << "O";
Sleep(100);    
    cout << "R";
Sleep(100);    
    cout << "L";
Sleep(100);   
    cout << "D";
Sleep(100);
    cout << "!\n";
Sleep(1000);
system("pause");
return 0;
}


It works perfectly but its too long. I want to be able write huge paragraphs of writing which take up too much space. Thanks
1
2
3
4
5
6
const char* str = "hello world!";//an array of characters.

for(const char* p = str; *p != 0; p++){//a for loop which goes through every char
   Sleep(100);
   cout << *p; 
}
Hi,

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.

James
Last edited on
Trust hamsterman more than me!.. Took me so long to type that I did not realise he had posted in the meantime. :/
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 const char*. 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";
Hi Hamsterman,

Many thanks for the heads up.
Sorry for the late reply. I was ill :P

Anyways, Thanks. I think I've got the hang of it now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <conio.h>
#include <windows.h>

using namespace std;

void z() {Sleep(50);}

char x[] = "\n Hello World!#";
int main(){
for (int i=0;x[i]!='#';i++)
{
system("COLOR 0a");
cout << x[i];
z();
}
getch();
return 0;
}


But I'm having trouble integrating this loop into my project.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;

int main ()
{
  system("COLOR 0a");
  string mystr;
  cout << " \n  Hello, My name is System! \n ";
  Sleep(2000);
  cout << " \n  What's your name? \n \n  ";
  getline (cin,  mystr);
  cout << " \n  Hello " << mystr << ".\n";
  Sleep(2000);
  cout << " \n  What is your favorite colour? \n \n  ";
  getline (cin,  mystr);
  Sleep(2000);
  cout << " \n  That's nice. My favourite colour is " << mystr << " too! \n  ";
  Sleep(3000);
  cout << " \n  What is your favourite food? \n \n  ";
  getline (cin,  mystr);
  Sleep(3000);
  cout << " \n  Oh... \n  ";
  Sleep(2000);
  cout << " \n  I've never heard of that type of food before. \n  ";
  Sleep(3000);
  cout << " \n  Can you explain to me briefly what " << mystr << " is? \n \n  ";
  getline (cin,  mystr);
  Sleep(3000);
  cout << " \n  I see... \n  ";
  Sleep(2000);
  cout << " \n  but I am still finding it hard to understand the concept. \n ";
  Sleep(5000); 
  cout << " \n  Anyways, I have to go now.\n  ";
  Sleep(2500);
  cout << " \n  BYE! \n \n \n  ";
system ("pause");
  return 0;
}


And I was wondering if theres anyway I can improve my code. Thanks

Last edited on
You really lie Sleep(), eh?

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.
erm....B!
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.
No no, its actually in milliseconds so 5000 is 5 seconds precisely.

Your spot on. I want every single character to appear one after another.

I'm relatively new to C++ so I'm kinda struggling to fully grasp the concept.

Can you possibly show me how to do that?
We will not give complete codes, so I advise you to do the following:
Check this page:
http://www.cplusplus.com/doc/tutorial/functions/

Write a function that does the following:
1
2
3
4
Function accepts a char* input
  When the end of the input is not reached
     Print the next character
     Wait
Last edited on
Topic archived. No new replies allowed.