-8

I tried to make a FizzBuzz code in C# but the error code it gives me is that I'm missing a " , " somewhere but I can't find where to place it Also I know there are other underlying programs in the code I just need this fixed so I can compile and fix them

using System;

namespace ConsoleApp5
{
    class Program
    {
        static void Main(string[] args)
        {
            float i = 0;
            if (i != 101)
            {
                i = i + 1;
                float i3 = i / 3;
                float i5 = i / 5;
                float i15 = i / 15;
                string Print = Convert.ToString(i)
                else ; if ((i3 %1) > 0)
                {
                   string Hold = ("Fizz");
                    Print = Hold;
                }
                else if ((i3 % 1) > 0)
                {
                    string Hold = ("Buzz");
                    Print = Hold;
                }
                else if ((i15 % 1) > 0)
                {
                    string Hold = ("FizzBuzz");
                    Print = Hold;
                }
                Console.WriteLine(Print)
                ; Console.WriteLine("Done");
                Console.ReadLine();


            }
        }
    }
}
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • What is the exact error message and where does it say it happens? Yes there are many problems here and I’m sure your compiler gives them out so you can start from the first one and move onwards. – Sami Kuhmonen Mar 11 '18 at 04:50
  • Please tell me you have a good reason for variable names `i`, `ii`, `iii`, `iiii`, `f`, `d`, etc. Meaningful names make debugging easier, this is a mess. For example, the line `else ; if ((ii %1) > 0)` all by itself won't compile, neither will the line above it. – Ron Beyer Mar 11 '18 at 04:51
  • The only problem I get is CS1003 C# Syntax error, ',' expected – user9407437 Mar 11 '18 at 04:51
  • 3
    You have more problems then that – TheGeneral Mar 11 '18 at 04:52
  • Well it dosen't tell me anything else, It only gives me the you need a "," error or CS1003 C# Syntax error, ',' expected – user9407437 Mar 11 '18 at 04:54
  • C# gives you specific line number information when it encounters an error, as well as a complete error message. You've provided neither. Also,step back and **read** the code you've written. I can see at least two very obvious syntax errors that jump out at you with a brief glance. – Ken White Mar 11 '18 at 04:59
  • Well i guess you have code syntax problem, take a look [here](http://rextester.com/VKJFY80621) and tell if it works in your code – Agent_Orange Mar 11 '18 at 05:00
  • 1
    @Agent_Orange Your code has errors too, the variables created in the first `if` scope are not visible in the `else` scope you created. – Ron Beyer Mar 11 '18 at 05:02
  • What is the purpose of the semicolon here?: `else ; if ((i3 %1) > 0)`? – ProgrammingLlama Mar 11 '18 at 05:03
  • 1
    @john, this is one of the syntax mistakes i am pointing out – Agent_Orange Mar 11 '18 at 05:04
  • @Ron Beyer, ROGER ! – Agent_Orange Mar 11 '18 at 05:04
  • 3
    Stop commenting on this post. The poster should be making an effort to solve the problem, and at the very least should be posting the specific error message and line location. There is no benefit in continuing to add noise to the comments here. – Ken White Mar 11 '18 at 05:20
  • Build early and build often. If you've added so much code that you can't find your syntax error, roll back to the last good version and try adding less. – John Wu Mar 11 '18 at 06:20

1 Answers1

3

Fizz-Buzz was originally a game designed to teach children division, it worked like this:

The player designated to go first says the number "1", and each player counts one number in turn. However, any number divisible by three is replaced by the word fizz and any divisible by five by the word buzz. Numbers divisible by both become fizz buzz.

So your program has a number of errors, first one is that you use an if where you should be looping. Your first if statement:

if (i != 101)
{ ... }

Really doesn't do anything. You set i=0 in the previous statement, so i will never equal 101. What you need to do instead is a while loop:

float i = 0.0f;
while (i < 101.0f)
{
   //Run the program
}

The next problem you have is that it is OK to use i for an iterator, or even x or y if iterating dimensions, but that is really where the single letter variables should stop. Use meaningful names, it makes things much easier:

So, again we need to check if i is divisible by 3, 5, or both. We can do that with simple boolean variables, no need to make things more complicated.

bool divisibleBy3 = i % 3.0f == 0.0f;
bool divisibleBy5 = i % 5.0f == 0.0f;

The next thing you have wrong is that you have ; in strange places, namely you seem to mix them in on separate lines. Try not to do this. There are very few reasons that a ; should not be on the end of every code line, and there should really only be one per line. So this:

string Print = Convert.ToString(i)
else ; if ((i3 %1) > 0)

Is an error because it treats it all as one line until it hits the ;, so your code really becomes:

string Print = Convert.ToString(i) else;
if (...)

And it should be obvious what the problem with that is.

The last problem I'll touch on really isn't a code issue, but a form one. You have a lot of "holding" variables that don't do anything but temporarily put things in places then put them somewhere else, like this:

if ((i3 %1) > 0)
{
   string Hold = ("Fizz");
    Print = Hold;
}

What is the purpose of Hold? You could just write:

Print = "Fizz";

The ( and ) are also unnecessary. So lets take all these lessons and put them into the Fizz-Buzz program:

int i = 0; //No reason to use float here, int is just fine
while (i <= 100)
{
    bool divisibleBy3 = i % 3 == 0;
    bool divisibleBy5 = i % 5 == 0;

    if (divisibleBy3 && divisibleBy5)
        Console.WriteLine("FizzBuzz");
    else if (divisibleBy3)
        Console.WriteLine("Fizz");
    else if (divisibleBy5)
        Console.WriteLine("Buzz");
    else
        Console.WriteLine(i.ToString());

    i += 1;
}
Console.WriteLine("Done");
Console.ReadKey(true);

Or, it can be written with a for loop:

for (int i = 0; i <= 100; i++)
{
    bool divisibleBy3 = i % 3 == 0;
    bool divisibleBy5 = i % 5 == 0;

    if (divisibleBy3 && divisibleBy5)
        Console.WriteLine("FizzBuzz");
    else if (divisibleBy3)
        Console.WriteLine("Fizz");
    else if (divisibleBy5)
        Console.WriteLine("Buzz");
    else
        Console.WriteLine(i.ToString());
}
Console.WriteLine("Done");
Console.ReadKey(true);

So you can see how giving variables meaningful names, paying attention to indenting/formatting, and understanding of the ; can help you make debugging easier. Clean, well formatted code is easy to read and debug, and giving variables meaningful names means you can tell what the purpose is without having to read through the entire use of the variable.

Note: Some programmers will argue that Fizz-Buzz can be condensed down to 1-3 lines of code. While it is possible, I would argue that it doesn't demonstrate good programming practices. There is a big difference between readable code that can be maintained, and just making something short for the sake of it being short.

Ron Beyer
  • 11,003
  • 1
  • 19
  • 37