944,154 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 2088
  • C# RSS
Sep 9th, 2006
0

newbie question

Expand Post »
Well i need to make a program which would display a certain string in a triangle form in the middle of the console something like this:

C# Syntax (Toggle Plain Text)
  1.  
  2. t
  3. eee
  4. sssss
  5. ttttttt
it wont display right here but i think you get the point.

I am somewhat able to do that but i need to slow the display so that it looks like it is a triangle and want to be able to run it for as long as possible as this program is just for show...

C# Syntax (Toggle Plain Text)
  1. using System;
  2. class test {
  3. //Creating global variables
  4. protected static int orix;
  5. protected static int oriy;
  6.  
  7. //Function to print string in diffrent areas
  8. protected static void echo(string s, int x, int y) {
  9. try {
  10. Console.SetCursorPosition(orix+x, oriy+y);
  11. Console.Write(s);
  12. }
  13. catch(ArgumentOutOfRangeException error) {
  14. Console.Clear();
  15. Console.WriteLine(error.Message);
  16. }
  17. }
  18.  
  19. //MAIN PROGRAM
  20. static void Main()
  21. {
  22. string str = Console.ReadLine();
  23. int strlength = str.Length;//variable that keep string length
  24. int width = Console.WindowWidth, height = Console.WindowHeight;
  25.  
  26. //check to see if string is exist
  27. if(strlength != 0) {
  28. Console.Clear();
  29. orix = Console.CursorTop;
  30. oriy = Console.CursorLeft;
  31.  
  32. //check to see if the console width is 80 or set it
  33. if(width != 80) {
  34. Console.SetWindowSize(80, height);
  35. }
  36.  
  37.  
  38. int y = 0;
  39. while(true) {
  40. int hw = Console.WindowWidth/2;
  41. int halfway = hw;
  42. int ht = hw - strlength;
  43. int inc = 2;
  44. int incy = 0;
  45. int s = 1;
  46.  
  47.  
  48.  
  49. for(int i=0;i<strlength;i++) {
  50. if(i == strlength-1) {
  51. inc = -2;
  52. }
  53.  
  54. string stri = "";
  55. for(int n=0;n<s;n++) {
  56. stri += str[i];
  57. }
  58. s += inc;
  59.  
  60. echo(stri,halfway,y);
  61. y++;
  62. if(i == 0) {
  63. incy = -1;
  64. }
  65. if(halfway == ht) {
  66. incy = 1;
  67. }
  68. halfway = halfway+incy;
  69. }
  70. Console.WriteLine(strlength-1);
  71.  
  72. }
  73. }
  74. else {
  75. System.Console.WriteLine("A string needs to be entered for this program to work");
  76. System.Console.ReadKey();
  77. }
  78.  
  79. }
  80. }


it works for like a sec and there is text but it is so fast you cant read the text and then i get a error after that.

error:
The value must be greater than or equal to zero and less than the console's buffer size in that dimension.
Parameter name: top
Actual value was 3420.
Last edited by ashneet; Sep 9th, 2006 at 4:21 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Junior Poster
ashneet is offline Offline
147 posts
since Jun 2005
Jan 14th, 2009
0

Re: newbie question

Put
Console.ReadLine() when your app is exiting while loop. By that way you can keep your app running till user enter something.
Reputation Points: 10
Solved Threads: 2
Light Poster
virang_21 is offline Offline
29 posts
since Jan 2009
Jan 15th, 2009
0

Re: newbie question

You're setting a cursor position beyond the bounds of your console window, it wont like that
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: New in Asp.net & C#.net 2005
Next Thread in C# Forum Timeline: Object reference not set to an instance of an object.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC