Prev Topic | Next Topic > Reply | Forward | Delete < Prev Message | Next Message > hi. working on a assignment. and wrote a program. now i navigate to my directory where the file is : in the command prompt i do: g++ -o file namethen file name exe. like for example g++ -o Cash Cash.cpp and press enter. it compiles no errors. but when i try to run the program and Tyep Cash in the command prompt. the program does not run. just the file name hangs. will post the session details for the current session. as learning to program in c++ using the g cc ++ compiler. and using text pad to write the code and using the command prompt to compile and run. will paste my code and the session details plus the exercise. if any one can help me out. that would be gredat. tried writing this program a couple of times under different file names, but get the same result. maybe my logic is flawed. /* Program: Cash Register. File: Cash.cpp Description: Cash Register Program. Author: Marvin Hunkin. Revision: 1.00 05/08/2009 First release. */ // iostream is needed for the cout statement. #include <iostream> using namespace std; //Declare functions float initialise(); float tax(float value); void total(float value); int main() { //call functions in the main function initialise(); tax(); total(); } //close main function float initialise() { char item; int itemNumber; int counter; cout<< "Welcome to Marvin's Cash Register Program\n"; //for loop goes here. for (int counter = 0; counter <=6; counter++) { cout<< "Enter a item and then press enter\n"; cin>> item; cout << "Enter a item number and then press enter\n"; cin>> itemNumber; } //close for loop } //close initialise() function float tax(float value) { float tax = 0.1; char item; float total = 0; //if block goes here. if (item >=10) { total = tax * total; } // close if block return value; } // close tax() function void total(float total) { cout << "Total $" << total << endl; } // close total() function -------------------------------------------------------------------------------- Session 10 Cohesion and Coupling -------------------------------------------------------------------------------- Objectives The purpose of this session is to introduce you to the concept of cohesion and coupling and what these concepts mean when writing a C++ program. -------------------------------------------------------------------------------- Last session we looked at how to write a function. This session we are going to look at how to make better functions. There are a number of objectives when deciding what to put into a function and what not to put into a function. Good function design states that functions should be loosely coupled and be tightly cohesive. Cohesion Cohesion is the measure by which the statements within a function relate to each other. The statements in a function should only perform one task. Most of the programs that we have written so far have only had one function and that was the main function. In our examples so far, the main function has displayed the title information, gathered user input, processed data and displayed the output. To write good programs, each of our functions should only perform one task each. To demonstrate the concept lets use a practical example. We are going to write a program that keeps track of a persons bank account. With a bank account we do a number of tasks. We withdraw money, deposit and make a balance enquiry. The following code does the job but suffers from a lack of cohesion. /* * Program: Cohesion and Coupling example - 1. * File: cohesCoup1.cpp * Description: This program is an exercise to demonstrate cohesion in a program. * Author: Henry Bush * Revision: 1.00 2/12/2003 First release. */ // iostream is needed for the cout statement. #include <iostream>using namespace std; int main() { float balance = 0.0; float amount = 0.0; char option; cout << "Welcome to the Bank Account Manager\n\n"; cout << "Enter the account balance: $"; cin >> balance; cout << "w = withdrawal, d = deposit, b = account balance, x = exit program" << endl; cout << "Type in one of the choices from the list above "; cin >> option; while(option != 'x') { switch(option) { case 'b': cout << "\nThe account balance is: $" << balance << endl; break; case 'd': cout << "Type in the amount that is being deposited: $"; cin >> amount; balance += amount; break; case 'w': cout << "Type in the amount that is being withdrawn: $"; cin >> amount; balance -= amount; break; case 'x': exit(0); break; default : cout > option<< " is not a valid value try again "; break; } // close switch block cout << "\nType in one of the choices (w, b, d, x) "; cin >> option; } // close while loop } // close main function Lets look at the program and see how we can rewrite the program in a much better manner. The first thing to do is to analyse the tasks that are performed in the program. Obviously, there are the balance, deposit and withdrawal tasks, but if we look closer at the program we can see that there is some beginning programing stuff and a choice task. So if we add these tasks up we then have 5 tasks and we can write the program with 5 functions. We can start off by writing the blank functions and declaring them as in the following /* * Program: Cohesion and Coupling example - 1. * File: cohesCoup1.cpp * Description: This program is an exercise to demonstrate cohesion in a program. * Author: Henry Bush * Revision: 1.00 2/12/2003 First release. */ // iostream is needed for the cout statement. #include <iostream>using namespace std; // Declare the functions. void initialise(); void deposit(); void withdrawal(); void accountbalance(); void input(); int main() { } // close main function void initialise() { } // close initialise function void deposit() { } // close deposit function void withdrawal() { } // close withdrawal function void accountbalance() { } // close balance function void input() { } // close input function At this stage there are no parameters and the return type is void. Lets start with adding the variables that we need for this program. They were float balance, float amount and char option, we will add them above the main function. The reason for this will be explained later this session. The initialise function is where we are going to display the title, get user input for the account balance and prime the while loop. Yes, I can already here you saying that is three things, but for this example it will be OK to call all of this initialising the program. The function should look like the following void initialise() { cout << "Welcome to the Bank Account Manager\n\n"; cout << "Enter the account balance: $"; cin >> balance; cout << "w = withdrawal, d = deposit, b = account balance, x = exit program" << endl; cout << "Type in one of the choices from the list above "; cin >> option; } // close initialise function The Deposit function is where we make the deposits so add the code to make it look like this void deposit() { cout << "Type in the amount that is being deposited: $"; cin >> amount; balance += amount; } // close deposit function And the withdrawal function will have code like this void withdrawal() { cout << "Type in the amount that is being withdrawn: $"; cin >> amount; balance -= amount; } // close withdrawal function The accountbalance function will display the account balance so it will look like the following void accountbalance() { cout << "\nThe account balance is: $" << balance << endl; } // close balance function The input function is where we will input what the users next choice is like this void input() { cout << "\nType in one of the choices (w, b, d, x) "; cin >> option; } // close input function At this point we only have the main function left and this is where the most noticeable feature is. The main function now only controls the operation of the program and lets all of the other functions do the detailed work. The program will start off with some initialisation and then into a while loop we will put a switch statement and a call to the input function. Modify your main function so that it looks like the following int main() { initialise(); while(option!='x') { switch(option) { case 'b': accountbalance(); break; case 'd': deposit(); break; case 'w': withdrawal(); break; case 'x': exit(0); break; default : cout << option<< " is not a valid value try again "; break; } // close switch block input(); } // close while loop } // close main function One of the big things to note is that main has now become much smaller and easier to read. Programs that are easy to read are also easier to maintain. The program should now look like the following /* * Program: Cohesion and Coupling example - 1. * File: cohesCoup1.cpp * Description: This program is an exercise to demonstrate cohesion in a program. * Author: Henry Bush * Revision: 1.00 2/12/2003 First release. */ // iostream is needed for the cout statement. #include <iostream>using namespace std; // Declare the functions. void initialise(); void deposit(); void withdrawal(); void accountbalance(); void input(); float balance = 0.0; float amount = 0.0; char option; int main() { initialise(); while(option!='x') { switch(option) { case 'b': accountbalance(); break; case 'd': deposit(); break; case 'w': withdrawal(); break; case 'x': exit(0); break; default : cout << option<< " is not a valid value try again "; break; } // close switch block input(); } // close while loop } // close main function void initialise() { cout << "Welcome to the Bank Account Manager\n\n"; cout << "Enter the account balance: $"; cin >> balance; cout << "w = withdrawal, d = deposit, b = account balance, x = exit program" << endl; cout << "Type in one of the choices from the list above "; cin >> option; } // close initialise function void deposit() { cout << "Type in the amount that is being deposited: $"; cin >> amount; balance += amount; } // close deposit function void withdrawal() { cout << "Type in the amount that is being withdrawn: $"; cin >> amount; balance -= amount; } // close withdrawal function void accountbalance() { cout << "\nThe account balance is: $" << balance << endl; } // close balance function void input() { cout << "\nType in one of the choices (w, b, d, x) "; cin >> option; } // close input function It is made of a number of small functions that only perform one task each. This technique makes for programs that are easy to handle as they get larger. With programs of this size it is hard to justify in your own mind why you would bother, after all we have nearly doubled the number of lines of code and have only created the same program. Once your programs reach hundreds of lines of code you will see the benefits of this type of technique. Recapping when you write functions try to put into one function only the code that performs one task. Sometimes you may need to put statements into a function that perform more than one task. In these cases try to put tasks that relate to each other. Another thing to try for is to have your functions small. Try and have then less than one screen full of text. You will not always be able to achieve these guides, but if you work towards them, then you are on the right track. Coupling. Where cohesion relates to the function's internal behavior, coupling relates to how it interacts with the program that it exists in. Functions should be loosely coupled. By that I mean that a function should not rely on statements or variables from outside the function. A well written function should be able to be copied into another program and work without any changes to the function or the program. If we look at the example that we have been working on, we can see that each function depends on the variables that we declared at the beginning of the program. If we took one of the functions out of this program and placed it into another, we would have to modify the function or the program to make them work together. We are going to modify the program again to make the functions loosely coupled. Firstly we will move the variable declarations back into the main function. When they were placed before the main function made them global variables. Global variables are accessible to all the functions in a program. This is handy but very dangerous. It is hard to see at this size of program how it can be a problem. The more parts of a program that can gain access to a variable, the greater the chance of being altered without realising the consequences. To avoid this we use a technique to control the access to variables. By placing the variable declaration inside a function makes them what are called local variables. That means that they are only accessible from within the function. By doing so makes the programs more robust, but it does have the down side of making the programmer have to think more about the way they write their programs. The only way to get data into a function is through the parameter list, and the only way to get data out of a function is through the return type. If we look at the initialise function first. We do not need to pass any variables to the function but we do wish to return the balance entered. So we make the return type a float. When a function returns a value, the calling function must accept the value, as in the following balance = initialise(); Inside this function though we also had the user enter the choice for the next function. We will move this back to the main function. The deposit function needs the balance value and we need to return the new balance. So we need to pass the balance value to the function as a parameter. Within the function we declare a new variable to hold the amount deposited. After performing the calculation we need to return the new balance as in the following float deposit(float value) { float amount = 0.0; cout << "Type in the amount that is being deposited: $"; cin >> amount; value += amount; return value; } // close deposit function The withdrawal function will be almost identical to the deposit function float withdrawal(float value) { float amount = 0.0; cout << "Type in the amount that is being withdrawn: $"; cin >> amount; value -= amount; return value; } // close withdrawal function The account balance function requires the balance value but does not need to return anything as in the following void accountbalance(float value) { cout << "\nThe account balance is: $" << value << endl; } // close balance function The last function is the input, this function will return a value to the option variable and does not have to take any parameters. char input() { char inputValue; cout << "\nType in one of the choices (w, b, d, x) "; cin >> inputValue; return inputValue; } // close input function Put all together the program will look like this /* * Program: Cohesion and Coupling example - 1. * File: cohesCoup1.cpp * Description: This program is an exercise to demonstrate cohesion in a program. * Author: Henry Bush * Revision: 1.00 2/12/2003 First release. */ // iostream is needed for the cout statement. #include <iostream>using namespace std; // Declare the functions. float initialise(); float deposit(float value); float withdrawal(float value); void accountbalance(float value); char input(); int main() { float balance = 0.0; char option; balance = initialise(); cout << "w = withdrawal, d = deposit, b = account balance, x = exit program" << endl; cout << "Type in one of the choices from the list above "; cin >> option; while(option!='x') { switch(option) { case 'b': accountbalance(balance); break; case 'd': balance = deposit(balance); break; case 'w': balance = withdrawal(balance); break; case 'x': exit(0); break; default : cout << option<< " is not a valid value try again "; break; } // close switch block option = input(); } // close while loop } // close main function float initialise() { float value = 0.0; cout << "Welcome to the Bank Account Manager\n\n"; cout << "Enter the account balance: $"; cin >> value; return value; } // close initialise function float deposit(float value) { float amount = 0.0; cout << "Type in the amount that is being deposited: $"; cin >> amount; value += amount; return value; } // close deposit function float withdrawal(float value) { float amount = 0.0; cout << "Type in the amount that is being withdrawn: $"; cin >> amount; value -= amount; return value; } // close withdrawal function void accountbalance(float value) { cout << "\nThe account balance is: $" << value << endl; } // close balance function char input() { char inputValue; cout << "\nType in one of the choices (w, b, d, x) "; cin >> inputValue; return inputValue; } // close input function Make sure that you can understand what is happening here in the program. Cohesion and coupling are important concepts to creating larger workable programs. -------------------------------------------------------------------------------- Session 10 Practical Exercise The practical exercise for this session will be to create a cash register program that you input the cost of an item, the number purchased and if a sales tax applies to the item. If a tax does apply to the item, the tax value will be ten percent of the items cost. Example Item Cost Tax Applies? Cost Plus Tax Bread $2.00 No $2.00 Shirt $20 Yes $22.00 Total $24.00 In the example, the bread costs $2, it does not have sales tax on it, so its final price is $2. The shirt on the other hand, has a cost of $20, and does have tax on it, so ten percent of 20 is 2, making the final price of the shirt $22.00. The program will then calculate the cost of all the items. This will be in a loop so that a number of different items can be inputed and when finished the program it will produce a grand total. If you can complete this exercise without any difficulties then you can proceed to the next lesson. If you found that you had any problems with this exercise then contact your instructor about the problems and attach any working code so that we can discuss where your solutions lay. After you successfully complete this exercise, remember to send your completed code into your instructor for assessment. -------------------------------------------------------------------------------- __________ View the list's information and change your settings at //www.freelists.org/list/programmingblind