Open Side Menu Go to the Top

02-18-2016 , 04:37 AM
Quote:
Originally Posted by adios
Off the top of my head use encapsulation to write a function the reads user input with a signature something like:

Code:
bool AcceptUserEntry(string & userEntry, size_t maxEntryChars);
Returns false when too many chars entered.

Write another function with a signature something like:


Code:
bool IsUserEntryValid(string userEntry);
Return false for invalid entries.

Then insert calls to those functions in your main routine instead of processing user entries inline. In that way you encapsulate how users enter data and what the criteria is for valid entries. Divide and conquer.

adios,


Thanks for the reply mate!

I'm still very new to C and C++, and don't have a lot of programming experience in general. I've taken a class on Python and have messed around with some of the codecademy modules on Python and PHP, but I'm still quite new to programming, and I think your response is a bit over my head at this point. I understand it logically, but I really wouldn't know how to go about coding that in C, C++, or Python. I actually just learned in my last class how to create and call a new function in C++.



Quote:
Originally Posted by Wolfram
one thing I'd recommend is getting the invalid input out of the way first, i.e. "exit early".

e.g.

Code:
if (state is invalid)
  return error;

if (input is a)
  return answer for a;
else if (input is b)
  return answer for b;
else
  return generic answer;
It's more readable imo, and some people agree:
http://programmers.stackexchange.com...n-if-statement

Wolfram, thanks for the reply mate!

Would this solution require a new function, like adios mentioned? Or would it look at lot like my code, just with the input validation check before all the if/else statements? Or are you saying the check should be done immediately after the input is given for the variables?

Cheers!
Programming homework and newbie help thread Quote
Programming homework and newbie help thread
$25m Guaranteed WPM on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
Programming homework and newbie help thread
02-18-2016 , 08:48 AM
Quote:
Originally Posted by CyberShark93
okay, i found my error, in the loop condition for (i=1;i<=x;i=i*2),
instead of x, it should be the modulus N.

i have got to be the biggest ****** in the history of the universe.
LOL no. Our code defects are very often quite humbling. Thanks for posting btw.
Programming homework and newbie help thread Quote
02-18-2016 , 08:59 AM
Quote:
Originally Posted by catsec
adios,


Thanks for the reply mate!

I'm still very new to C and C++, and don't have a lot of programming experience in general. I've taken a class on Python and have messed around with some of the codecademy modules on Python and PHP, but I'm still quite new to programming, and I think your response is a bit over my head at this point. I understand it logically, but I really wouldn't know how to go about coding that in C, C++, or Python. I actually just learned in my last class how to create and call a new function in C++.






Wolfram, thanks for the reply mate!

Would this solution require a new function, like adios mentioned? Or would it look at lot like my code, just with the input validation check before all the if/else statements? Or are you saying the check should be done immediately after the input is given for the variables?

Cheers!
The reason I suggest the two separate functions has to do with the concepts of encapsulation. For instance AcceptUserEntry would provide a consistent interface between your main program and the source of your input data. If for some reason you wanted to add another entry source like a file, network connection, etc. you would just change AcceptUserInferface and your main routine is unaffected. Same thing with a validity check function. What constitutes a valid entry could possibly change and thus that change would be isolated to the function that checks for input validity.
Programming homework and newbie help thread Quote
02-18-2016 , 09:56 AM
Every coder, 2 seconds after finding a standard bug, would vote themselves dumbest person in the history of the world.
Programming homework and newbie help thread Quote
02-18-2016 , 02:41 PM
Quote:
Originally Posted by catsec
I understand what you're saying, thanks! Question: should I always have the mindset that changes to the code I write will probably need to made at some point in the future, and write my code so that it's easy to change?

Also, could you comment generally regarding how well written and structured and readable my code is?

Cheers!
This is where there's kind of a disconnect between work you do while learning and work you do professionally. For assignments/practice problems/etc, you'll usually forget about a program forever the minute you're done with it, so it's not particularly useful to think about structuring your code in a flexible way for future changes. But professionally, working on real-world projects, the way things are used can (and will) change very easily, and when that happens it helps to have code that can adapt. So, the earlier you start learning habits like that, the better, though it won't particularly benefit you in the short term.
Programming homework and newbie help thread Quote
02-18-2016 , 05:10 PM
Quote:
Originally Posted by ChrisV
Every coder, 2 seconds after finding a standard bug, would vote themselves dumbest person in the history of the world.
haha, yes!
Programming homework and newbie help thread Quote
02-18-2016 , 05:43 PM
Quote:
Originally Posted by ChrisV
Every coder, 2 seconds after finding a standard bug, would vote themselves dumbest person in the history of the world.
Long ago when I was quitting smoking I got stuck on a problem and I couldn't figure it out. I showed it to a coworker, he looked at it for like 2 seconds, and was like "that isn't how that parameter is used" which was completely obviously true - something I knew deep in my core for years, I just hadn't noticed it.

