11 Replies - 1307 Views - Last Post: 15 February 2015 - 11:29 AM Rate Topic: -----

#1 fbodieslive  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-February 15

Need help with loops.

Posted 14 February 2015 - 07:08 PM

Hello,
I am new here. I am a marketing major and am taking an "Intro to programming" class as an elective. I picked it because I thought it would be fun and easy, but I was wrong.

I have to code a GUI that calculates distance (speed*time) using a loop and display the results in a listbox.

Here is the code I have, so far. I am trying to follow along with my book, but I don't know what else to try.

namespace HW5CH5_1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void calculateButton_Click(object sender, EventArgs e)
        {
            int speed;
            int time;
            int count = 1;
            int distance;

            if (int.TryParse(speedTextBox.Text, out speed))
            {
                if (int.TryParse(timeTextBox.Text, out time))
                {

                    while(count<=time)
                    {
                        distance=(speed*time);

                        outputListBox.Items.Add(distance.ToString);

                        count=count+1;
                    }
        }
        

                       


It keeps telling me I can't use the To.String method on the listbox. Says I can't convert method to an object.

This post has been edited by macosxnerd101: 14 February 2015 - 07:13 PM
Reason for edit:: Please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Need help with loops.

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5078
  • View blog
  • Posts: 13,705
  • Joined: 18-April 07

Re: Need help with loops.

Posted 14 February 2015 - 07:17 PM

No problem, you just forgot to add parenthesis on your toString call...

outputListBox.Items.Add(distance.ToString()); //<--- Notice the extra parenthesis



You have to make sure you add empty parenthesis anytime you call a method that takes no parameters. Otherwise it thinks you are referring to a property or object when you are really calling a method and its return value (a string in this case) is what is being added to the listbox items.

:)
Was This Post Helpful? 0
  • +
  • -

#3 fbodieslive  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-February 15

Re: Need help with loops.

Posted 14 February 2015 - 07:34 PM

Thanks a lot! I another question. In my book, when the user inputs (3) for hours traveled and (40) for the MPH it shows 3 different values in the listbox. It shows the value for the 1st hour which would be (1*40)=40 then (2*40)=80 then (3*40)=120. If you input these sames values in my GUI it just lists 120 three times. How do I fix that? Here is my code.
  private void calculateButton_Click(object sender, EventArgs e)
        {
            int speed;
            int time;
            int count = 1;
            int distance;

            if (int.TryParse(speedTextBox.Text, out speed))
            {
                if (int.TryParse(timeTextBox.Text, out time))
                {

                    while (count <= time)
                    {
                        distance = (speed * time);

                        outputListBox.Items.Add("After hour" + count + "the distance is" + distance.ToString());

                        count = count + 1;
                    }

                }
                else
                {
                    MessageBox.Show("Invalid time");
                }

            }
            else
            {
                MessageBox.Show("Invalid speed");
            }
        }


This post has been edited by macosxnerd101: 14 February 2015 - 08:08 PM
Reason for edit:: Please use code tags

Was This Post Helpful? 0
  • +
  • -

#4 Soulyy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 30
  • Joined: 12-February 15

Re: Need help with loops.

Posted 14 February 2015 - 08:51 PM

Your error lies in this line
distance = (speed * time);

You should use count to calculate the distance for each hour rather than the total time. One of the easiest ways to discover these types of errors is to debug your application and step through it.
Was This Post Helpful? 0
  • +
  • -

#5 fbodieslive  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-February 15

Re: Need help with loops.

Posted 14 February 2015 - 10:07 PM

View PostSoulyy, on 14 February 2015 - 08:51 PM, said:

Your error lies in this line
distance = (speed * time);

You should use count to calculate the distance for each hour rather than the total time. One of the easiest ways to discover these types of errors is to debug your application and step through it.

I'm not getting errors it just isn't doing what I want. How do I go about counting each hour?
Was This Post Helpful? 0
  • +
  • -

#6 Soulyy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 30
  • Joined: 12-February 15

Re: Need help with loops.

Posted 14 February 2015 - 10:44 PM

View Postfbodieslive, on 14 February 2015 - 10:07 PM, said:

I'm not getting errors it just isn't doing what I want. How do I go about counting each hour?

Yes, and one of the easiest ways to figure out why your program isn't working correctly is to debug it. Knowing how to do this will save you a lot of team even if you are only going to be taking this one class.

while (count <= time)
{
     distance = (speed * time);
     outputListBox.Items.Add("After hour" + count + "the distance is" + distance.ToString());
     count = count + 1;
}



You are already count each hour with this loop. Your loop will continue to loop until the variable "count" is greater than the variable "time" (or WHILE count is less than or equal to time). So if time = 3 the block of code in the while loop will execute 3 times. Of those 3 executions the variable "count" will have 3 different values for each execution and these values represent each hour.

https://msdn.microso...y/2aeyhxcd.aspx
Was This Post Helpful? 2
  • +
  • -

#7 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,377
  • Joined: 02-June 10

Re: Need help with loops.

Posted 15 February 2015 - 06:09 AM

Its clear your teacher does what most do and expects you to code - but (s)he hasn't bothered to first show you how to debug your programs in Visual Studio. I don't really know how they get their credentials with this thinking. Please see the debugging tutorials listed below: They will put you head and shoulders above your competition in class.

See FAQ # 2 "How do I debug my program?"

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 6: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated December 2014
Spoiler

Was This Post Helpful? 0
  • +
  • -

#8 fbodieslive  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-February 15

Re: Need help with loops.

Posted 15 February 2015 - 08:00 AM

Can debugging help if you don't have an error? My code works it just doesn't do what I need it to do. Your right, my professor just told us to hit F5 and see if the program works.

This post has been edited by andrewsw: 15 February 2015 - 08:37 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#9 Michael26  Icon User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: Need help with loops.

Posted 15 February 2015 - 08:39 AM

Debugging helps you to see how code "flows" in memory, it helps you to see every increment, every line you execute and its result.
It's not only to fix errors.
Was This Post Helpful? 1
  • +
  • -

#10 fbodieslive  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 14-February 15

Re: Need help with loops.

Posted 15 February 2015 - 10:46 AM

So, how do I go about solving my issue? I tried replacing "time" in this statement "while (count <= time)" with a number, but it didn't help. I pretty much copied the tutorial out of my book since it is, so similar to the actual problem, but the way the results display is different than the tutorial in the book.

Thank you all for your helpful answers thus far.
Was This Post Helpful? 0
  • +
  • -

#11 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,377
  • Joined: 02-June 10

Re: Need help with loops.

Posted 15 February 2015 - 11:29 AM

Quote

So, how do I go about solving my issue?
Listen carefully... DEBUGGING IS HOW YOU SOLVE YOUR ISSUE. Its been said at least 10 times now.

You have to debug. If you had done the tutorials you would get that by now. You have to walk through the running code one line at a time and confirm the actual values of your variables and compare them to the expected values. When the actual value does not match the expected value you have just found the flaw in your logic or implementation.

The next time you post a question I expect to see a screen shot of your Visual Studio WHILE RUNNING showing the locals or watch pallet with the run-time values of the variables in question. If that isn't part of your next post then it will be clear that you're not interested in learning the skills required for programming and I will close this thread as "gimme the codez" because its quickly becoming a case of you whining until someone posts fixed code for you.

This post has been edited by tlhIn`toq: 15 February 2015 - 11:33 AM

Was This Post Helpful? 0
  • +
  • -

#12 Soulyy  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 30
  • Joined: 12-February 15

Re: Need help with loops.

Posted 15 February 2015 - 11:29 AM

View Postfbodieslive, on 15 February 2015 - 10:46 AM, said:

So, how do I go about solving my issue? I tried replacing "time" in this statement "while (count <= time)" with a number, but it didn't help. I pretty much copied the tutorial out of my book since it is, so similar to the actual problem, but the way the results display is different than the tutorial in the book.

Thank you all for your helpful answers thus far.

Ask yourself a few questions:
1. Why am I using this loop?
2. What is the variable "count" counting?
3. What values could count possibly have?

Also try this:
1. Select the line "count = count + 1;" and press F9 to toggle breakpoints. Breakpoints pause your program when the line with the breakpoint is reached
2. Press F5 to debug the program
3. Select Visual Studio and select Debug > Windows > Locals
4. Press F10 to step over each line of your code while examining the locals window

As I said before your error lies in calculating the distance, the loop is fine as it is. Your code doesn't produce any errors because the error in your code is a human error.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1