d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Make Me A Better Programmer - From Step 1
12356Next
Add Reply New Topic New Poll
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jun 25 2012 11:01pm
I am just going to post code I did for various projects in class, and if you guys see any ways for me to improve, let me know about them.

It will be important for me to learn about stuff outside of the classroom, and I know there are a bazillion knowledgeable parties on JSP (too many friggen programmers on this site real talk) who might be able to help.

Don't think this is breaking any rules - so hopefully I can keep this topic rolling for awhile.

anyways, this is the 2nd project we were assigned - nothing difficult, but I feel like it took quite a few lines to achieve something pretty simple.


Code
#include <iostream>
#include <iomanip>

using namespace std;

const float TIP20=.20;
const float TIP15=.15;
const int MAXCOST = 1000;


int main() {
char service;
float tiprate, mealcost, total, tiptotal;

cout << setprecision(2) << fixed << showpoint;

cout << "Was the service tonight satisfactory?" << endl;
cin >> service;
if (service == 'y' || service == 'Y') tiprate = TIP20;
   else if (service == 'n' || service == 'N') tiprate = TIP15;
       else {
           cout << "Please enter a y or n and try again!" << endl;
           return 1;
           }
sleep(2);

cout << "Thank you for your feedback. Please enter the cost of your meal tonight" << endl;

cin >> mealcost;

if (mealcost < 0 || mealcost > MAXCOST ) {
   cout << "Error: You may only enter a number between 0 and 1000. Please try again." << endl;
   return 1;
   }

tiptotal = (tiprate * mealcost);
total = (mealcost + tiptotal);

cout << "Your meal cost was " << mealcost << "$" << endl;
cout << "Your tip was " << tiptotal << "$" <<  endl;
cout << "The total cost of your meal with tip tonight is " << "$" <<  total << endl;

return 0;
}



edit: some specifics

create a programmer that asks the user how the service was. Depending on answer use one of two tip rates. Calculate the total cost of their meal with tip included. Make sure cost is between 0 or 1000

Professor was super insistent on us using constants

Don't know (at the moment) how to restart something when you enter something that doesn't work - so I have to tell the user to restart



edit: already ran into one issue

if the user types something that begins with a Y or N but isnt y or n.....it goes through the entire thing and gives some awkward values for the rest.

edit2: well apparently, whatever letters after the 'y' or 'n' are carried over to the next function.....which currently doesn't know wtf to do.

Need to fix this ~_~

This post was edited by Eep on Jun 25 2012 11:14pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jun 25 2012 11:17pm
my advice is for someone who's programmed a bit, maybe had 1-2 classes or learned on their own. if you're on week1 of your first class, this might be more confusing than helpful.

with respect to programming, the first red flag i see is that everything is inside main. i would isolate each "step" of the program into a separate function. you'll notice that each "step" does certain things:
1. prompt user for input
2. validate data
3. process data

which lays itself out nicely. you can also separate them into "tiers" or "layers" to abstract out cin/cout. if you wanted to reuse your program in the future without using the console (eg: you have a GUI with buttons/msgboxes/etc) it would be much easier. i'm more familiar with it in the OOP world, but i'm sure C has some way to do it (function pointers?).

Quote
Don't know (at the moment) how to restart something when you enter something that doesn't work - so I have to tell the user to restart


Putting stuff in multiple functions will help with this. it also makes it easier to manage recoveries when errors occur imo. the idea is that your program follows a certain "workflow", meaning that it has different states. when errors occur, you can keep it in the same state or change to a different state

As a practise for later, i'd suggest learning how to create log files. suppose you had a bug in your program and you want to figure it out. the only way is to keep staring at the code and running it through a debugger. also if you have separate functions, your log could include every input/output of every function you write, which greatly helps you narrow down where the problems are.

i also recommend creating your own library that you'll use in future projects. something like is a great candidate:
Quote
if (service == 'y' || service == 'Y')

You could create a library like so:
Code

public boolean cbool(string s) throws InvalidArgumentException{
   if (s == 'y' || s == 'Y') return true;
   if (s == 'n' || s == 'N') return false;
   throw new InvalidArgumentException;
}


Obviously, look into other libraries first to avoid duplicate code.


This post was edited by carteblanche on Jun 25 2012 11:23pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jun 25 2012 11:20pm
Quote (carteblanche @ Jun 26 2012 12:17am)
my advice is for someone who's programmed a bit, maybe had 1-2 classes or learned on their own. if you're on week1 of your first class, this might be more confusing than helpful.

with respect to programming, the first red flag i see is that everything is inside main. i would isolate each "step" of the program into a separate function. you'll notice that each "step" does certain things:
1. prompt user for input
2. validate data
3. process data

which lays itself out nicely. you can also separate them into "tiers" or "layers" to abstract out cin/cout. if you wanted to reuse your program in the future without using the console (eg: you have a GUI with buttons/msgboxes/etc) it would be much easier. i'm more familiar with it in the OOP world, but i'm sure C has some way to do it (function pointers?).



Putting stuff in multiple functions will help with this. it also makes it easier to manage recoveries when errors occur imo.

