Hello.

Recently, I decided to return to learning C ++, but since my level of knowledge is catastrophically low, I came here again :) Today I want to try to do something like a typewriter. It sounds loud, but I will explain what the essence is. Console application in C ++. The user enters his text, and then the field is cleared and the computer starts typing the text at quarter intervals (for example) seconds between characters.

Since I came here to learn, I will try to express my ideas. From what I know, Sleep is needed here, most likely the use of arrays and loops. It seems like it should be easy. In my head flashed the idea that each character from the text should be made to the array. Or replace the variable. That is, the value of the variable is replaced with a text symbol every time, and at a quarter-second interval it is displayed on the screen as long as there are characters in the entered text.

I will be grateful to those who will help to understand a little, and better push to the decision.

UPDATE

#include <iostream> #include <Windows.h> using namespace std; int main() { char string str[]; cout << "Type your text"; cin >> str; for (int i = 0; i<=str.Length - 1; i++) { cout << str[i]; Sleep(200); } system("pause"); } 

That's what happened with me. This gives an error. What is wrong here?

  • one
    save the string and print it character by character (the most common search), insert sleep into a cycle for 0.25 seconds. and all) - Sh4dow
  • 2
    For the future - if the program "gives errors" - be sure to write which ones. This will speed up the decision many times and reduce the concentration of the mat) - Sh4dow

4 answers 4

Instead

 char string str[]; 

write just

 string str; 

Instead of

 str.Length str.length() 

Before further questions, I recommend reading this resource .

  • Unfortunately, there is still a mistake - Yegor Eremin
  • @ Vendetta8247, you write Unfortunately, still a mistake suggesting the presence of telepathic abilities in potentially responding? Give the text produced by the compiler (or what you see on the screen). - avp
  • one
    cplusplus.com is good only because it loves google, however, if you think about it with your head, the fact that google is probably authoritative in python, javascript ... but not in these and crosses. then a short search will lead to an alternative resource - karmadro4

Thanks for the comments. Besides the fact that avp answered (thanks for the resource), it was also necessary to add #include <string>

  • The only problem that remains is that the space is not recognized. When I write the text "Hello World!" comes to the end of the word Hello and stops - Yegor Eremin
  • one
    @ Vendetta8247, I have MinGW g ++ running and without the #include <string> operator >> for string enters not the entire string, but the next "word". To read the entire line instead of cin >> str; write getline (cin, str); - avp
 #include <iostream> #include <windows.h> #include <conio.h> using namespace std; int main() { string str; cout << "Enter the String: " << endl; getline(cin, str); for (int i = 0; i <= str.length() - 1; i++) { cout << str[i]; Sleep(200); } cout << endl; system("PAUSE"); return EXIT_SUCCESS; } 

This is how it looks. I also decided to do it for the sake of interest. What you need =)

  • And why include conio.h? What is it and is it necessary? - Yegor Eremin
  • @ Vendetta8247, to use Sleep () you need to connect the library <conio> - Rules
  • @ Vendetta8247 To use the Sleep () function, you need an additional 2 libraries: conio.h and windows.h - navi1893
  • For Sleep () , windows.h is sufficient . Without conio.h, this program is also great. - Yes, you look at the file (conio.h) * Low level console I / O functions. Please try to use ANSI * standard ones if you are writing new code. It is needed when calling functions getch (), kbhit (), etc. - avp

It's easy, here's a C # option:

 static void Main(string[]args) { Console.WriteLine("Введите строку"); string str = Console.ReadLine(); // считываем строку for (int i = 0; i <= str.Length - 1; i++) // запускаем цикл до тех пор, пока не будет достигнут последний символ нашей строки { Console.Write(str[i]); // выводим 1 символ, с каждой итерацией он возрастает, и выводится следующая буква Thread.Sleep(300); // засыпаем на 0.3 секунды. } Console.Read(); // ждем нажатия клавиши } 
  • @johniek_comp, it is clear that the TC has a problem not with the output algorithm , but with the C ++ language . Why does he need code in another language? - avp