First off here is my code:

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

namespace example_1
{
    class Program
    {
        static void Main(string[] args)
        {
            decimal d = 24;
            string input;
            Console.WriteLine("Enter A number");
            input = Console.ReadLine();
            decimal total = Convert.ToDecimal(input), result = (d * total);
            Console.WriteLine(result);
            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("Would you like to repeat?");
            repeat = Console.ReadLine();
            Console.WriteLine();
            Console.WriteLine();
            Console.Read();
        }
    }
}

My Questions:

1. I'd like to add something like an if statement or some kind of loop after the

Console.WriteLine("Would you like to repeat?");

part.

Some thing like this.

repeat = Console.ReadLine();
if (repeat = y)
{
    // some code to start the process over
    // from the begining.
}
else (repeat = n)
{
 // some code to exit application
}

2. How would I implement a check to make sure the user inputs only a number for the first ReadLine part and only 'y' or 'n' for the second?

NOTE:
I've searched all over the web for Tutorials but most are plain code for C# Forms and I don't learn anything from them. And this suits my needs as it is quite simple to code.

Any and all help is appreciated.
Thanks in advance, Poab.

Recommended Answers

All 5 Replies

1. Add a do while loop

do{
// your code here

}while(repeat == "y")

2. first part use

int num;
bool isNumber = Int32.TryParse(intput, out num);

if(isNumber)
{
//this means the user input a number
}

the second part

bool isLetter = Char.isLetter(repeat[0]);

or use the readkey function and while its not Y or N.. keep reading.

or use the readkey function and while its not Y or N.. keep reading.

Ok LizR how would I about using the ReadKey Function would you mind showing me an example?

Good example comes in the help, just look at Console.ReadKey

Ok, Thanks worked like a charm.

Also, I decided to use Windows Form instead of console. Did this for a quick learning/lesson type thing.

Thanks LizR, and dickersonka.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.