Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What I am trying to make is a for loop that repeats a word 5 times and after the word, shows which cycle the word is on (1-5)

What I have tried:

using System;
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = (Console.ReadLine());
            for (int i = 0; i < 5; i++) ;
            {
                Console.WriteLine(i);
            }
        }
    }
}
Posted
Updated 23-Sep-17 4:17am

1 solution

Try this:
static void Main(string[] args)
{
    string inp = Console.ReadLine();
    for (int i = 0; i < 5; i++)
    {
        Console.WriteLine("{0}, {1}", inp, i + 1);
    }
}
ReadLine returns a string, not an integer, so you need to store it correctly. The variable you store the string in, shouldn't be the same name as your loop variable, or the system won't know which one you mean!
WriteLine can take a format string as its first parameter which describes what to print. In this case it says "print the first variable, a comma, then the second variable".
So the result of the loop (assuming you entered "Hello") would be:
Hello, 1
Hello, 2
Hello, 3
Hello, 4
Hello, 5
 
Share this answer
 
v2
Comments
Klaudia28 23-Sep-17 13:51pm    
It says that "i" doesn't exist in the current context
OriginalGriff 23-Sep-17 15:52pm    
My mistake - I based it on your code (so you'd recognise it) and forgot to remove a semicolon tat shouldn't be there.
Gone now.
Klaudia28 24-Sep-17 15:11pm    
It's still the same.
OriginalGriff 24-Sep-17 15:27pm    
Double check your code is identical to mine.

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