Thread: C++ calculations

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    10

    C++ calculations

    In this part I need to have a calculation that mutiplies by 2 in order to display the Purchase amount. I know I need to have another array I just don't know where to put it or how to address. I have written a comment of an if else that I thought would work but I am not quite sure.
    Code:
    #include<iostream>
    #include<iomanip>
    #include<fstream>
    #include <stdlib.h> 
    using namespace std;
    
    
    
    struct info_struct
    {
    	char ID[5];
    	int number_year;
    	int number_CD;
    };
    
    
    
    void Display(info_struct[], int);
    void Passing(info_struct[], int);
    
    int main()
    {
    	
    	int total_on_hand = 0;
    //	int i;
    	char select;
    	cout << setprecision(2)
    		<< setiosflags(ios::fixed)
    		<< setiosflags(ios::showpoint);
    
    
    
    	info_struct info[4] = 
    	{{"M123" , 2, 3},
    	{"M225", 1, 6},
    	{"M248", 2, 1},
    	{"M552", 3, 5}};
    	do
    	{
    	
    	cout << "\n";
    	cout << "Mr. Muzik's Fanatastic Music Club\n";
    	cout << "==================================\n";
    	cout << "A - Display Annual Report\n";
    	cout << "B - Input Information\n";
    	cout << "Please make a selection: ";
    	cin >> select;
    	cin.ignore(80, '\n');
    	switch(select)
    	{
    	case 'A':
    	case 'a':
    		
    		
    			 Display(info, 4);
    			
    		
    		break;
    	
    
    	case 'B':
    	case 'b':
    			Passing(info, 4);
    			
    
    		break;
    	default:
    		cout << endl << endl
    			<< "Invalid Property Code! Try again.\n" << endl;
    		break;
    
    
    	}
    	}while((select != 'a' || select != 'A') && (select != 'B' || select != 'b'));
    	return 0;
    }
    
    
    
    
    
    void Display(info_struct NEW[], int size)
    {
    			//int Pur[4];
    			int i;
    			int total_on_hand = 0;
    			cout << endl << endl;
    		    cout << setw(20)<< "ID" 
    			 << setw(20) << "Number of Year"
    			 << setw(20) << "Number of CD bought" 
    			 << setw(20) << "Purchase\n" << endl;
    
    
    			for (i = 0; i < size; ++i)
    			{
    				/*
    				if(NEW[i].number_year < NEW[i].number_CD)
    				{
    
    				total_on_hand = NEW[i].number_year * 2;
    				}
    			else if(NEW[i].number_year > NEW[i].number_CD)
    			{
    				total_on_hand = NEW[i].number_CD * 2;
    				}
    				*/
    				cout << endl;
    				cout << setw(20)<< NEW[i].ID 
    					 << setw(20)<< NEW[i].number_year 
    					 << setw(20)<< NEW[i].number_CD;
    	
    			 
    				 
    			 }
    }
    void Passing(info_struct NEW_2[], int size_2)
    {
    	char file_name[81];
    	int i;
    
    	  cout << "\nEnter the name of the file you want to write to: ";
      cin.getline(file_name, 81);
    
    	 ofstream out_file(file_name);
      if (!out_file)
        {
          cout << "\nERROR: File could not be opened.";
            exit(1);
          }
    
    out_file << endl << endl;
    		    out_file << setw(20)<< "ID" 
    			 << setw(20) << "Number of Year"
    			 << setw(20) << "Number of CD bought"  << endl;
    
    			for (i = 0; i < size_2; ++i)
    			{
    				out_file << endl;
    				out_file << setw(20)<< NEW_2[i].ID 
    
    					 << setw(20)<< NEW_2[i].number_year 
    					 << setw(20)<< NEW_2[i].number_CD  << endl;
    		
    	}
    	
    }
    I posted the whole code so that you can see how it falls into place and the if else is in the Display Funtion

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    First, you need to repair the broken indentation.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Doing calculations on a data file
    By bassist11 in forum C Programming
    Replies: 2
    Last Post: 03-30-2009, 07:47 PM
  2. calculations with pointers
    By mackieinva in forum C Programming
    Replies: 2
    Last Post: 09-17-2007, 01:46 PM
  3. C program compile but not doing the calculations
    By abs.emailverify in forum C Programming
    Replies: 8
    Last Post: 11-08-2006, 08:43 AM
  4. simple arithmetic calculations
    By viciousv322 in forum C Programming
    Replies: 8
    Last Post: 11-18-2005, 07:13 AM
  5. How do I get these calculations correct?
    By nadeni0119 in forum C++ Programming
    Replies: 10
    Last Post: 04-07-2003, 11:09 AM