Thread: Totally and output chosen array fields or switch case total and output

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    9

    Totally and output chosen array fields or switch case total and output

    How do I output only the array fields chosen 8.00 and/or 11.00 etc.. and the cooresponding service names

    if I use a switch statement how do I ouput total the case amts chosen


    Code:
    void main ()
    
    {
    	const int SIZE = 3;
    	double serv[SIZE] = {8.00,12.00,11.00};
    	double tot=0;
    	int a;
    	int numserv;
    	
    	cout<< "0 = Cable"<< endl
    	       << "1 = Home Cinema Channel"<< endl
    	       << "2 = The Captain Cartoon Channel"<< endl;
    	       cout<< " _______________________________________________" <<endl;
    	
    	cout << "How many extra services"<< endl;
    	cin>> numserv;
    	
    	if (numserv > 0)
    	
    	for (int i=1; i<=numserv; ++i)
    	{
    		cout << "Enter service\t"<< i << ":";
    		cin >> a;
    
    		tot+=serv[a];
    	}
    		
    
    	
    
    	
    	cout<<"Cable =\t"<<serv[0]<<endl;
    	cout<<"Home Cinema Channel =\t"<<serv[1]<<endl;
    	cout<<"The Captain Cartoon Channel=\t"<<serv[2]<<endl;
    	
    	cout << "tot"<< setprecision(2) <<tot<< endl;
    
    } 
    
    OR THIS CODE
    
    
    	double tot=0;
    	int numserv;
                    double cost;
    	int serv;
    
    	
    	cout<< "1 = Cable"<< endl
    		<< "2 = Home Cinema Channel"<< endl
    		<< "3 = The Captain Cartoon Channel"<< endl;
    	cout<< " _______________________________________________" <<endl;
    	
    	cout << "How many extra services"<< endl;
    	cin>> numserv;
    
    	if (numserv !=0)
    
    		for (int i=1; i<=numserv; i++)
    		{
    	                   cout << "which service   " << endl;
    	                   cin>> serv;
    
    		switch (serv)
    	                          {
    		case 1:  
    			cost= 8.00;
    			break;
    		case 2:
    			cost = 12.00;
    			break;
    		case 3:
    			cost = 11.00;
    			break;
    		
    		              }
    
    		if (serv == 1)
    		
    		cout << "Cable = "<< cost << endl; 
    		
    		if (serv == 2 )
    
    		cout << "Home Cinema Channel = "<< cost << endl;
    		
    		if (serv == 3)


    And Hammer appears from nowhere to add the code tags

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    Edit your post.
    Use code tags, main returns an int.

    Not sure what you're asking, but if you want a total, put in a variable for total, and in your cases you should be able to put two statements...
    ie
    Code:
    case 1: 
    {
    cost= 8.00;
    total+=8.00;
    }
    break;
    Demonographic rhinology is not the only possible outcome, but why take the chance

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    362
    void main()?????

    It's int main() and we leave it to you to discover why.

    As Azuth points out, code tags. (Hammer will appear shortly...)

    Up to me, and I had to use a 'switch' statement? I'd opt for your second section of code with a variable named 'service' and one named 'cost', placing each within a 'case' element with the appropriate data, i.e.

    Code:
     
    case 1: /*string*/ service = "Cable";
                 cost = 8.00;
                 break;
    case 2: service = "Home Cinema Channel";
                 cost = 12.00;
                 break;
    etc.
    Can't help but feel that you're beating this one to death unnecessarily, though, like Azuth, I'm not sure exactly where you're going with this.

    Sorry.

    -Skipper
    "When the only tool you own is a hammer, every problem begins to resemble a nail." Abraham Maslow

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    9
    This program is suppose to allow the user to chose the services that apply. Then services choosen and amts are to be displayed along with a total of the services.

Popular pages Recent additions subscribe to a feed