Jump to content

Hi, I wonder if someone could help me out I am a bit nervous posting my rather amateur attempt ...


G+_Pete Murphy
 Share

Recommended Posts

Hi,

I wonder if someone could help me out. I am a bit nervous posting my rather amateur attempt at coding here as when I read through what others have done I am super impressed and kind of feel like the dunce of the class. But I won’t give up :D and will try asking for help.

After last weeks vid I thought I would try to make a very, and I mean Very, simple program to ask the user for two numbers, then pass them off to a function which should return a value which is the addition of the two numbers (simple eh --- or so I thought).

First I couldn’t work out how to read in an integer, I just couldn’t get it, so I resorted to using strings and copying the code from Lou from an earlier episode to check it is a number (hope you don’t mind Lou). Then I thought I would use a simple addition to add the two numbers together in a function and return the output.

But what happens is that by using strings I get for example 123 + 456 (should add up to 579) but instead it returned a value of ‘123456’. OK,ok, I had quite a chuckle and actually LOL-ed for real. Computers do seem to be quite literal.

Anyway, I have made my attempt, got my hands dirty and gotten really lost. I also know my code is rather messy and doesn’t look anywhere near as clean as what others have posted (I get the feeling others have a lot of prior experience). Anyway, I just came up with the idea of asking the community for help. Could someone please take a look at my attempt and just let me know how you would have done it in a cleaner, nicer fashion, e.g. how to ask the user to enter an integer. Any help would be highly, highly appreciated -> what I really want is some feedback rather than a solution so I can continue to learn.

My first program’s code can be found here -> http://pastebin.com/Cc91RqdQ 

http://pastebin.com/Cc91RqdQ

Link to comment
Share on other sites

You are SO close!  Here is one hint: In Lou's code, he uses TryParse() which actually does two things at the same time.  Not only is it testing to see if a string CAN be converted, it actually does the conversion too, if it can.  It returns the converted number in the second parameter.  That's why you need the "out" keyword there.  So take a look at the line right before the TryParse() call.  I think there is a variable there that you can use...

Link to comment
Share on other sites

The great thing about programming I am learning is that there is loads of ways to do one thing. Ive only been doing this a week and hitting some challenges too so happy to help when I can. 

 

Because you have it as a string you cant add them. Same as you cant add the word mushroom and toilet. What would the answer be lol? Strings are text (in a manner).

 

One thing you can do if you want to keep the code the way it is since you most likely understand it, but change it slightly.

 

have your function (SumOfIntegers) return an int instead of a string then at the start of the function have the string "Conver.ToInt32" (notice the way I have typed that as its a hint for you).

 

Also change  IntegerSum to an int so it can return teh value fro your newly edited function. 

 

Im happy to work along side you on skype / facebook or something as good to have class mates to bounce ideas off.

Link to comment
Share on other sites

Hi all,

 

I just wanted to drop you a note to say how grateful I am for your help. And get this, I actually made it!!!! My code thanks to your help now works so I really, Really Appreciate your help.

 

It is getting a bit late here and I need to get up for work early tomorrow so I am not going to tweak my code anymore, I do realize I need to change some things to make it a bit better (e.g. I cut and pasted the code for enter number 1 then enter number 2 where I know this should instead be a function of its own – I will work on this later on).

 

Here is my code, I have worked it so much over the last few days that I no longer really recognize it, I got so frustrated with tracking my variables at one point I just renamed everything (I should write better notes). Anyway, once again thankyou, thankyou thankyou for your help.

 

Here is my very basic, simple, easy, beginners first function code (but now a working code!!!)

 

http://pastebin.com/4HJnN2uD 

 

Pete

Link to comment
Share on other sites

 

I do have one follow up question though 

– why do I have to go to all that effort to get an integer from the user where I first get a string then sanitize it and use TryParse to turn it into an integer. It just seems like a lot of extra steps.

 

I mean, this seems to me to be really the long way around. I understand that you need to do this for good practice reasons to make sure someone doesn’t break your code (as Padre said by typing, for example , ‘Balloon‘ instead of actually entering a whole number as expected) but why does it appear that C# is so strict on this point. I mean I was pulling my hair out trying to get the code correct and getting frustrated that this below just would not work….

 

Console.Write("\nPlease enter an integer value: "); 

int enteredNumber = Console.ReadLine();

 

As say I understand the need to check/scrutinize/sanitize entered values before proceeding but what if, for example, if I somehow trusted the user to not make mistakes or enter the wrong value – like the ‘user’ was actually another computer such as a calculator or some other software that was guaranteed to return an integer value? 

 

I would just like to know why I spend a couple of days struggling with why the simple code above would not work? (I would have expected it would have still worked even if it was indeed bad coding practice). Once again many thanks in advance.

 

Pete

Link to comment
Share on other sites

Console.ReadLine taking code from an already created class(or namespace, im still new) which you call at the top of the code where all those include lines are and does the work for you so it has to be a string as thats how it was programmed.

 

Also strings are special and not really variables at all and have their own class called String so they get special permissions.

 

However I feel where youre coming from as my code I posted had a similar issue.

Link to comment
Share on other sites

 Share

×
×
  • Create New...