Thread Rules 1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution. 2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20) 3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible. 4. Use [code] tags to format code blocks.
struct Test //creates a structure { char name[10]; //a 10-character array int num1; //the first integer int num2; //the second integer int sum; //the result of the subtraction of num2 from num1 } inst; //instance of the struct
...
inst.sum = &inst.num1 - &inst.num2; printf("The result is: %d\n", &inst.sum);
So first question is, why you are using ampersands here? Which should tell you the exact answer about why this isn't working.
Wow, okay, that fix was actually incredibly simple. Whenever I executed it before, I got the error "Segmentation fault (core dumped)" and I saw someone say that putting an ampersand fixed it Now that everything's working fine, I do have a question about this statement:
printf("Enter a name (up to 10 characters): "); fgets(inst.name, 11, stdin);
It currently will work correctly if the user hits the enter key with 10 characters or less inputted, but if they input 11 or more characters and hit Enter, the rest of the code immediately executes, meaning the user can't put in any numbers. Is there a way to fix that? I tried doing a for loop to accept 10 characters, 1 at a time, but it had the problem that if you inputted 4 characters, you had to press enter 6 times to get to the next part of the code, as well as it would take more than 10 characters if you inputted them all at once
Wait a moment here...
&inst.num1 - &inst.num2
Are those pointers to the struct members or struct itself? I don't remember much of my C but I'm almost sure that if you work on a pointer to the structure then you need this syntax:
struct Test //creates a structure { char name[10]; //a 10-character array int num1; //the first integer int num2; //the second integer int sum; //the result of the subtraction of num2 from num1 } inst; //instance of the struct
...
inst.sum = &inst.num1 - &inst.num2; printf("The result is: %d\n", &inst.sum);
So first question is, why you are using ampersands here? Which should tell you the exact answer about why this isn't working.
Wow, okay, that fix was actually incredibly simple. Whenever I executed it before, I got the error "Segmentation fault (core dumped)" and I saw someone say that putting an ampersand fixed it Now that everything's working fine, I do have a question about this statement:
printf("Enter a name (up to 10 characters): "); fgets(inst.name, 11, stdin);
It currently will work correctly if the user hits the enter key with 10 characters or less inputted, but if they input 11 or more characters and hit Enter, the rest of the code immediately executes, meaning the user can't put in any numbers. Is there a way to fix that? I tried doing a for loop to accept 10 characters, 1 at a time, but it had the problem that if you inputted 4 characters, you had to press enter 6 times to get to the next part of the code, as well as it would take more than 10 characters if you inputted them all at once
The inst.name char array needs to be size 11, otherwise fgets with 11 writes over the end of the array, meaning the null terminator is written into adjacent memory, which is lucky this time because it will likely be written into struct padding.
For your problem, if you use fgets(11, stdin) and enter more than 10 characters, only 10 characters are taken from stdin and put into the specified char array, plus a \0 at the 11th position. The remaining characters stay in the stdin buffer, so the next time you try to get from stdin it automatically reads from the remaining characters.
If you want to clear the input buffer, you can use this after fgets.
Otherwise, the proper way to do input with correct error handling is to only use getchar() and manually read one by one into buffers. If the user overflows the buffer you know they're doing the wrong things. While you're filling the buffer you enforce your rules, such as <10 characters to read.
You have the correct idea with the for loop, but your implementation should also handle all the error cases. This is all proper input sanitation and an important thing to try for yourself and learn.
Speaking of C, is the K&R book still the go to? I haven't done c or c++ since I took the two intro courses as electives from my uni 3 years ago lol. Kind of want to pick up C again and maybe learn some more about how low level stuff works (and appreciate how magical Python is).
On February 08 2017 09:54 travis wrote: if i have a function
int whatever() { while(1) { //do some stuff if(some condition is met) { return (some int); } } }
is this oversimplified example an easy way to create a loop in a function that needs to meet some criteria before stopping? since this will just loop and then when it meets the criteria you can just return whatever it is you want to return ?
or are loops typically done this way?
int whatever() { while(condition is not met) { //do stuff }
return(some int);
}
I guess this is a dumb question. It probably just depends on what's better for what you are trying to do. But really I am trying to make sure that nothing breaks in the first one.
Write your condition in the while clause. Don't write while(true) unless you are waiting for something outside of your program's control (such as a button being pressed) and even then, most programming languages have better control structures than while(true) loops, and at the very least you're going to need threading, and you always want to avoid busy waits.
I guess if your condition is really hard to write as a single clause you can consider it, but then your code probably needs refactoring in any case...
If you need the code to execute at least once, regardless of whether your condition holds or not, use a do.. while loop.
Regarding blisse's comment: I personally don't mind returning from different spots: I don't think debugging this form is inherently harder than debugging the same, but written with breaks.
On February 08 2017 12:45 Neshapotamus wrote: After seeing travis post his simple question.
I have seen people write this code in every which way (code at bottom)
I listed several of them at the bottom.
Anyone have any preference to which version they write?
More recently, I prefer to write "fun1" way as this is the easiest to reason about meaning it maps directly into math. You also get other benefits as its easier to parallelize. However, you need a TCO language to execute the statement.
The second most intuitive is fun2.
Anyone who writes fun5, I just avoid...
void fun1(){
void fun(int a, int b) { if (a > b){ return } else fun(a, b+1) }
a = <init> b = <init> fun(a,b) }
void fun2(){ a = <init> for(int b = <init>; a > b; b++){ } }
void fun3(){ a = <init> b = <init> while(true){ if (a > b){ break; } b += 1 } }
void fun4(){ a = <init> b = <init> while(a > b){ b += 1 } }
void fun5(){ start: if (a > b){ b += 1 goto start } }
Honestly, why not
int a = init; int b = a;
And that's the problem with such a simplistic example, because it doesn't show the (dis)advantages of each approach. For a more complex example, any one of fun1, fun2 or fun4 are OK for me, depending on what exactly you need to do. Fun3 is almost always bad (see my response to travis, one post up), and fun5 is only acceptable if you got stuck in the 70s...
In general, if you're incrementing a counter, a for loop is by far the most intuitive way of writing that in imperative languages. If you have a boolean condition, use a while loop. And for some things, recursion is simply easier. For example, tree traversal.
On February 08 2017 16:36 TheEmulator wrote: Speaking of C, is the K&R book still the go to? I haven't done c or c++ since I took the two intro courses as electives from my uni 3 years ago lol. Kind of want to pick up C again and maybe learn some more about how low level stuff works (and appreciate how magical Python is).
do you guys have an idea what kind of technical knowledge is required to work in defense industry as a programmer? I assume they are not writing web apps with angular
On February 09 2017 02:38 mantequilla wrote: do you guys have an idea what kind of technical knowledge is required to work in defense industry as a programmer? I assume they are not writing web apps with angular
disclaimer: I don't know c or c++.
I don't know about your question, but just a reminder that you might need security clearance for that thing.
Edit: I also remember hearing about MISRA, but I don't know how widely used it is.
On February 08 2017 16:36 TheEmulator wrote: Speaking of C, is the K&R book still the go to? I haven't done c or c++ since I took the two intro courses as electives from my uni 3 years ago lol. Kind of want to pick up C again and maybe learn some more about how low level stuff works (and appreciate how magical Python is).
Why do you want to pick C up? Why do you want to limit yourself to C only? Use C++ so you can use C and C++ if you want. C++ is better than ever though. It's much harder to end up with memory leaks in C++11/14, too.
Quick question as I am currently preparing for my (really basic) JPQL / databases exam:
What consitencyproblems could occour with the following code(see comments) EDIT: Slow down Manit0u, I only have to know what consitencyproblems can occour here, not how to fix them
em.getTransaction().begin(); Player playerA = (Player) em.find(Player.class, newLong(30)); //Player is now in persistence context em.getTransaction().commit(); playerA.setFirstName("Alex");//changing players name, however since it is outside of the transactionscope, it is not yet in the DB em.getTransaction().begin();//new transaction context em.getTransaction().commit();//transaction context closed, name is now 'Alex' in DB, if this fails, playerA.name=Alex, but in the DB the old name remains
On February 09 2017 05:39 Artesimo wrote: Quick question as I am currently preparing for my databases exam:
What consitencyproblems could occour with the following code(see comments)
em.getTransaction().begin(); Player playerA = (Player) em.find(Player.class, newLong(30)); //Player is now in persistence context em.getTransaction().commit(); playerA.setFirstName("Alex");//changing players name, however since it is outside of the transactionscope, it is not yet in the DB em.getTransaction().begin();//new transaction context em.getTransaction().commit();//transaction context closed, name is now 'Alex' in DB, if this fails, playerA.name=Alex, but in the DB the old name remains
This looks like a silly example to me, so here are a few comments first: 1. Why commit() when you only find a player (read only query)? There's no change. Maybe your lecturer is trying to confuse you, but it's still dumb. 2. Name is changed to Alex before you create a new transaction, so when you attempt to read/synchronise names, you might end up with the old name. However, as I said, this is silly and it looks more like implementation detail of this data context than actual databases.
On February 08 2017 16:36 TheEmulator wrote: Speaking of C, is the K&R book still the go to? I haven't done c or c++ since I took the two intro courses as electives from my uni 3 years ago lol. Kind of want to pick up C again and maybe learn some more about how low level stuff works (and appreciate how magical Python is).
Why do you want to pick C up? Why do you want to limit yourself to C only? Use C++ so you can use C and C++ if you want. C++ is better than ever though. It's much harder to end up with memory leaks in C++11/14, too.
There's nothing wrong in C. It's relatively easy to learn (not a big language), but very hard to master. It's arguably more useful than C++, depending on what you're interested in - if you want to supplement your other languages by learning something more low level, how some of the libraries for your language of choice are implemented or just dabble in the OS internals for example.
K&R C is 272 pages (and it's really all you need), while it's C++ equivalent is 1368 pages long and you'd probably need to read on STL, boost and other bullshit, which weigh in at similar info level.
Besides, Rust seems to be gaining a ton of traction now, with many libraries being ported to it and some big companies (Mozilla primarily) picking it up for their production line.
But you don't have to heed my warnings. For me, C++ is just above Java on the list of programming languages I find fun to work with and into which I'd like to dive in (hint: Java is at the very bottom of my list).
On February 09 2017 05:39 Artesimo wrote: Quick question as I am currently preparing for my databases exam:
What consitencyproblems could occour with the following code(see comments)
em.getTransaction().begin(); Player playerA = (Player) em.find(Player.class, newLong(30)); //Player is now in persistence context em.getTransaction().commit(); playerA.setFirstName("Alex");//changing players name, however since it is outside of the transactionscope, it is not yet in the DB em.getTransaction().begin();//new transaction context em.getTransaction().commit();//transaction context closed, name is now 'Alex' in DB, if this fails, playerA.name=Alex, but in the DB the old name remains
Why do it in such a convoluted way?
If you really want to ensure the consistency, you should do something like:
Thank you for the detailed answer, unfortunately it is for the wrong problem. I am not tasked to fix this, I only need to point out every thing that could go wrong.
^ There's nothing wrong in C? Yeah, repeating yourself because you don't have templates is surely nothing wrong. Dealing with heap memory more often than you need to is nothing wrong in C. Not being able to tell who deletes what because your stuff cannot be wrapped via an RAII class (to put it simply, destructor deallocates stuff for you so you never leak memory) is nothing wrong. Learning low level stuff? Yeah, right. As if you can't #include <windows.h> or the appropriate *nix header from C++. C++ can do everything C can *and* more by definition because C is a subset of C++. This isn't a flame war, it is the facts.
To be clear I'm fully aware of the differences between the languages since I've studied both. I'm not intending to work with either language professionally, if I was I would probably focus on both TBH. This is purely just a side thing that I'm interested in studying since I have more time now that I'm done University, and I chose C simply because "I wanted to"
I appreciate your concern though. No harm in that.
Quick git question: I've cloned a project from github, and messed around with it some. Now I want to upload it as a completely fresh project, and am not 100% sure how to. I'm not even really sure what keywords I should be Googling for, so if anyone even just wants to drop some of those I'm happy to figure it out on my own from there.
On February 09 2017 06:19 raNazUra wrote: Quick git question: I've cloned a project from github, and messed around with it some. Now I want to upload it as a completely fresh project, and am not 100% sure how to. I'm not even really sure what keywords I should be Googling for, so if anyone even just wants to drop some of those I'm happy to figure it out on my own from there.
Any reason why you aren't forking the project and continuing on your fork? Missing lots of context for better solutions to this. What do you mean by "completely fresh project"? Did you clone a project or did you fork it?
Otherwise easiest way to blow away all the git history (fresh project) is:
Delete the .git folder in the root directory Create a new git project in the root directory Add all the files to a new commit Push to your git server or github
On February 09 2017 06:19 raNazUra wrote: Quick git question: I've cloned a project from github, and messed around with it some. Now I want to upload it as a completely fresh project, and am not 100% sure how to. I'm not even really sure what keywords I should be Googling for, so if anyone even just wants to drop some of those I'm happy to figure it out on my own from there.
Any reason why you aren't forking the project and continuing on your fork? Missing lots of context for better solutions to this. What do you mean by "completely fresh project"? Did you clone a project or did you fork it?
Otherwise easiest way to blow away all the git history (fresh project) is:
Delete the .git folder in the root directory Create a new git project in the root directory Add all the files to a new commit Push to your git server or github
That's the solution if you want to lose all of history and such. Another choice would be changing the upstream address to your own repo.
Thank you for the detailed answer, unfortunately it is for the wrong problem. I am not tasked to fix this, I only need to point out every thing that could go wrong.
Just compare the code. In your example plenty of things could go wrong. You could go out of transaction scope unexpectedly, you make two commits for a single change operation, etc.
Bonus question: What would happen to the original code if no object was fetched from the db?
I'm not entirely sure why they're assigning you such questions. I would personally use a hatchet on anyone trying to push code like that.
On February 09 2017 07:45 Manit0u wrote: Just compare the code. In your example plenty of things could go wrong. You could go out of transaction scope unexpectedly, you make two commits for a single change operation, etc.
Bonus question: What would happen to the original code if no object was fetched from the db?
I'm not entirely sure why they're assigning you such questions. I would personally use a hatchet on anyone trying to push code like that.
Yeah, which is why I was asking in here... I am not really sure what exactly the point of the question is. I am pretty sure they are looking for one/two very specific answers but I am just not sure...
About the bonus question: I assume since there hasn't been fetched an actual object from the DB, the entity is empty and you would get some kind of error since you are calling a non static function (setName) in a static enviroment?