d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Make Me A Better Programmer - From Step 1
Prev123456Next
Add Reply New Topic New Poll
Member
Posts: 2,736
Joined: Nov 28 2009
Gold: 34.00
Jun 26 2012 11:06pm
Quote (Eep @ 27 Jun 2012 06:51)
someone at cprogramming forums told me my indentation was ass

how can I improve it // what is the standard way to do it nicely


use the tab key, dont spam space. if your editor indents too far, change the settings to make 1 tab equal to 2 spaces

this is how i would indent your code



indenting is also a matter of preference. some press enter before opening brackets, some don't. ( http://2.imgland.net/FUWw7L.png )

This post was edited by eagl3s1ght on Jun 26 2012 11:08pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 3 2012 01:48am
meh, that one was too much above me I spose. Or rather, I could have just used a while loop.


Anyway, here is the next program I wrote:

Code
#include <iostream>
#include <iomanip>

using namespace std;

int main() {

   double x, rate, total = 0;

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

   cout <<"Please enter the rate of interest" << endl;

   cin >> rate;

   if (rate < .01 || rate > .1) {
       cout <<" Error: You must enter a % between 1% and 10%." << endl;
       return 1;
   }

   cout <<"Please enter some values"<< endl;

   while (cin >> x) {
       while (x < 0) {
           cout << "Negative value detected. Ignored." <<endl;
           break;
       }
       total *= 1+ rate;
       total += x;
       total +=  x*rate;
   }

   cout <<"Your total is: $" <<  total  << endl;

   //double variables  means total might not be 100% accurate!!

   return 0;
}



Instructions were to create a program that compounds interest. Anything you see in there that seems odd (the restrictions on % values and not accepting negative numbers) were the instructors orders. Tried to fix up my indenting etc. Still not sure how to make separate main functions like someone mentioned before.

