Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to find out the easiest to use loop for an assignment in school. We are using C# 2012 and I need it to on the same line output a number representing which day it is (integer for the day, just needs to be a number - 1, 2, 3, 4, etc going down the lines) and to the right of each day number I need it to put out a decimal/double that starts with 0.01 and doubles each day (so day 2 would need to say 2 and then 0.02 spaced out enough to have a header showing "Day:" and "Amount"). End result needs to print out something similar to the below -

Day Amount Owed
--- -----------
1 0.01
2 0.02
3 0.04

needs to calculate until day 64


I have the below code so far, but it gives me errors for the variable i.

Error 1 Use of unassigned local variable 'i' f:\documents\visual studio 2012\Projects\RichBeggarBC\RichBeggarBC\Program.cs 18 23 RichBeggarBC

Same error is given even if I remove the = 0.01 from the double i = 0.01;.

C#
static void Main(string[] args)
        {
            //since the output will require a decimal, I am using a double as the variable type to grant more than 7 spaces
            double number;
            //used int for day
            int day = 1;
            double i = 0.01;

            Console.Write("Day:     Amount owed:\n");

            //for loop to run until number is made
            for (i = 0.01; day <= 64; i *= 2)
                Console.WriteLine (day);


            //console.readline used to sop program from finishing in order to allow it to remain open
            Console.ReadLine();
        }




I have also just tried a nested for loop and get errors when trying to use it.

C#
for (int i =1; i <= 64; i++)
{
    for (double j = 0.01; j * 2)
    {
        Console.WriteLine (i);
    }
    Console.WriteLine ();
}


This gives me 2 errors. I get 1 error saying it expected a ; after the j * 2) and error 2 says cannot change double into bool and highlights j * 2). I just want to make it so each line prints i (and increment by 1) and then j after it so that it would read like in the table in the original post.
Posted
Updated 29-Aug-15 8:22am
v3

Concerning the first loop
C#
for (i = 0.01; day <= 64; i *= 2)

The break condition for the loop is base on day variable. However, you don't increment the day at all.

Why not loop through the day and increment i inside the loop. So the loop would be
C#
for (day = 1; day <= 64; day++)


What comes to the second loop
C#
for (double j = 0.01; j * 2)

You should add the condition, how long the loop is continued. The loop always contains three parts:
C#
for (initializer; condition; iterator)

Now you're missing the condition thus the error
 
Share this answer
 
The issue is that I do not see a condition other than day = 64. Therefore would the 2nd loop be able to be -

for (double j = 0.01; day <= 64; j * 2)

Also, would this keep it incrementing with the day so that day 2 = 0.02, day 3 = 0.04, etc?
 
Share this answer
 
v2
Just an update. I made the below changes and I got it to run without the console.writeline (day) - however it only ran 1 for the day and nothing for the rest of the application and it continued for infinity. I need it to add the total of the 0.01 for day 1 and multiplied by 2 each day for a 2nd "column" to the right of the day number (about 5-6 spaces away). I am not sure why it is not incrementing to a limit of 64 on the day and why it is not writing the line for the increment.

C#
for (day = 1; day <= 64; day++)
{
    for (double j = 0.01; day <= 64; j *= 2)
    {
        Console.WriteLine (day);
    }
    Console.WriteLine ();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900