I am attempting to write a code that output text one letter at a time with a small delay between them. I wanted to put in some code that would detect if a user entered a key at which point it would display the entire line of text all at once and break out of the for loop. When I attempt to put an if/else statement in the program, it enters the for loop and stops whenever it hits the if part of the program.
//The purpose of this program is to write out text across the screen a letter at a time
//if a user hits a key, it exits the loop and outputs the entire thing at once.
#include <iostream>
#include <Windows.h>
void main()
{
char
chrbuffer[80] = {0},
str[80] = "This is used to represent a long line of text that will appear on the screen.";
for(int i = 0; i <= strlen(str); i++)
{
std::cout << str[i];
Sleep(90);/*Sleep is measured in milliseconds || 1000 milliseconds = 1 second.
Sleep is Windows exclusive, will need to see if it interrupts the entire
program or just this sequence*/
//if(std::cin.get() == '\n')
//{
// std::cout << str;
// break;
//}
}
std::cout << "\n";
std::cin.get();
}
The compiler isn't actually giving me any errors. If I run the program with lines 18-22 un-commented it will output the first letter, wait for you to hit enter, output the next character on the second line, and continue the same pattern.
Something that is interesting with it is that if I hold down any key other than "enter" until you hit enter, then it will output one letter for each character that was typed.
i.e. If i entered 12 spaces, it would output the next 12 characters in the array, but on the same line.
I think, but can not verify (not at a compiler) that the problem is your cin.get() line. You have your if statement looking for a '\n', or return key. But if you give it something else (like a space) then it will evaluate as false and cout str[i].
I would have declared another char variable in the beginning, such as
char x; //used for decision making
And then put the cin statement in the loop instead of the if statement, such as
1 2 3 4 5
cin << x;
if(x=='\n')
{
//...
}
The problem is, using cin will always pause a program for input, so the delay of 90 you have placed earlier in the code does nothing, because cin will always pause it. Offhand, I can't think of any way to get a character without pausing the program unless you do multithreading which is rather complex, I don't totally get it myself yet. I'll do some digging and get back to you.
#include <iostream>
#include <Windows.h>
int main()
{
char
chrbuffer[80] = {0},
str[80] = "This is used to represent a long line of text that will appear on the screen.";
for(int i = 0; i <= strlen(str); i++)
{
std::cout << str[i];
Sleep(90);
// may or may not work depending on the implementation of in_avail()
if ( std::cin.rdbuf() -> in_avail() )
{
std::cout << str+i+1 ;
break ;
}
}
std::cout << "\n";
std::cin.get();
}
is, I think, the closest you can get to doing this with std::cin, and as the comment notes whether it works is dependent on implementation details.
Input via cin is blocking. There isn't a portable way to do this, and since you're using windows specific code anyway, perhaps you should seek a non-blocking OS-specific way to detect if input is available.