help

This is a discussion on help within the C++ Programming forums, part of the General Programming Boards category; what is wrong with my code im trying to- 1. calculate all the registrants using a counter 2.calculate the amount ...

  1. #1
    yuh
    yuh is offline
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    help

    what is wrong with my code

    im trying to-
    1. calculate all the registrants using a counter
    2.calculate the amount using a accumulator
    3.it has to be in a while loop

    Code:
    #include<iostream>
    
    using namespace std;
    
    int main()
    {
    	//Variables
    	short regs = 0;
    	short count = 0;
    	double average = 0.0;
    	double amtDue = 0.0;
    	
    	//Input
    	cout<<"Enter the amount of company registrants: ";
    	cin>>regs;
    
    	while (regs > 0)
    	{
    		if (regs >= 1)
    			count++;
    			amtDue= regs*150;
    		if (regs >= 4)
    			count++;
    			amtDue= regs*100;
    		if (regs >=10)
    			count++;
    			amtDue= regs*90;
    
    		cout<<"Enter the amount of next company registrants: ";
    		cin>>regs;
    	}
    
    	//Calculations
    	average= amtDue/count;
    	
    
    	//Output
    	cout<<"Total Amount of Registrants:"<<count<<endl;
    	cout<<"Total Amount Due $ "<<amtDue<<endl;
    	cout<<"Average Per Registrant $ "<<average<<endl;
    
    	return 0;
    
    }

  2. #2
    yuh
    yuh is offline
    Registered User
    Join Date
    Mar 2010
    Posts
    2
    anyone?

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    If you want to do more than one statement in an "if" statement, you must surround it by brackets. So, for example,
    Code:
    		if (regs >= 1)
    			count++;
    			amtDue= regs*150;
    should be
    Code:
    		if (regs >= 1)
    		{
    			count++;
    			amtDue= regs*150;
    		}
    Also, you're going to have problems after you fix that. For example, if I enter regs as 11, then "regs >= 1" is true so that block gets executed. "regs >= 4" is also true so that gets executed. Finally, "regs >= 10" is also true so that gets executed. You probably only want one of these to get executed. To do that, I'll give a hint that you could use "if/else" statements and change the order of these statements, so that exactly 0 or 1 of them is true for each iteration of the while loop.

    EDIT: Also, How To Ask Questions The Smart Way.
    Last edited by nadroj; 03-08-2010 at 11:50 PM.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21