LMK. If you have a better way to fix that initial cin >> rate; not having to close the program when an erroneous number is received, I would be grateful! (I tried doing a loop but couldn't figure out how to make it go to the next step)
Member
Posts: 4,541
Joined: Sep 15 2011
Gold: 10,391.00
Jul 3 2012 02:55am
Code
while (x < 0) {
 cout << "Negative value detected. Ignored." <<endl;
 break;
}


dude, this is just an if statement.

the input validation that you called "odd" are not odd at all. they look fine. what's odd is your program's control flow... which honestly kind of sucks. your nested while loops aren't doing what you think they're doing.

here's a simple idiom you can use for input gathering and validation:

Code

while (true) {
 prompt for input
 if (input is invalid) {
   print an error
 } else {
   break
 }
}


This post was edited by irimi on Jul 3 2012 02:59am
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 3 2012 03:03am
Quote (irimi @ Jul 3 2012 03:55am)
Code
while (x < 0) {
 cout << "Negative value detected. Ignored." <<endl;
 break;
}


dude, this is just an if statement.

the input validation that you called "odd" are not odd at all.  they look fine.  what's odd is your program's control flow... which honestly kind of sucks.  your nested while loops aren't doing what you think they're doing.

here's a simple idiom you can use for input gathering and validation:

Code
while (true) {
 prompt for input
 if (input is invalid) {
   print an error
 } else {
   break
 }
}


I'll try that.


edit: the initial while is supposed to accept many values. Apparently that break you added caused it to stop after 1.


with the 'nested while' it was doing exactly what I wanted it to do - allow the user to keep entering new values and ignore any negatives.


edit: nvm, it was *NOT* getting rid of the negative number :/

need to find a way to fix this!


Code
while (true) {
       while (cin >> x) {
           if (x<0) {
               cout <<"Negative value detected: Ignored." << endl;
           }
           else {
               break;
           }

       total *= 1+ rate;
       total += x;
       total +=  x*rate;

       }
   }


does NOT work!

This post was edited by Eep on Jul 3 2012 03:30am
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 3 2012 03:49am
Code
while (cin >> x) {
       if (x<0) {
           cout << "Negative" << endl;
           continue;
       }
           total *= 1+ rate;
           total += x;
           total +=  x*rate;


   }



THIS ended up working. Thank GOD for the continue thing.
Member
Posts: 4,541
Joined: Sep 15 2011
Gold: 10,391.00
Jul 3 2012 11:21am
the pseudocode i posted works just fine. you're just retarded.

This post was edited by irimi on Jul 3 2012 11:21am
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 3 2012 02:33pm
Quote (irimi @ Jul 3 2012 12:21pm)
the pseudocode i posted works just fine.  you're just retarded.


that is a good way to get people to listen!

at the end of the day, my thing worked, yours didn't, probably because you didn't explain it properly (it probably worked, but it wasn't what I needed)

This post was edited by Eep on Jul 3 2012 02:49pm
Member
Posts: 4,541
Joined: Sep 15 2011
Gold: 10,391.00
Jul 3 2012 03:45pm
the code you wrote that followed my pseudocode ended up resembling nothing like it. for one thing, i didn't write two while loops. for another, it wasn't written for gathering multiple values - it was written to do input validation on a single value (you were complaining about not being able to properly validate and re-prompt for interest rate values), though it could easily be adapted to collect multiple values.

you seem to have a pretty shallow understanding of how control flow works. i'd suggest that you spend some time looking at code and running it through your head line by line, rather than doing things by simple trial and error. knowing how your code runs is one of the fundamental requirements to becoming a competent programmer, and it's kind of the point of this exercise anyhow.

finally, i have fairly little interest in convincing you that i'm right. i have no stake in this either way.

edit: instead of a continue in your if statement, all you needed was to put the rest of the code in an else clause

This post was edited by irimi on Jul 3 2012 03:52pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 3 2012 04:00pm
Quote (irimi @ Jul 3 2012 04:45pm)
the code you wrote that followed my pseudocode ended up resembling nothing like it.  for one thing, i didn't write two while loops.  for another, it wasn't written for gathering multiple values - it was written to do input validation on a single value (you were complaining about not being able to properly validate and re-prompt for interest rate values), though it could easily be adapted to collect multiple values.

you seem to have a pretty shallow understanding of how control flow works.  i'd suggest that you spend some time looking at code and running it through your head line by line, rather than doing things by simple trial and error.  knowing how your code runs is one of the fundamental requirements to becoming a competent programmer, and it's kind of the point of this exercise anyhow.

finally, i have fairly little interest in convincing you that i'm right.  i have no stake in this either way.

edit: instead of a continue in your if statement, all you needed was to put the rest of the code in an else clause


Well I didn't know at first that my initial code wasn't working - I had to test it again and found out (hence the edit)

I spent a good time last night and I eventually DID figure out that this supposed 'flow' was not correct. Though I am not quite sure about this flow you speak of - the class I am in is an intro one, and no speak of this flow has been done. Though I will probably figure it out sooner or later.

If my understanding is 'shallow', it is because I have been in an intro class only and for less than 2 months. No other previous experience really. I thought I conveyed this before...

I am not sure if you came here to help or to just call me names - because if I wanted someone to argue ad hominems w/, I would go to general chat or something.

Anyways, I will see if that works...


Code
while (cin >> x) {
       if (x<0)
           cout << "Negative number detected: Ignored." << endl;
       else {
           total *= 1+ rate;
           total += x;
           total +=  x*rate;
       }
   }


works

This post was edited by Eep on Jul 3 2012 04:03pm
Member
Posts: 4,541
Joined: Sep 15 2011
Gold: 10,391.00
Jul 3 2012 05:54pm
my only point is that when you don't know anything, don't tell someone that their code doesn't work, because chances are much, much higher that You're Just Doing It Wrong.

also, google is your friend

control flow:
http://en.wikipedia.org/wiki/Control_flow

edit - sidenote: while i certainly don't/won't get offended if you call me out for being wrong about something (regardless of whether i actually am or not), i reserve the right to laugh at you in the case that i'm not actually wrong (which will be most of the time).

This post was edited by irimi on Jul 3 2012 06:02pm
Go Back To Programming & Development Topic List
Prev123456Next
Add Reply New Topic New Poll