OK, so I’m trying to make a string thing, so that the string is updated. Sort of like you have a string “hello” and I want it to update itself somewhat like “h” “he” “hel” “hell” “hello”
So, I have:
#include <iostream>
#include <string>
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
using namespace std;
int main()
{
system("title game");
system("color 0a");
string sentence = "super string ";
for(int i=1; i<sentence.size(); i++){
cout << sentence.substr(0, i) <<endl;
}
return 0;
}
The code returns out like:
“s
“su”
“sup”
“supe”
“super”
Obviously on different lines, but when I remove the end line, the sentence builder just goes berserk. It displays something like “spupsppuepr sttrrtrsubstringsubstring”
Is there anyway I can update the string on THE SAME LINE? (and not have it completely destroyed)
You could print a carriage return character
'\r'at each iteration, returning the cursor to the beginning of the line:Or just output each character in sequence:
You probably also want to insert a short delay for each loop iteration to achieve a typewriter effect.