• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C programming problem - I need direction.

Scrapster

Diamond Member
Some of you have tried to help me in different ways. I'm very confused at this point. My code looks nasty and my output isn't turning out the way it's supposed to be. I'd appreciate if you guys could critique me a little on how I can add a little style, and less mess. This is my first function program. The only things we are allowed to use in this program are loops(for, while, and do while), functions (basic), and if/else statements. I've basically wasted the last 3 hours trying to mess around with it, but I seem to mess it up even more, the more I tinker.

My code

Sample program - This is what my program is SUPPOSED to do (If you compare the output values for my code and the samples, they are semi-close but not the same, this is where alot of my confusion is).

If you guys want to take a look at it, pass along a few suggestions on improvement. Feel free to /*comment*/ on specifics. My email address is: scrapster@scrappyabs.com

I'm on my way to a classmates house to see if we can work out our problems. I'll check back later.

 
int option, aprNum, result, i, periodNum, option, n = 0;
double apr, principal, deposit, running_total, interest;

for one thing only n is given a default value. Why not initialize all of your variables?
 
I noticed this last time but didn't comment on it - how did you get away with declaring "option" twice? (near the beginning)
int option, aprNum, result, i, periodNum, option, n = 0;
It gave me (and should've given you) an error when trying to compile it.

I also notice that you're declaring way too many global variables (variables that aren't declared inside any function are considered global).

I think you're being a little too careful with variables, for example:
in the beginning, you declare n as a global int (int option, aprNum, result, i, periodNum, option, n = 0;)

then, in the function 'prepare_counter',
int prepare_counter (void) {
if(periodNum == 1) {
n = 1;
}
if(periodNum == 2) {
n = 12;
}
....
return n;
}

Since you're working with a global variable (any part of the program can access a global variable), why would you bother having this function return it?

Then, in the program itself, it calls calls the function, and assigns the return value to n. If you understand what I'm saying, you've basically just assigned the variable a number, that number gets returned and assigned to itself. What I'm saying is, either you make a variable just to use in the function (it would go after the int prepare_counter (void) { line), and have it return that (the most common way things are done in C), or if you want the function to just work with the global variable directly, don't bother having the function return anything (change int prepare_counter (void) { to void prepare_counter(void) {, replace all the return n;'s with return in the function, and get rid of the n = PrepareCounter line).


I think I might've been confusing here, so I'll stop, wait for this to be thought over, and field questions.
 
I can't help you-but I am very interested in C programming, and I want to bump this thread for the sake of your program
 
Loose the globals. Make those local variables. You need to be passing in variables and returning variables in your functions.

int x = enterAPR(); //You need to catch the return.
int y = period_menu(); //Same here
double z = apr_total( aprNum, periodNum ); //Pass in those variables and catch the double return

Why are you calling main() from within main()?????
 
gittyup: in the program, main is being used as a recursive function....it would've made more sense as a loop, but it works, so its irrelevant
 
Back
Top