I got so mad I had to go for a drive to cool off.
Programming homework and newbie help thread Quote
02-18-2016 , 06:23 PM
I just spent 90 minutes trying to figure out why an array was getting printed to the console when I ran my program. Driving me nuts. Finally figured out, it was because the debugger prints out when an instance variable changes, and it wasnt actually because of the program.

Im using an unfamiliar language and new tools (not that I'm anything other than a neophyte when im in familiar surroundings). You are going to waste an obscene amount of time on 'stupidity' when you are learning, you just have to accept it as part of the process.
Programming homework and newbie help thread Quote
02-18-2016 , 09:47 PM
Quote:
Originally Posted by ChrisV
Every coder, 2 seconds after finding a standard bug, would vote themselves dumbest person in the history of the world.
I read a quote many years ago when I was learning Z80 assembler:

A programmer exists in 2 states. Deep despair because their code doesn't work or great elation because they know *why* their program doesn't work.
Programming homework and newbie help thread Quote
02-18-2016 , 10:48 PM
Programming homework and newbie help thread Quote
02-19-2016 , 11:00 AM
In addition to programming I do a lot of hobbyist stuff that involves troubleshooting. Electronics, CNC, wood and metal working. I have learned that I can not engage in these activities unless I have a solid block of time - at least 2 hours. Otherwise, I usually get stuck on something and I'm kind of a dick for the rest of the evening. Or, in a rush to get something done before I run out of time for the day, I **** something up.
Programming homework and newbie help thread Quote
02-20-2016 , 11:29 AM
So it never dawned on me that you can use something like rails or php for the back end only and then a different framework for the front end. Just one of those things I never thought about but it makes sense that you can. Seems like a great thing for a school to teach, but I digress.

Odds a student can learn enough angular and rails and bootstrap in 10 weeks to make a fairly functional, basic, single page application?
Programming homework and newbie help thread Quote
02-20-2016 , 05:06 PM
Quote:
Originally Posted by Noodle Wazlib
Odds a student can learn enough angular and rails and bootstrap in 10 weeks to make a fairly functional, basic, single page application?
probably pretty good, but you woudn't actually understand or be using any of the technologies well.

if you needed a basic, functional SPA app, every one of the technologies is complete overkill, with bootstrap being the least offender and maybe defensible depending...
Programming homework and newbie help thread Quote
02-20-2016 , 05:13 PM
I don't really know that much about javascript frameworks and stuff, but by observation, a lot of people using them don't really know very much about javascript. I would advise learning everything about javascript/html/css by, as gaming_mouse says, not using anything but the absolute basics. The only JS package I think I'd recommend using is jQuery.

If you can make a decent app with the basics, then you will easily be able to pick up stuff like react or angular, and you'll actually have a chance of understanding what they're doing and how to make them work for you.

I don't know anything about RoR but I've been told it's more heavyweight than a lot of other simple frameworks, which seems funny because it was the goto recommendation everyone used to have for a beginner to get a website working Right Now.

If you're making a simple single page app you probably don't need much of anything so I guess I'd go for the simplest thing available. I've found Flask (which is python based) very easy to get going from the ground up.
Programming homework and newbie help thread Quote
02-20-2016 , 06:00 PM
About the extent of "web" our school taught is "here's html and css, oh and MVCs are a thing". And then all the capstone projects are web apps.

Thanks a lot!!!

So it's basically "learn as many frameworks as it takes to make your app function in four months, gogogo!!"

I'd looked at django and slightly at flask, but since I don't know much of what's needed for a web app (is a routes page even necessary for a SPA?), I'd just be taking stabs in the dark.
Programming homework and newbie help thread Quote
02-20-2016 , 06:54 PM
I don't know what a routes page is.

In my experience from working with frontenders though, they want exactly 1 thing from me:
"endpoints" that return/store/modify data
and that is all.

Like I dunno, say you had a single page app that did something silly like let users post comments, read comments, maybe sort or filter them, whatever. Everything pretty much happens in the JS part, except that there would be, say
* an endpoint to save a comment
* an endpoint to get a list of comments that meet a given filter (all, by poster, by date range, etc)
and, uh, that would be about it.

With flask, you'd basically end up with a few python functions that would handle the data storage and retrieval. The ones you wanted to expose as endpoints, you'd use one the methods that flask has for saying "this URL maps to this function using these arguments". There are 2 or three ways of doing it, the simple way is to use flask's decorator. It'd probably be like 20 or 30 lines of python, max, that'd be your entire flask app.

(then you'd need whatever html/css/js on top of that to make it do stuff)
Programming homework and newbie help thread Quote
02-20-2016 , 08:19 PM
Quote:
Originally Posted by gaming_mouse
probably pretty good, but you woudn't actually understand or be using any of the technologies well.

if you needed a basic, functional SPA app, every one of the technologies is complete overkill, with bootstrap being the least offender and maybe defensible depending...
Angular routing might be nice to have depending on what is meant by a "basic SPA".

Edit: What does your app have to do lowkey?
Programming homework and newbie help thread Quote
02-20-2016 , 08:27 PM
Store and retrieve docx files, possibly render them as PDFs. Have user accounts.
Programming homework and newbie help thread Quote
02-20-2016 , 09:33 PM
Noodle, you will need at least one route for "/".

If I may be so crass, maybe try out Python Bottle. It has everything you need and you don't have to read reams of documentation to get started. Just:

Code:
from bottle import *

@route("/")
    return "hello world!"

run(host='localhost', port=8080)
or, from the docs:

Code:
from bottle import route, run, template

@route('/hello/<name>')
def index(name):
    return template('<b>Hello {{name}}</b>!', name=name)

run(host='localhost', port=8080)
Can't do that in Django.
Programming homework and newbie help thread Quote
02-20-2016 , 09:49 PM
Yeah, Flask works that way too. From the names I assume that one is an offshoot or clone of the other.
Programming homework and newbie help thread Quote
02-20-2016 , 11:24 PM
Well id also have to figure out a fair amount of the behind the scenes stuff, like writing to the database and whatnot.
Programming homework and newbie help thread Quote
02-21-2016 , 12:07 AM
Quote:
Originally Posted by Noodle Wazlib
Well id also have to figure out a fair amount of the behind the scenes stuff, like writing to the database and whatnot.
Yeah it's turtles all the way down.

Tell you what, though, you can get started by pretending the database part is already solved for you. That is, consider an endpoint that you want to return some results from the database for you.

Write the function, ignore the database, and hard-code some return value into the function.

Why? Because it'll let you get the part of the front end that does the work of fetching the data and displaying it working, without having to have a working function that accesses the database.

Or, you can skip the database and do something silly like write the data directly to a file, and read from the file. If you're using python there are a few easy but silly ways to do this. Is this a Good Idea? Not for a real app. But it's fine for the period of time where you're trying to just get one part working.

The difficulty that you're seeing is that there are several moving parts that need to work together. So, find a way to isolate it to one part, and work on that.

Alternatively, you could just start with the database parts, forget all the front end stuff for now, and just get an endpoint working that you interact with manually until it works. Getting data into/out of a database is pretty easy in most modern languages. Once that's working then you could move on to the front end parts.

Divide and conquer. Repeat until finished.
Programming homework and newbie help thread Quote
02-21-2016 , 12:12 AM
And as an aside, I think Getting Into Computer Programming is harder these days than when I started, because of these interdependencies. When I started programming the state of the art was something like an Apple IIc. It had a mostly text interface with some very terrible and slow graphics. So the programs you could make yourself, while simple, were not THAT far off from something you could buy. You didn't have to learn multiple coordinating systems to make something interesting to show your friends or a potential employer.

These days you kind of have to know a lot of stuff. You have to be able to do some system administrator type stuff. Maybe you have to install a database, figure out how SQL or mongo or whatever works, design a database structure, find out how to interact with the database in your language, figure out how the HTTP protocol and REST interfaces work. Learn some javascript and jquery, HTML and CSS. Holy crap now you can make a crappy web page!

I wrote my first programs with pencil and paper and typed them into a computer when I had access to it. Can you imagine?
Programming homework and newbie help thread Quote
02-21-2016 , 12:15 AM
Quote:
Originally Posted by Noodle Wazlib
About the extent of "web" our school taught is "here's html and css, oh and MVCs are a thing". And then all the capstone projects are web apps.

Thanks a lot!!!

So it's basically "learn as many frameworks as it takes to make your app function in four months, gogogo!!"

I'd looked at django and slightly at flask, but since I don't know much of what's needed for a web app (is a routes page even necessary for a SPA?), I'd just be taking stabs in the dark.
can you describe the app?

i can probably give you some recommendations for small libraries that would be more than enough to accomplish what you need, would teach you more fundamental web skills, and avoid a bunch of incidental complexity that will come with the frameworks you mention.

@ChrisV,
Does angular offer its routing as a separate tiny module, or do you have to bring in all of angular? Because if he only needs simple routing, he should just use a small routing library -- there are a ton of them.
Programming homework and newbie help thread Quote
02-21-2016 , 12:18 AM
Quote:
Originally Posted by RustyBrooks
Yeah it's turtles all the way down.

Tell you what, though, you can get started by pretending the database part is already solved for you. That is, consider an endpoint that you want to return some results from the database for you.
This is good advice, and depending on what's acceptable for your class, you might be able to keep pretending -- that is, just use one of the new parse alternatives or firebase for you backend.
Programming homework and newbie help thread Quote
Programming homework and newbie help thread
$25m Guaranteed WPM on CoinPoker
Join the action now
Daily Rewards • Splash Pots • CoinRaces
Programming homework and newbie help thread

      
m