As a practise for later, i'd suggest learning how to create log files. suppose you had a bug in your program and you want to figure it out. the only way is to keep staring at the code and running it through a debugger. also if you have separate functions, your log could include every input/output of every function you write, which greatly helps you narrow down where the problems are.


ahh. That would make sense.

Can you help me with the 2nd edit I made? I don't like the fact that if someone types 'yes', the cin fetches the 'y' but the 'es' is inputted into the answer for the mealcost....

I also need a way to make it so the cin for mealcost will not accept strings or something.

edit; how does one create a log file? Is it done in vi or done in c++ or done in unix? (the three most important things to me right now ;_;)


edit: the library thing

is it something you would just keep on a text file to use for the future? Or is it saved on a database somewhere? Also, I obviously don't get a lot of what is going on in the one you posted. Maybe you could be so kind as to explain it part by part! (stuff like 'throws', 'cbool', 'InvalidArgumentException')

This post was edited by Eep on Jun 25 2012 11:26pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jun 25 2012 11:27pm
Quote (Eep @ Jun 26 2012 01:20am)
ahh. That would make sense.

Can you help me with the 2nd edit I made? I don't like the fact that if someone types 'yes', the cin fetches the 'y' but the 'es' is inputted into the answer for the mealcost....

I also need a way to make it so the cin for mealcost will not accept strings or something.

edit; how does one create a log file? Is it done in vi or done in c++ or done in unix? (the three most important things to me right now ;_;)


i'm not a C coder so i can't help with C library-specifics i'm afraid.

i assume C has a log library you can use. java has log4j which is very easy to use. basically you just call log.debug(...), log.error(...) in your code, and you have an xml configuration file that controls what your output looks like (does it include file name, timestamp, etc), how large your log file grows (so it doesn't kill your hard disk), how verbose you wanna track, etc.

Quote
is it something you would just keep on a text file to use for the future? Or is it saved on a database somewhere?


i'm not a C person so i can't give details there. notice how you say #include <iostream> on top? that means you're using someone else's library. if you create your own library, you'll add #include <EepsLibrary> and have access to all the functions you wrote so you dont have to keep writing the same code over and over

This post was edited by carteblanche on Jun 25 2012 11:30pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jun 25 2012 11:29pm
Quote (carteblanche @ Jun 26 2012 12:27am)
i'm not a C coder so i can't help with C library-specifics i'm afraid.

i assume C has a log library you can use. java has log4j which is very easy to use. basically you just call log.debug(...), log.error(...) in your code, and you have an xml configuration file that controls what your output looks like (does it include file name, timestamp, etc), how large your log file grows (so it doesn't kill your hard disk), how verbose you wanna track, etc.



i'm not a C person so i can't give details there. notice how you say #include <iostream> on top? that means you're using someone else's library. if you create your own library, you'll add #include <EepsLibrary>


ohhh, I see. That would be pretty useful.

thanks.
Member
Posts: 4,541
Joined: Sep 15 2011
Gold: 10,391.00
Jun 26 2012 01:39pm
if you're planning to do any serious programming with C, one of the best things you can learn is how to use gdb

logging is generally not great for C programming because, well... if you're programming in C, chances are that you're working at a low enough level that logging libraries aren't readily available or easily usable.

different story if you're eventually going to be working with C++ and are just using C as an intermediate stepping stone, of course.

This post was edited by irimi on Jun 26 2012 01:42pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jun 26 2012 01:58pm
Quote (irimi @ Jun 26 2012 02:39pm)
if you're planning to do any serious programming with C, one of the best things you can learn is how to use gdb

logging is generally not great for C programming because, well... if you're programming in C, chances are that you're working at a low enough level that logging libraries aren't readily available or easily usable.

different story if you're eventually going to be working with C++ and are just using C as an intermediate stepping stone, of course.


c++ is just the first thing they teach you with at my uni.

Over the course of the degree we will see a lot of different languages I imagine.
Member
Posts: 4,541
Joined: Sep 15 2011
Gold: 10,391.00
Jun 26 2012 04:05pm
actually, reading fail on my part, since i didn't even really look at your code very much and assumed it was C code from carteblanche's responses. looks like you're already working with C++, so yeah... adding logging to your coding habits will go a long way and it's trivial to learn.

gdb is still useful for C++ debugging of course, but is a bit harder to get into... but well worth the time for any kind of serious C/C++ development imo.

aside from that, it really is just a matter of spending more time/getting more comfortable with coding and the mode of thought required to do it. things that used to be hard will get easier, and as that happens, work on developing an eye for simplifying, simplifying, simplifying. as with many things in life, and with coding in particular, the less code you have to write to achieve something, the more it's worth.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jun 26 2012 04:37pm
Yeah. I like the idea carter had about writing separate functions for each different part.....it makes a lot of sense. I just don't know if it is something my professor would want me doing or if he would even care, since it is an intro class.


The way he is grading the assignment is he is taking a file with 2 inputs (y and some number) which enters into your program. Since he never told us how to fix those problems I had, I am guessing he won't dock points for them.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jun 26 2012 10:51pm
someone at cprogramming forums told me my indentation was ass

how can I improve it // what is the standard way to do it nicely
Go Back To Programming & Development Topic List
12356Next
Add Reply New Topic New Poll