

InterviewSolution
1. |
Solve : C++ Console App text display refresh question? |
Answer» When running a C++ program that displays for example the prior text and then a cartwheel with a percentage of complete for an installer, what method do people use to show the ascii text transition so that the prior text above it doesnt have to be refreshed. Currently I have a loop that displays the static text above it with a progress bar and the progress bar I have a cartwheel by use of stepping "/" then "-" then "\ "then "|" and repeat with a percentage to the right of it to show the progress, and the cartwheel pauses and continues when it reaches portions that take longer to process. The text above it flickers with each transition since it is reprinted to display after a system("cls"); routine, since otherwise you will have it scroll down each transition. \c (I believe( is carriage return (ASCII character 13) new line is \n carriage return is \r Think of a teletype machine with a carriage that can move LEFT and right (that holds the paper which is wrapped around a roller) and a fixed print head. Carriage return moves the carriage back (returns it) so the print head is over the leftmost character position. Newline returns the carriage and then moves the roller one lines worth of rotation "upwards" so that the print head is over the next line. Thanks for describing it like a teletype. That visual helped me better understand the \r At one time a teletype was the usual output device for computers, and later on you got a type of CRT + keyboard terminal sometimes nicknamed a "glass teletype". In many ways text output to the console is still based on teletype concepts. Thanks for the HELP. Below is a quick test of it in action. Works Awesome! I guess I was spoiled to never have to use a teletype. When I dove into computers in 1983 I had a monochrome display with Basic. The closest thing to a teletype was the dot matrix printer printing out my listed code or printing out fake Point of Sale transactions for school projects like the Grand Onion that I remember the teacher having us create a spoof of Grand Union Food Store. Code: [Select]//Testing \r with pretend installer #include<iostream> using namespace std; int a,b,x,i=0; int main(){ cout<<"Installation Simulation - using Slash R\n"; cout<<"=========================================\n"; while(i<=15){ //increment while loop counter placed here to change from 0 to 1 prior to loop i++; //Return and Display progress if(i==1){ cout<<"\r"<<"* [ 5% ]"; } //For delay placed here so that no delay on first display of 5% //For delay used so that you can see transition of STEPS vs only displaying last print //Very large number because fast computer can crunch out the 10 million calculations of b=a //quickly for(x=1;x<=100000000;x++){ b=a; } if(i==2){ cout<<"\r"<<"** [ 10% ]"; } if(i==3){ cout<<"\r"<<"*** [ 15% ]"; } if(i==4){ cout<<"\r"<<"**** [ 20% ]"; } if(i==5){ cout<<"\r"<<"***** [ 25% ]"; } if(i==6){ cout<<"\r"<<"****** [ 30% ]"; } if(i==7){ cout<<"\r"<<"******* [ 35% ]"; } if(i==8){ cout<<"\r"<<"********* [ 42% ]"; } if(i==9){ cout<<"\r"<<"********** [ 50% ]"; } if(i==10){ cout<<"\r"<<"*********** [ 55% ]"; } if(i==11){ cout<<"\r"<<"************ [ 60% ]"; } if(i==12){ cout<<"\r"<<"************* [ 67% ]"; } if(i==13){ cout<<"\r"<<"************** [ 75% ]"; } if(i==14){ cout<<"\r"<<"*************** [ 88% ]"; } if(i==15){ cout<<"\r"<<"**************** [ 100% ]\n"; } } cout<<"\n\n\n"<<"Simulated Installation Complete\n\n"; system("pause"); return(0); }Actually have another question as well since playing with the \r. It seems that if I have a statement in an COUT asking for user input in a CIN, I cant find a way to make the COUT display the first question and user input as keyed and then clear that last question for a clean screen for the next user input unless I scroll the screen with a bunch of \n's. I was hoping I could double up on the \r's and have it clear out the prior line which was the COUT information in addition to the user input as displayed. Currently the text displayed and user inputs unless I scroll with a bunch of \n's shows like this Enter Integer # 1 3 Enter Integer # 2 5 Enter Integer # 3 8 The sum of your input integers = 16 Is there a way to clear without using system("cls"); and without scroling with cout<<"\n\n\n\n\n\n\n\n\n\n"; so you see the first input question and then upon entering your answer it clears up 2 lines to overlay fresh information in the place of the prior question. All other text displayed above it uneffected since you dont have to clear screen or scroll to remove the prior question? There is no way to atomically clear the screen in C++; some libs seem to have a clrscr() function, but I keep getting unresolved externals when I try. The REASON is C/C++ don't know that the standard input and output are really something that can be cleared. For example, consider the case where the output is being piped to a file- what should clearing the output device do? Really, the only option is what you stated- a set of consecutive linefeeds.Thanks BC! Figured I'd check in case there was a better way. Guess I will stick with the consecutive line feeds since I try to avoid system("cls"); |
|