I have this counter programm based on object oriented programming in C# and the counter has an Increment Method which counts up so like it goes from 0..1...2.. to 30.
The for loop was working perfectly fine but then I decided to add a class with two Methods that either count up in odd or even steps which also works fine. The only problem is that after changing the main class now it doesn't work in a loop anymore until its on 30, so I have to enter every step individually.
This is my main code (I'm pretty sure the problem is due to the Console.ReadLine and input in general but I don't know what exactly is the issue)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Counter
{
class Program
{
static void Main(string[] args)
{
char confirm;
int input;
do
{
//int inNum;
Console.Clear();
ModusCounter b = new ModusCounter(0, 30);
for (; ; )
{
b.Display(19, 7);
Console.Write("do you want (1)odd // (2) even // (0)standard ? ");
input = Convert.ToInt32(Console.ReadLine());
if (input == 1)
{
b.OddCount();
}
else if (input == 2)
{
b.EvenCount();
}
else
b.Increment();
System.Threading.Thread.Sleep(300);
if (Console.KeyAvailable) break;
}
//b.Increment();
b.Display(19, 7);
Console.WriteLine();
Console.WriteLine();
Console.WriteLine("do you want to restart? (J // N) ");
confirm = Convert.ToChar(Console.ReadLine());
}
while (confirm == 'J');
Console.ReadLine();
}
}
}
ModusCounter:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Counter
{
public class ModusCounter:CCounter
{
private int wholenum;
public int Wholenum
{
get => wholenum;
set
{
wholenum = value;
}
}
public ModusCounter(int start, int limit) : base(start, limit)
{ }
public void EvenCount() //2
{
Wholenum = 2;
Level += Wholenum;
if (Level % 2 == 0)
{
Level += Wholenum - 2;
}
else
{
Level += Wholenum -1;
}
if (Level > Limit)
Level = Start;
}
public void OddCount() //1
{
Wholenum = 1;
Level += Wholenum;
if (Level % 2 == 0)
{
Level += Wholenum;
}
else
{
Level += Wholenum + 1;
}
if (Level > Limit)
Level = Start;
}
}
}
base class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Counter
{
public class CCounter
{
private int start;
private int limit;
private int level;
public int Start
{
get => start;
protected set
{
start = value;
}
}
public int Limit
{
get => limit;
protected set
{
limit = value;
}
}
public int Level
{
get => level;
protected set
{
if (value < Start)
level = Start;
else if (value > Limit)
Level = Limit;
else
level = value;
}
}
public CCounter(int start_, int limit_)
{
Start = start_;
Limit = limit_;
Level = Start;
}
public void Increment()
{
int level = Level + 1;
if (level > Limit9
Level = Start;
else
Level = level;
}
public void Display(int x_, int y_)
{
Console.SetCursorPosition(x_, y_);
Console.Write(Level.ToString("00"));
}
}
}