473,395 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How could I display results of an operation to a text file temporarily?

I am writing my own script for a simple calculator that determines if a number is prime, and its prime factors. Currently my code looks like:

Expand|Select|Wrap|Line Numbers
  1.  Private Sub primebutton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles primebutton.Click
  2.         Dim oWrite As System.IO.StreamWriter
  3.         oWrite = File.CreateText("C:\TempFactors.txt")
  4.         TestLabel.Text = ""
  5.         'Determines perfect square qualifications on following 5 lines
  6.         Dim root As Double
  7.         Dim compare As Integer
  8.         root = System.Math.Sqrt(answer)
  9.         compare = CInt(CLng(root) \ 1)
  10.         If root = compare Then TestLabel.Text = "Perfect Square Composite (Not Prime)"
  11.         'Determines if number is prime
  12.         Dim n1 As Double
  13.         Dim n2 As Double
  14.         Dim n3 As Double
  15.         Dim loopcount As Double
  16.         n3 = compare / 1
  17.         loopcount = compare - 2
  18.         Do Until loopcount = 0
  19.             n1 = answer / n3
  20.             n2 = CLng(n1) \ 1
  21.             If n1 = n2 Then oWrite.Write(n2.ToString, n3.ToString)
  22.             End
  23.             loopcount = loopcount - 1
  24.         Loop
  25.         oWrite.Close()
  26.     End Sub
The loopcount = compare - 2 means I want the loop to be 2 less than the square root of the answer so that when do a descending calculation (loopcount = loopcount - 1) it doesn't see if 1 or 0 is a factor of the answer. for the calculations after that, I want, every time n1 = n2 (a whole number factor of the answer), I want to report those results to a text file (n2 and n3) in this format "(n2, n3)" then go to the next line. and continue to execute the loop until the loopcount is zero. Currently, when I run the code, the application closes when I click the prime button (button that runs this code) and I don't know why. This is my first semester in school, and have never coded before, so I am a definite laymen with code, but I really really want to learn it (hence why for fun im just creating algorithms for different things). Please help!! What did I do wrong and how can I fix this problem. Thanks in advance.

-Nick
Oct 21 '09 #1
2 1214
tlhintoq
3,525 Expert 2GB
But I can make some suggestions.
  • Assume that everything is broken, or at least not ideal.
  • Presume that the user is going to provide data in a format or manner that you just didn't expect. If you use a textbox for a number, the user will type "One".
  • Assume that hardware breaks in the middle of what you are doing, so you have to recover.
  • Take a few extra lines of code to get standards like the boot drive, the number thousands seperator etc. Don't assume that you have a C: drive or that a comma is the separator because not everyone is in America.
  • Check that folders exist, even if you just did a call to make it: You may not have permissions.
  • Get used to placing breakpoints and walking through the code line by line. Double check EVERYTHING on every line. Keep the "Locals" and "Autos" windows open so you can see your values.
    • Put a breakpoint on line 1.
    • When the code stops there, walk through line by line with F-10.
    • Check the values of your assumptions.
  • Stop. Breath. Relax. Then reason out the problem. Cut it down by sections or halves. "The value was good here, then at this method it wasn't. Where did it go between 'A' and 'B'?"
  • Range check and validate values. Confirm that you didn't get a zero when you are only set to accept 1-10. Confirm your objects and values aren't null. Initialize them to a known default if possible. If a selection can be from 0-10, then initialize to -1: Now you have something to check for.
Oct 21 '09 #2
tlhintoq
3,525 Expert 2GB
TIP: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.
Oct 21 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Follower | last post by:
Hi, I am working on a function to return extracts from a text document with a specific phrase highlighted (i.e. display the context of the matched phrase). The requirements are: * Match...
7
by: Winston | last post by:
Hi, I want to use the Webbrowser Control (the Internet Explorer Control) to display an XML-structure within a C# WinForm. The way the IE displays a XML-file in its native form is exactly what I...
6
by: Mark | last post by:
Hello all, I have the following file.txt storing these kind of values: Name: John Doe E-mail: john@doe.com Phone: 555 865 8901 Address: 71 Huntington Ln. (33444) FL - Delray Beach US Member...
2
by: BluNuit | last post by:
I have a simple c# app that calls a Java (AXIS) web service to perform some operations. The call works fine (the java code fires and the operations are executed), but the response is always null. ...
4
by: Jono | last post by:
Hi Everyone, As it says in the title, I'm looking for a way to display a page while long running operations are performed on the server. Ideally, I'd like some way to push the current request...
4
by: murrayatuptowngallery | last post by:
I started a JS project (my first) a year or two ago, got distracted & am just now getting back to it. By luck I had somehow found a way to collect data with a form, perform calculations with...
1
by: aziziahmad | last post by:
hello..i'm trying to display my result in text file.. how am i going to display from printf command to display at text file? this is the example of of line need to be display in text file = ...
3
by: Chris McFarling | last post by:
What's the most efficient method to export the result set of a SQL query to a text file using ASP.NET? There could potentially be a few thousand rows.
9
by: tshad | last post by:
I have a Windows App that is doing some work and then writing a "Now Processing..." line to the status line of the window as well as the Textbox on the form. But the problem is that the work is...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.