Jump to content

adding plus and minus 10 in c#

Daniboi

so i am trying to make a program that asks for a user input between -10 and +10 and then adds them together. i am really new to c# and would really appricate some help with it and so far its gone as well as a drunk doctor tying to do heart surgry. here is what i have wrote but i have no clue what to do next nor get the bit i have done to work. Thanks :)


 

 sbyte sbnum1 = 0;
                sbyte sbnum2 = 0;

                sbyte.TryParse(Console.ReadLine();

                sbyte answer = sbnum1 + sbnum2;

 

Link to comment
Share on other sites

Link to post
Share on other sites

Usually I approach these sorts of homework assignments by breaking it into steps, to 'simply' fill in the code after deciding the structure. The structure you could follow is something like this:

// Ask for input
// Check if input is between -10 and +10 (and actually a number), if it isn't, go back to step 1. Otherwise proceed

// Do the steps above once more for the second number

// Add these two numbers together
// Display the result on screen.

 

The way you could approach this (could, as with any coding assignments, there are a lot of ways to go about it) is likely implementing a while-loop for the user input, as you don't know when they will give the right input.

Then afterwards, you simply add it all up, once the user had the right input.

 

Is there any reason you're using sbyte? You could probably use int too.

Hope this helps a bit. I dislike giving people a piece of code if it's homework, as I feel like you won't learn anything from it.. Because thinking of a structure of code is one thing, actually creating it is another..

But I just wrote a piece of code that I think should work for this purpose, so if there are any questions; please do ask.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

44 minutes ago, minibois said:

Usually I approach these sorts of homework assignments by breaking it into steps, to 'simply' fill in the code after deciding the structure. The structure you could follow is something like this:


// Ask for input
// Check if input is between -10 and +10 (and actually a number), if it isn't, go back to step 1. Otherwise proceed

// Do the steps above once more for the second number

// Add these two numbers together
// Display the result on screen.

 

The way you could approach this (could, as with any coding assignments, there are a lot of ways to go about it) is likely implementing a while-loop for the user input, as you don't know when they will give the right input.

Then afterwards, you simply add it all up, once the user had the right input.

 

Is there any reason you're using sbyte? You could probably use int too.

Hope this helps a bit. I dislike giving people a piece of code if it's homework, as I feel like you won't learn anything from it.. Because thinking of a structure of code is one thing, actually creating it is another..

But I just wrote a piece of code that I think should work for this purpose, so if there are any questions; please do ask.

yeah so the reason for sbyte is so i can go into negitive numbers. my issue is that i know the theory but not well enough to code. this assignment is just a relitivity annoying one. i just am stuck on what part you have to do get a user input

Link to comment
Share on other sites

Link to post
Share on other sites

12 minutes ago, Daniboi said:

yeah so the reason for sbyte is so i can go into negitive numbers. my issue is that i know the theory but not well enough to code. this assignment is just a relitivity annoying one. i just am stuck on what part you have to do get a user input

C# int can also handle negative numbers. It's a signed exclusive integer (if I used the 'exclusive' thing correctly..). The value can range from -n to +n-1. n = max value depending on what version of int you're using (Int32, Int64, etc.)

 

Reading user input into a variable like an sbyte (or even int) is kind of dangerous, because if the user fills in something that is not a number, the program will crash. That is why it is safer to read it into a string, check if that input is correct, then afterwards convert it to an int (or whatever numeric data variable you want to use).

You are using Console.ReadLine() correctly though, I just recommend doing something like this:

Quote

string input = Console.ReadLine();
while(!isInputGood(input))
{
  Console.WriteLine("Please use only numbers, between -10 and +10");

  input = Console.ReadLine();
}
int inputInt = Int32.Parse(input);

 

isInputGood() is a method that returns a true or a false, depending on if the user input matches your restrictions (must be a number, between -10 and +10). The 'while' creates an endless loop, until some input is sent that matches the restrictions. If anything is given as an input that does match, it will go out of the while loop and to the Int32.Parse. This can handle negative and positive numbers.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, minibois said:

C# int can also handle negative numbers. It's a signed exclusive integer (if I used the 'exclusive' thing correctly..). The value can range from -n to +n-1. n = max value depending on what version of int you're using (Int32, Int64, etc.)

 

Reading user input into a variable like an sbyte (or even int) is kind of dangerous, because if the user fills in something that is not a number, the program will crash. That is why it is safer to read it into a string, check if that input is correct, then afterwards convert it to an int (or whatever numeric data variable you want to use).

You are using Console.ReadLine() correctly though, I just recommend doing something like this:

isInputGood() is a method that returns a true or a false, depending on if the user input matches your restrictions (must be a number, between -10 and +10). The 'while' creates an endless loop, until some input is sent that matches the restrictions. If anything is given as an input that does match, it will go out of the while loop and to the Int32.Parse. This can handle negative and positive numbers.

ok thnks that will really help :)

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, Daniboi said:

ok thnks that will really help :)

Good luck with the rest of the code! If you need any more assistance, I'll be glad to help.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×