Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,108 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,720 people online right now. Registration is fast and FREE... Join Now!




Loops and selection structures statement

 
Reply to this topicStart new topic

Loops and selection structures statement, C program

klogin
16 Oct, 2007 - 05:05 AM
Post #1

New D.I.C Head
*

Joined: 16 Oct, 2007
Posts: 2


My Contributions
Hi everyone. I'm new to C programming. I'm writing a program that will prompt users to enter a number of amounts, the program will checK if amount is >= 0, then if condition is met the loop will continue until the number of amounts is completed, else the program will print " invalid number" and it will repromt the user:

Enter amount #1: 10000
Enter amoun #2: 8000
Enter amoun #3: -20000
*** Invalid entry. amoun must be positive. ***
Enter amount #3: 25000

I think i need two more loop, also work the conditional statements, but i can figure out how to do it.

Thanks,


[#include <stdio.h>



int main(void)
{
/*Declare Variables */

float amount,
float average = 0;
amounTotal = 0;

int NumAmount, counter;

/*Prompt to enter number of Amounts to calculate */

printf( "First, enter the number of amounts to process: " );
scanf ("%i", &NumAmount);
fflush (stdin);

/*Display number of amount to be averaged */

printf( "Now enter %i Amount to be averaged. \n\n" , NumAmount);

/*Start loop*/


for( counter = 1; counter <= NumAmount; ++counter ){

if (amount > 0)


printf( "Enter amount #%i:", counter); /*Prompt for amount*/
scanf("%2f", &amount);
fflush (stdin);
amountTotal += amount;



printf("\n");

if (amount < 0){

printf ( " invalid number:\n " );

}

}



average = amountTotal / NumAmount;

/* Print the results */
printf( "The Average of the %i Amounts entered is %.2f\n", NumAmount, average );

printf ( "The total of the %i Amounts enter are %.2f ", NumAmounts, amountTotal );

getchar();
return( 0 );

} ]

User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Loops And Selection Structures Statement
16 Oct, 2007 - 08:13 AM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,009



Thanked: 5 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Hi ok below is the code I hope you needed, but you'll see that there are no new loops, I just simply used an if/else statement, but the main trick is in the else block where I used counter--;

Here is the code and compare it with yours, you'll see the differences.
Enjoy smile.gif

PS please use code tags when posting code.
CODE
#include <stdio.h>



int main(void)
{
/*Declare Variables */

float amount;
float average = 0;
float amountTotal = 0;

int NumAmount, counter;

/*Prompt to enter number of Amounts to calculate */

printf( "First, enter the number of amounts to process: " );
scanf ("%d", &NumAmount);
fflush (stdin);

/*Display number of amount to be averaged */

printf( "Now enter %d Amount to be averaged. \n\n" , NumAmount);

/*Start loop*/


for( counter = 1; counter <= NumAmount; ++counter )
{

printf( "Enter amount #%i:", counter); /*Prompt for amount*/
scanf("%2f", &amount);
if (amount >= 0)
{


fflush (stdin);
amountTotal += amount;



printf("\n");
}
else if (amount < 0){

printf ( " invalid number:\n " );
counter--;

}

}



average = amountTotal / NumAmount;

/* Print the results */
printf( "The Average of the %i Amounts entered is %.2f\n", NumAmount, average );

printf ( "The total of the %i Amounts enter are %.2f ", NumAmount, amountTotal );

getchar();
return( 0 );

}

User is offlineProfile CardPM
+Quote Post

klogin
RE: Loops And Selection Structures Statement
16 Oct, 2007 - 04:32 PM
Post #3

New D.I.C Head
*

Joined: 16 Oct, 2007
Posts: 2


My Contributions
QUOTE(PennyBoki @ 16 Oct, 2007 - 09:13 AM) *

Hi ok below is the code I hope you needed, but you'll see that there are no new loops, I just simply used an if/else statement, but the main trick is in the else block where I used counter--;

Here is the code and compare it with yours, you'll see the differences.
Enjoy smile.gif

PS please use code tags when posting code.
CODE
#include <stdio.h>



int main(void)
{
/*Declare Variables */

float amount;
float average = 0;
float amountTotal = 0;

int NumAmount, counter;

/*Prompt to enter number of Amounts to calculate */

printf( "First, enter the number of amounts to process: " );
scanf ("%d", &NumAmount);
fflush (stdin);

/*Display number of amount to be averaged */

printf( "Now enter %d Amount to be averaged. \n\n" , NumAmount);

/*Start loop*/


for( counter = 1; counter <= NumAmount; ++counter )
{

printf( "Enter amount #%i:", counter); /*Prompt for amount*/
scanf("%2f", &amount);
if (amount >= 0)
{


fflush (stdin);
amountTotal += amount;



printf("\n");
}
else if (amount < 0){

printf ( " invalid number:\n " );
counter--;

}

}



average = amountTotal / NumAmount;

/* Print the results */
printf( "The Average of the %i Amounts entered is %.2f\n", NumAmount, average );

printf ( "The total of the %i Amounts enter are %.2f ", NumAmount, amountTotal );

getchar();
return( 0 );

}





That's perfect, with a couple of adjustments it works great!. i don't know why i did not think about count--;

Thanks for your help! icon_up.gif



User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 09:26PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month