Jump to content

  • Log In with Google      Sign In   
  • Create Account

stitchs

Member Since 22 Feb 2011
Offline Last Active Dec 21 2014 11:31 AM

Topics I've Started

Reading Integers from a Text File

22 November 2014 - 11:03 AM

Hello,

 

I am working on an exercise for C# that involves reading from a Text file, extracting any Integer values (+ or -) and displaying them in the console window, in a tabular format.

 

On a 'one value per line' basis, I have managed to accomplish this. My code runs through the text file, ignores any decimals or non-numeric characters (NNC), and outputs Integers into a table.

 

I want to make this a bit more robust, the exercise never specifies how text in the file should be laid out, so I want to assume that having multiple Integers, Decimals/NNC on the same line can be accounted for. These would be separated by Whitespace.

 

My pseudo-code would be as follows:

 

  1. while Not at the end of the Stream
  2. create a StringBuilder
  3. while StreamReader.Peek is not a whitespace or null (should break on detecting whitespace)
  4. append the value at StreamReader.Read() to the string builder
  5. Try to parse this new string to an integer data type for output later.
  6. Repeat

My issue stems from points 3 and 4, the inner loop. Peek doesn't seem to recognise a whitespace value and the loop won't break.

 

while (!String.IsNullOrWhiteSpace(sr.Peek().ToString()))
{
       sb.Append((char)sr.Read());
}

sr is the StreamReader object. Is there something I'm missing here, or am I making it too hard on myself?

 

Please ask if you need any more information.

 

Many thanks,

 

Stitchs.

 

Update: I have stepped through the StringBuilder every time it is appended too, and have also found out that Read is also taking the \n and \r escape sequences into account. How would I account for these?


Problem with Programming Exercise; Loops within Loops

15 November 2014 - 02:25 PM

Hello,

 

I am working through a programming books exercises. I am on a chapter dealing with loops, and an exercise that makes use of inner loops. The aim of the exercise is to print a limited number of rows, each one printing a certain amount of spaces and stars:

 

-----*

----***

---*****

--*******

-*********

-*********

--*******

---*****

----***

-----*

 

I have come up with a solution, but it seems a bit messy to me and I cannot figure out how to clean it up.

int MAX_ROWS = 10;

for (int row = 1; row <= MAX_ROWS / 2; row++)
            {
                for (int space = row; space <= MAX_ROWS / 2; space++)
                {
                    Console.Write("-");
                }
                for (int star = 1; star <= row * 2 - 1; star++)
                {
                    Console.Write("*");
                }
                Console.WriteLine();
            }

            // Variable to subtract the number of stars to be printed on every run
            // of the second loop. This is decremented on each full run of the outer
            // loop.
            int starReducer = 5;

            for (int row = 6; row <= MAX_ROWS; row++)
            {
                for (int space = 1; space <= row - 5; space++)
                {
                    Console.Write("-");
                }
                
                for (int star = MAX_ROWS - 1; star >= row - starReducer; star--)
                {
                    Console.Write("*");
                }
                starReducer--;

                Console.WriteLine();
            }

(The dashes are meant to be spaces, but are used to show if the loop prints the correct number of characters).

 

My issue comes from the two nested loops dealing with drawing the stars. On the first 5 rows, I want the star count to be ascending, adding two more stars to each row, at a time. I currently do this by setting star count to one, and it loops until star count becomes <= (the current row * 2) - 1. This prints the required; * *** ***** etc. for each row.

 

The second sets star count to 9, and does a reverse loop that stops when star count >= the current row (starting with 6) - (a new variable to reduce the amount that the loop counts down each time) starReducer, which is decremented on every completion of said loop.

 

Is there a better, cleaner solution to this? It works but it bugs me that if I were to come back to this in a few days, it might not be as clear to me as it is now.

 

I appreciate anyone who takes the time to have a look.

 

Many thanks,

 

Stitchs.


C# trouble obtaining a decimal value.

04 October 2014 - 09:46 AM

Hi all,

 

I am working through book exercises for C#. And one of the questions is to convert a monetary amount, represented by a double.

 

I have encountered a small error when I try to extract the penny value from this Double value:

 

for example:

 

I input 47.32. I use the formula/code:

int penniesToConvert = (int)((moneyIn - Math.Truncate(moneyIn)) * 100);

This takes the double value, subtracts the Integer part, multiplies by 100 to make .32 into the Integer 32, and casts it to be placed in a new Integer type. This is then used for calculations later.

 

Until now, I assumed this was fine, and it gave me an Integer value of 32. I changed the penny value of the original double to test it; 13, 47, 63 etc.

 

It's only when I input .33, that the Integer I get back is 32. I extracted the part of moneyIn - Math.Trunc... (without multiply by 100) and when I look through the debugger, I get a value of .3299999999... Which, when multiplied and casted, becomes 32. The same happens with the value 66. Why is this, is there anyway to account for these, or is my method of extracting the penny value incorrect here.

 

On a sidenote, this is only in Chapter 2 of the book, so I can't imagine that the solution should be too complicated.

 

Any help on this would be much appreciated.

 

Stitchs.


Returning to Programming

14 June 2014 - 06:19 AM

Hi all,

 

So after a year out from programming, and the forums, I have decided to return to thing that I love!

 

I have been out of the loop for so long that I am effectively starting again. I want to start with a language that is easier to understand, and plug away at, than C++. It didn't work for me before, but I hope to go back to it at a later date.

 

I want to program, and I want to see results fast(er than C++), so I have decided to go with C#. Is this a good choice? What are good resources/books to start from the ground up. Is it worth going into XNA.

 

I know this sounds like a lot to ask, and I am not unfamiliar by any means with forum guidelines, but if there is any advice, I will gladly take it.

 

Thanks in advance for reading/replying,

 

Stitchs.


'Completed' Pong Project; looking for Critique.

29 December 2012 - 09:02 PM

Hey guys,

I have been working on this for about a month and half now, with two weeks spent on the first iteration and the last 3-4 on changes suggested by this wonderful community.

 

I have worked my way through the the list and am now (in loose quotations) finished. I am pleased with what I have learned on the way, and how I will take some of it forward. I also realise that my code was nowhere near what it should have been and have revised much of it.

 

There are still some issues:

  • Hard-coded/cumbersome state machine in Game.h (to be revised in next project)
  • Ball sprite appearing white (this is one I could use some advice on)
  • Game over text being highlighted incorrectly
  • Lack of sound/save functionality.
  • Players should probably by local to Game.h and opposed to Arena.h

 

 

Could I get you guys to try it out, and have a gander at the code as some new feedback would be excellent to take into the next project. Please find this below.

 

I write this late and will probably think of extra things, and add them tomorrow.

 

Regards,

 

Stitchs.


PARTNERS