user3299197
user3299197

Reputation: 115

How would I list the number of weeks and days in C#?

I actually really want to learn to do this for javascript in SharePoint purposes, but I'm more familiar with C# so I wanted to do the logic here first. I know I need to account for things less than 7 days and change days to day when the week = 1, etc, but my issue is that I get weird values when setting my integers equal to whatever the user has entered.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int days_ = 0;
            int Weeks = 0;
            int Days = 0;

            Console.WriteLine("How many days are left?");
            days_ = Convert.ToInt32(Console.Read());
            Weeks = days_ / 7;
            Days = (days_ % 7);
           // Console.WriteLine(days_ / 7);
           // Console.WriteLine(Weeks);
            Console.WriteLine((42 % 7)+"== 42%7");
            Console.WriteLine("{0} Weeks and {1} Days",Weeks,Days); 
            Console.ReadKey();
        }
    }
}

If I explicitly enter 42 % 7 into the code, I get 0.. but if I enter 42 as the value of days_, it keeps saying 42 % 7 = 3. I also have trouble with the weeks integer saying that things like 60/7 = 7. Again, the main goal of this is just to have the logic spelled out in my head before I try to duplicate the process in SharePoint through javascript/jquery. I'm pretty novice at development outside of basic html, but I didn't think my C# was this bad to where I can't make a simple console app work properly.

Upvotes: 0

Views: 78

Answers (1)

tbrass86
tbrass86

Reputation: 76

Console.Read() reads per character, so '_days' is only reading the '4'. Obviously, 4 % 7 = 3.

Try Console.ReadLine

Per hatchet, which makes more sense:

You may want to edit to add that Read returns an int. '4' returns the ASCII code 52. 52 % 7 = 3, and 52 / 7 = 7

A Convert.ToInt could also be your friend after the input read.

Upvotes: 4

Related Questions