1) An overview of what your program does.

The entire program asks the user for a starting balance, then deposits and withdrawals, then asks the amounts of each, and then prints a record of all transactions and an ending balance.

The area i'm having difficulty with is the part where the user enters the amount of deposits.

in main it calls my function deposits() which looks like:

int deposits ()
{
int number_of_deposits;

do
	{

        printf ("\n\nEnter the number of deposits: ");
		scanf	("%i", &number_of_deposits);
		fflush(stdin);

		if (number_of_deposits > 50)
			  printf ("*** Too many deposits. ***\n");
		if (number_of_deposits < 0)
			  printf ("*** Negative number of deposits not allowed. ***\n");

	} while ( number_of_deposits > 50 || number_of_deposits < 0);

 return number_of_deposits;
  }

then it asks the user for the amount of the deposits:

for (i = 0; i < number_of_deposits; i++ )
	{
		do
		{
			printf ("Enter the amount of Deposit #%i: ", i+1 );
			scanf	("%f", &amount_to_deposit[i]);
			fflush(stdin);

			if (amount_to_deposit[i] <= 0)
			  printf ("*** Deposit amount must be positive, please re-enter ***\n");

		} while (amount_to_deposit[i] <= 0);

		current_balance = current_balance + amount_to_deposit[i];


	} /* end for loop */

2) The result of your current code.

The for loop that asks the user for amount of each deposit just keeps looping forever.

for some reason the "number_of_deposits" in my for loop isn't the same as the number the user entered when the function was called.


3) What you expected your code to do.

As you can see I want the number that the user enters when the "deposits" function is called to be stored in the variable "number_of_deposits" so that my for loop will actually exit.

I've tried rewriting my function a number of different ways but the result seems to be the same.

Recommended Answers

All 5 Replies

Are you calling in main the function deposits() like this?:

int number_of_deposits;

number_of_deposits = deposits();

fflush(stdin); is a no-no. Here's a link why not to use it.

no I wasn't, I was just calling the function up. Looks like that's fixed it, thanks!

about the fflush, I know its wrong but I guess since I'm a beginner it's easier to just tell me to use that while learning the basics since portability will be an issue later, but for now everyone is using the same compiler so all is well and you can add that to the article which lead me to this site, "What Are They Teaching C Programmers These Days?"

no I wasn't, I was just calling the function up. Looks like that's fixed it, thanks!

Yes, you were just missing a variable that would hold the value passed by the return of the function.

about the fflush, I know its wrong but I guess since I'm a beginner it's easier to just tell me to use that while learning the basics since portability will be an issue later, but for now everyone is using the same compiler so all is well and you can add that to the article which lead me to this site, "What Are They Teaching C Programmers These Days?"

I certainly understand what you are saying. However , if I may, I would like to encourage you to try to learn without the use of this flushing practice.
Why? Would you ask.
Because it creates a habit and a process of thinking that is different that the process to do right source files.

For example. If all that you are concern with is the flushing of the stdin buffer you could just write a little function to do just that:

void flush_it( void )
{
    int ch;
    
    do
    {
        ch = getchar();
    }
    while( ch != '\n' && ch != EOF );
}

However the concern is not if we should flush or not to flush. I'll say that the concern should be the way we would obtain the input from the user.

about the fflush, I know its wrong but I guess since I'm a beginner it's easier to just tell me to use that while learning the basics since portability will be an issue later...

And after you've learned to use it, and after time forget that it's wrong, what are you going to tell your boss when the program doesn't work with their compiler when you get a job? Tell him "it always worked before!"?

... but for now everyone is using the same compiler so all is well and you can add that to the article which lead me to this site, "What Are They Teaching C Programmers These Days?"

As my mom always said, "if all your friends jumped off the Empire State Building, would you do it too?"

And after you've learned to use it, and after time forget that it's wrong, what are you going to tell your boss when the program doesn't work with their compiler when you get a job? Tell him "it always worked before!"?

As my mom always said, "if all your friends jumped off the Empire State Building, would you do it too?"

no, I'll tell my hypothetical boss that's the way I was taught it so if it doesn't work to blame professor Sears.

and I was saying that everyone used the same compiler to show why the professor isn't worried about us using fflush.

We don't get docked for not using it, but we DO get taken off for using things we haven't been taught, so I'm stuck using scanf which leads to fflushing I guess.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.