Ask coding questions

← Back to all posts
help with homework
MaureenFlannery (0)

Can someone please tell me what i am dong wrong with this?

This is C#. If I declare "i", i get an error. If I don't declare "i", i get a different error. See the comments below to see the errors.

using System;

class MainClass {
public static void Main (string[] args) {
int i = 0;

// You can reuse i for every problem. Just reassign a value to it. i = 0; Do not try to redeclare it.

Console.WriteLine("\nProblem 1 \n--------------------------------------");
// Problem 1 - (FOR LOOP REQUIRED) Create a 12-element array called months.  Set each element to the name of each month. For example months[0] = “January”.  Use a for loop to display the number and name of each month.

string[] Months = new string[] {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

for (i < 11; i++)
{
  Months[i]= i;
}

}
}

Comments
hotnewtop
MaureenFlannery (0)

i got it! Thank you all so such for your help!!!

using System;

class MainClass {
public static void Main (string[] args) {

string[] Months = new string[12];
Months[0] = "January";
Months[1] = "February";
Months[2] = "March";
Months[3] = "April";
Months[4] = "May";
Months[5] = "June";
Months[6] = "July";
Months[7] = "August";
Months[8] = "September";
Months[9] = "October";
Months[10] = "November";
Months[11] = "December";

for (int i = 0; i < 12; i++)
{
Console.WriteLine("Month :" + Months[i] + " and number: " + i);

}

}
}

fuzzyastrocat (1867)

There are actually multiple issues in this program, so let's look at each one.

First, a for loop needs 3 things: something it does first, something it checks to see if it should keep looping, and something it does at the end of each loop. Your for loop only has two items however — the expressions given suggest that it lacks something to do first.

However, in your case it appears that it doesn't actually need anything to do first, so you can actually just leave that space blank — the error originates from the fact that you still have to put a semicolon even if you leave it blank. So, a proper loop would be the following (assuming this is the behavior your desire; this style is highly unconventional):

//  ↓ Note how nothing is put in this place
for (; i < 11; i ++)

Now that we have that done, you'll probably get some error about type mismatch (string and int). That's from Months[i] = i;. Think about what you're doing there: you've declared Months[] as an array of all the month names — strings — but in the loop, you try to overwrite each month name with its corresponding number — ints. If that were valid, you'd end up with Months[] being equal to { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } after the loop was done. It isn't valid, though, because you can't set a string value equal to a number. It's like saying "I have a basket that holds only apples (strings). Put a grapefruit (int) in the basket." The computer just doesn't know what to do.

Therefore, I'm not really sure what you're trying to do here, so I can't really help you fix it since I don't know what the desired outcome is.

If you're trying to solve problem 1, why do you need to overwrite the month names with their numbers? Think about what the problem is asking — to print out the month names and corresponding numbers — and how you'd do that for just a single month. If you had a month name stored in the variable a_month and its number in the variable number_of_month, how would you print out the given month name and number?

Once you've solved that, you can use that code inside the loop (replacing the Months[i]= i;) and just use Months[i] (the given month) instead of the fake name a_month and i (the given number) instead of the fake name number_of_month.

SixBeeps (5332)

Can you please link a Repl? If not, wrap the code in a codeblock.

Aivoybia (24)

is this java? if it is, i think its supposed to be System.out.println not Console.WriteLine

Also, you need to have the i variable in the loop statement.

for (int i = 0; i < 11; i++) {

MaureenFlannery (0)

@MEadphonesmead

it's C#. if you look in the comments below here, i get an error when i declare 'i' in the loop.

Aivoybia (24)

@MaureenFlannery ok try adding the int i = 0
it probably wont fix it cuz I don't know c# but just try it

MaureenFlannery (0)

@MEadphonesmead
when i try this. . .

using System;
/*

  • Lab 6 (really, it is lab 4)
  • Maureen Flannery
    */
    class MainClass {
    public static void Main (string[] args) {
    int i = 0;

    // You can reuse i for every problem. Just reassign a value to it. i = 0; Do not try to redeclare it.

    Console.WriteLine("\nProblem 1 \n--------------------------------------");
    // Problem 1 - (FOR LOOP REQUIRED) Create a 12-element array called months. Set each element to the name of each month. For example months[0] = “January”. Use a for loop to display the number and name of each month.

    string[] Months = new string[12];
    Months[0] = "January";
    Months[1] = "February";
    Months[2] = "March";
    Months[3] = "April";
    Months[4] = "May";
    Months[5] = "June";
    Months[6] = "July";
    Months[7] = "August";
    Months[8] = "September";
    Months[9] = "October";
    Months[10] = "November";
    Months[11] = "December";
    /*string[] Months = new string[] {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    */
    for ( int i = 0; i < 11; i++)
    {
      Months[i]= i;
    }

    }
    }

i get this error message

 mcs -out:main.exe main.cs
main.cs(31,15): error CS0136: A local variable named i' cannot be declared in this scope because it would give a different meaning toi', which is already used in a `parent or current' scope to denote something else
Compilation failed: 1 error(s), 0 warnings
exit status 1

Aivoybia (24)

@MaureenFlannery and delete int i = 0 in the beginning on the code

MaureenFlannery (0)

@MEadphonesmead

if i do that . . .

using System;
/*

  • Lab 6 (really, it is lab 4)
  • Maureen Flannery
    */
    class MainClass {
    public static void Main (string[] args) {

    // You can reuse i for every problem. Just reassign a value to it. i = 0; Do not try to redeclare it.

    Console.WriteLine("\nProblem 1 \n--------------------------------------");
    // Problem 1 - (FOR LOOP REQUIRED) Create a 12-element array called months. Set each element to the name of each month. For example months[0] = “January”. Use a for loop to display the number and name of each month.

    string[] Months = new string[12];
    Months[0] = "January";
    Months[1] = "February";
    Months[2] = "March";
    Months[3] = "April";
    Months[4] = "May";
    Months[5] = "June";
    Months[6] = "July";
    Months[7] = "August";
    Months[8] = "September";
    Months[9] = "October";
    Months[10] = "November";
    Months[11] = "December";
    /*string[] Months = new string[] {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
    */
    for ( int i = 0; i < 11; i++)
    {
      Months[i]= i;
    }

    }
    }

then, i get this error message . . .
 mcs -out:main.exe main.cs
main.cs(33,18): error CS0029: Cannot implicitly convert type int' tostring'
Compilation failed: 1 error(s), 0 warnings
exit status 1

MaureenFlannery (0)

if i change:
for (i < 11; i++)
{ Months[i]= i;}

to become:
for ( int i = 0; i < 11; i++)
{
Months[i]= i;
}

then, i get this error message:

 mcs -out:main.exe main.cs
main.cs(18,15): error CS0136: A local variable named i' cannot be declared in this scope because it would give a different meaning toi', which is already used in a `parent or current' scope to denote something else
Compilation failed: 1 error(s), 0 warnings
exit status 1

MaureenFlannery (0)

i keep getting this error message:
 mcs -out:main.exe main.cs
main.cs(18,20): error CS1525: Unexpected symbol `}'
Compilation failed: 1 error(s), 0 warnings
exit status 1
 bad file descriptor