Sorting/Swaping/File(i/o)_withStrings Struct Arrays

  • (2 Pages)
  • +
  • 1
  • 2

18 Replies - 710 Views - Last Post: 03 December 2013 - 07:15 AM Rate Topic: -----

#16 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3666
  • View blog
  • Posts: 11,494
  • Joined: 05-May 12

Re: Sorting/Swaping/File(i/o)_withStrings Struct Arrays

Posted 02 December 2013 - 06:55 AM

View Postsporte00, on 02 December 2013 - 04:50 AM, said:

Also thanks for the direct c vs c++ example I'm going to try to focus a mostly c code from now on
struct FoodItem
{
  int    id;
  string name;
  int    calories;
  double cost;
  bool   on;

  FoodItem() {}
  FoodItem(int i, const string &n, int cals, double c, bool o)
    : id(i), name(n), calories(cals), cost(c), on(o) {}
};



Unfortunately, that is C++ code.
Was This Post Helpful? 0
  • +
  • -

#17 jimblumberg  Icon User is online

  • member icon


Reputation: 4292
  • View blog
  • Posts: 13,457
  • Joined: 25-December 09

Re: Sorting/Swaping/File(i/o)_withStrings Struct Arrays

Posted 02 December 2013 - 06:57 AM

Also when you do the reformatting don't forget to use consistent white space as well.

// IMO hard to read.
for(int i=0; i<count_items; i++) 
a=b+c*(d+a);
// IMO Easier
for(int i = 0; i < count_items; i++)
   a = b + c * (d + a);



Jim

This post has been edited by jimblumberg: 02 December 2013 - 06:58 AM

Was This Post Helpful? 0
  • +
  • -

#18 sporte00  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-November 13

Re: Sorting/Swaping/File(i/o)_withStrings Struct Arrays

Posted 03 December 2013 - 12:05 AM

ok, you're right... This is incredibly difficult to read, and largely so jammed together that even after I've put it in allman it still sucks. The good news i think i solved the error i was getting. I should be using switch cases not these if statements. Second note, this code is so vastly mixed I'm going to need some proofing after I've finished to remove any additional elements I miss that would mix the c or c++ only criteria. Attempt one Fail. I'll keep trying.
Was This Post Helpful? 0
  • +
  • -

#19 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3666
  • View blog
  • Posts: 11,494
  • Joined: 05-May 12

Re: Sorting/Swaping/File(i/o)_withStrings Struct Arrays

Posted 03 December 2013 - 07:15 AM

Also, don't be afraid to start from scratch. Take the concepts that you learned from v.Previous and start fresh on v.Next. Only transplant code to save on typing if you truly grok what it does. Otherwise, retype it from scratch. (Nevermind if source control loses the code copying/branching history. You have bigger fish to fry right now.)

It's something that almost all software engineers/developers learn towards the tail end of their initial learning curve. There are times when you have learned all you can from a particular version of a program, and any further maintenance on it is just a lot of shoveling code around or dealing with tons of special cases because the code was written with too tight coupling or too loose cohesion. The good software exhibits loose coupling/tight cohesion.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2