Click here to Skip to main content
14,334,621 members
   

C#

 
AnswerRe: New to Coding New to Code Project Pin
Ger Hayden24-Jun-19 21:51
memberGer Hayden24-Jun-19 21:51 
AnswerRe: New to Coding New to Code Project Pin
Richard MacCutchan24-Jun-19 22:24
protectorRichard MacCutchan24-Jun-19 22:24 
AnswerRe: New to Coding New to Code Project Pin
#realJSOP26-Jun-19 6:32
mve#realJSOP26-Jun-19 6:32 
AnswerRe: New to Coding New to Code Project Pin
Gerry Schmitz26-Jun-19 8:53
mveGerry Schmitz26-Jun-19 8:53 
QuestionWhat is wrong with my code in Visual Studios Pin
Member 1451091724-Jun-19 7:46
memberMember 1451091724-Jun-19 7:46 
AnswerRe: What is wrong with my code in Visual Studios Pin
Richard Deeming24-Jun-19 8:39
mveRichard Deeming24-Jun-19 8:39 
GeneralRe: What is wrong with my code in Visual Studios Pin
Member 1451091724-Jun-19 9:12
memberMember 1451091724-Jun-19 9:12 
AnswerRe: What is wrong with my code in Visual Studios Pin
OriginalGriff24-Jun-19 19:56
protectorOriginalGriff24-Jun-19 19:56 
I'm glad Richard has fixed that for you, but would you mind a suggestion?

Your code lacks what we call "validation" - which means that you don't check what the user types at all. As a result, he can enter anything he likes for "Age" and you will accept it - even if it isn't a number.
Instead of "trusting the user to get it right" it's a good idea to validate the input and check that what he entered is legit, or you will get problems later on. (It doesn't really matter for this code, but if you start to expand it then it will cause you grief).
So let's check that his Age is actually a number:
int age;
string ageInput;
do
    {
    Console.WriteLine("What is your age: ");
    ageInput = Console.ReadLine();
    } while (!int.TryParse(ageInput, out age));
What that does is sets up a loop:
do 
   {
...
   } while (condition);
which continues to go round until condition is no longer true.
Inside the loop, we prompt the user and read his response:
do
    {
    Console.WriteLine("What is your age: ");
    ageInput = Console.ReadLine();
    } while (...);
And finally we try to convert his input to an integer:
            do
                {
...
                } while (!int.TryParse(ageInput, out age));
int.TryParse is a standard method which takes two parameters: the input string and a place to put the result (don't worry about the word out in there - you'll come to that later), then returns a boolean value (true or false) which says "It converted OK" (true) or "That wasn't an integer he entered" (false). We put an exclamation mark in front of that to "reverse the decision" so that true becomes false and vice versa, so the loop goes round until we do get an integer.

Now, you can do maths with his input:
Console.WriteLine($"{name}, is {age} years of age and he works as a {occupation}.");
Console.WriteLine($"Next year, {name} will be {age + 1} years old.");
because you have validated his input and are sure it's a "real number".
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!

AnswerRe: What is wrong with my code in Visual Studios Pin
Ayush Singroul28-Jun-19 10:16
memberAyush Singroul28-Jun-19 10:16 
Questionadministratively scalable Pin
Tara1123-Jun-19 9:46
memberTara1123-Jun-19 9:46 
AnswerRe: administratively scalable Pin
Dave Kreskowiak23-Jun-19 10:07
mveDave Kreskowiak23-Jun-19 10:07 
AnswerRe: administratively scalable Pin
OriginalGriff23-Jun-19 19:58
protectorOriginalGriff23-Jun-19 19:58 
QuestionPerformance vs scalability Pin
Tara1123-Jun-19 6:34
memberTara1123-Jun-19 6:34 
AnswerRe: Performance vs scalability Pin
Dave Kreskowiak23-Jun-19 9:36
mveDave Kreskowiak23-Jun-19 9:36 
AnswerRe: Performance vs scalability Pin
#realJSOP26-Jun-19 6:34
mve#realJSOP26-Jun-19 6:34 
GeneralRe: Performance vs scalability Pin
Tara1126-Jun-19 9:37
memberTara1126-Jun-19 9:37 
QuestionWhy is this not CLS-Compliant? Pin
#realJSOP20-Jun-19 4:51
mve#realJSOP20-Jun-19 4:51 
AnswerRe: Why is this not CLS-Compliant? Pin
OriginalGriff20-Jun-19 5:02
protectorOriginalGriff20-Jun-19 5:02 
GeneralRe: Why is this not CLS-Compliant? Pin
#realJSOP20-Jun-19 5:06
mve#realJSOP20-Jun-19 5:06 
GeneralRe: Why is this not CLS-Compliant? Pin
OriginalGriff20-Jun-19 5:21
protectorOriginalGriff20-Jun-19 5:21 
GeneralRe: Why is this not CLS-Compliant? Pin
#realJSOP20-Jun-19 5:24
mve#realJSOP20-Jun-19 5:24 
QuestionAm I Remembering Wrong? Pin
#realJSOP20-Jun-19 4:34
mve#realJSOP20-Jun-19 4:34 
AnswerRe: Am I Remembering Wrong? Pin
OriginalGriff20-Jun-19 4:52
protectorOriginalGriff20-Jun-19 4:52 
GeneralRe: Am I Remembering Wrong? Pin
#realJSOP20-Jun-19 4:58
mve#realJSOP20-Jun-19 4:58 
GeneralRe: Am I Remembering Wrong? Pin
OriginalGriff20-Jun-19 5:08
protectorOriginalGriff20-Jun-19 5:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.