Large project how to seprate definition and declaration?
-
This was the whole code from main file. Please divide it in module form like professional people do so that I can exactly see the problem in my understanding of dividing the project into moduler form
/*
Name = Syed Sibt E Hassan
Registration Number = MC160201226- Input from the user
- Function Calls to reactangle and trapezoid
- Exit loop when input is not 'Y' || 'y'
*/
#include
using namespace std;float rectangleArea (float , float); //Declaration for Rectangle Area
float trapezoidArea (float , float, float); //Declaration for Trapezoid Area.int main()
{
int choice = 0;
char tryagain = 0;
do
{
cout << "\n\nEnter 1 to calculate the area of Rectangle" << endl;
cout << "Enter 2 to calculate the area of a Trapezoid" << endl;
cout << "\n\nEnter your choice : ";
cin >> choice;
switch(choice)
{
//Dealing with area of Rectangle.
case 1:
{float width = 0.0, length = 0.0, answer = 0.0; cout << "Enter the width of rectangle :"; cin >> width; cout << "Enter the length of rectangle :"; cin >> length; answer = rectangleArea(width, length); //Function Call to Rectangle cout << "The area of Rectangle is : " << answer << endl; break; } //Dealing with area of Trapezoid. case 2: { float base1 = 0.0, base2 = 0.0, height = 0.0, answer = 0.0; cout << "Enter base1 of trapezoid : "; cin >> base1; cout << "Enter base2 of trapezoid : "; cin >> base2; cout << "Enter the height of trapezoid : "; cin >> height; answer = trapezoidArea(base1, base2, height); //Function Call to Trapezoid cout << "The area of Trapezoid is : " << answer << endl; break; } default:{ cout << "Neither 1 nor 2 entered." << endl; } } cout << "\\n Do you want to do another calculation? : "; cin >> tryagain; }while (tryagain == 'Y' || tryagain == 'y'); return
You are not thinking like a programmer ... at the moment your answers are not needed as you just display them to the console So one easy option to patch your code in Calculation.CPP is this
void input_Area(void) // ** Pt1 here you declare you are going to return nothing AKA a void
{
float width = 0, length = 0, answer = 0;
cout << "Enter the width of rectangle :";
cin >> width;
cout << "Enter the length of rectangle :";
cin >> length;
answer = rectangleArea(width, length);
cout << "The area of Rectangle is : " << answer << endl;
}That matches your .H header file you don't have to change anything
#ifndef _CALCULATION_H
#define _CALCULATION_Hvoid input_Func(void); //Input function of text.
void input_Area(void); //Input function of Area. **** FIXED NOW MATCHES THE .CPP file
void input_Trap(void); //Input function of Trapezoid. *** STILL BUGGED .CPP says it returns a float this says void
float rectangle_Area (float , float); //Declaration for Rectangle Area.
float trapezoid_Area (float , float, float); //Declaration for Trapezoid Area.#endif
So you could go and fix input_Trap in the same way I am leaving that as an exercise .. problem fixed. So there is another alternative which is to return the answer in Calculation.cpp
float input_Area(void) // ** Pt1 here you declare you are going to return a float
{
float width = 0, length = 0, answer = 0;
cout << "Enter the width of rectangle :";
cin >> width;
cout << "Enter the length of rectangle :";
cin >> length;
answer = rectangleArea(width, length);
cout << "The area of Rectangle is : " << answer << endl;return (answer); // * So return the answer like you said you would at Pt1
}Now you need to fix the header file Calculation.H
#ifndef _CALCULATION_H
#define _CALCULATION_Hvoid input_Func(void); //Input function of text.
float input_Area(void); //Input function of Area. **** FIXED NOW MATCHES THE .CPP file above
void input_Trap(void); //Input function of Trapezoid. *** STILL BUGGED .CPP says it returns a float this still says void
float rectangle_Area (float , float); //Declaration for Rectangle Area.
float trapezoid_Area (float , float, float); //Declaration for Trapezoid Area.#endif
So you could go and fix input_Trap in the same way I am leaving that as an exercise .. problem fixed again. Look carefully at both answers all that changed is whether you return answer or not. In both cases the .H f
-
This was the whole code from main file. Please divide it in module form like professional people do so that I can exactly see the problem in my understanding of dividing the project into moduler form
/*
Name = Syed Sibt E Hassan
Registration Number = MC160201226- Input from the user
- Function Calls to reactangle and trapezoid
- Exit loop when input is not 'Y' || 'y'
*/
#include
using namespace std;float rectangleArea (float , float); //Declaration for Rectangle Area
float trapezoidArea (float , float, float); //Declaration for Trapezoid Area.int main()
{
int choice = 0;
char tryagain = 0;
do
{
cout << "\n\nEnter 1 to calculate the area of Rectangle" << endl;
cout << "Enter 2 to calculate the area of a Trapezoid" << endl;
cout << "\n\nEnter your choice : ";
cin >> choice;
switch(choice)
{
//Dealing with area of Rectangle.
case 1:
{float width = 0.0, length = 0.0, answer = 0.0; cout << "Enter the width of rectangle :"; cin >> width; cout << "Enter the length of rectangle :"; cin >> length; answer = rectangleArea(width, length); //Function Call to Rectangle cout << "The area of Rectangle is : " << answer << endl; break; } //Dealing with area of Trapezoid. case 2: { float base1 = 0.0, base2 = 0.0, height = 0.0, answer = 0.0; cout << "Enter base1 of trapezoid : "; cin >> base1; cout << "Enter base2 of trapezoid : "; cin >> base2; cout << "Enter the height of trapezoid : "; cin >> height; answer = trapezoidArea(base1, base2, height); //Function Call to Trapezoid cout << "The area of Trapezoid is : " << answer << endl; break; } default:{ cout << "Neither 1 nor 2 entered." << endl; } } cout << "\\n Do you want to do another calculation? : "; cin >> tryagain; }while (tryagain == 'Y' || tryagain == 'y'); return
Your code is not in a fit state to put in modules .. however I have provided code that does roughly what you want in modules. The code includes error checks on the entry you don't do in any way. File: Calculation.H
#ifndef _CALCULATION_H
#define _CALCULATION_Hfloat RectangleArea (float width, float length);
float TrapezoidArea (float base1, float base2, float height);#endif
File: Calculation.CPP
#include "Calculation.h"
//Calculation of Rectangle Area.
float RectangleArea(float width, float length)
{
float Area_Rectange;
Area_Rectange = width * length;
return (Area_Rectange);
}//Calculation of Trapezoid Area.
float TrapezoidArea(float base1, float base2, float height)
{
float Area_Trapezoid;
Area_Trapezoid = height * (base1 + base2) / 2;
return (Area_Trapezoid);
}File: Input.H
#ifndef _INPUT_H
#define _INPUT_H/* Asks user for menu choice up to given max value */
unsigned int MenuInput (char* prompt, unsigned int maxNum);/* Asks user for a float within given range, with prompt text */
float GetFloatInput(char* prompt, float min, float max);#endif
File: Input.CPP
#include #include "input.h"
/* Asks user for menu choice up to given max value */
unsigned int MenuInput(char* prompt, unsigned int maxNum) {
bool validFlag = false;
unsigned int iResult;
do {
printf(prompt); // Put prompt on screen
int err = scanf_s("%u", &iResult); // Scan for a unsigned int and hold any error
if ((err == 0) || (iResult > maxNum)) { // Check value between 0 .. maxNum
printf("Invalid menu choice try again\n"); // If value is invalid .. prompt that
} else validFlag = true; // Value is valid
scanf_s("%*[^\n]"); // Make sure we dump any characters in buffer
} while (validFlag == false); // Loop until valid
return (iResult); // Return result
}/* Asks user for a float within given range, with prompt text */
float GetFloatInput (char* prompt, float min, float max) {
int err;
float fResult;
do {
printf(prompt); // Put prompt on screen
err = scanf_s("%f", &fResult); // Scan for a float and hold any error
if (err == 0) { // Err = zero means entry is not a float
printf("Entry is not a float try again\n"); // Prompt entry was not a float to screen
} else {
if (fResult < min) { // Check entry is above minimum
printf("Re-enter value below minimum of %f\n", min);// Prompt -
Your code is not in a fit state to put in modules .. however I have provided code that does roughly what you want in modules. The code includes error checks on the entry you don't do in any way. File: Calculation.H
#ifndef _CALCULATION_H
#define _CALCULATION_Hfloat RectangleArea (float width, float length);
float TrapezoidArea (float base1, float base2, float height);#endif
File: Calculation.CPP
#include "Calculation.h"
//Calculation of Rectangle Area.
float RectangleArea(float width, float length)
{
float Area_Rectange;
Area_Rectange = width * length;
return (Area_Rectange);
}//Calculation of Trapezoid Area.
float TrapezoidArea(float base1, float base2, float height)
{
float Area_Trapezoid;
Area_Trapezoid = height * (base1 + base2) / 2;
return (Area_Trapezoid);
}File: Input.H
#ifndef _INPUT_H
#define _INPUT_H/* Asks user for menu choice up to given max value */
unsigned int MenuInput (char* prompt, unsigned int maxNum);/* Asks user for a float within given range, with prompt text */
float GetFloatInput(char* prompt, float min, float max);#endif
File: Input.CPP
#include #include "input.h"
/* Asks user for menu choice up to given max value */
unsigned int MenuInput(char* prompt, unsigned int maxNum) {
bool validFlag = false;
unsigned int iResult;
do {
printf(prompt); // Put prompt on screen
int err = scanf_s("%u", &iResult); // Scan for a unsigned int and hold any error
if ((err == 0) || (iResult > maxNum)) { // Check value between 0 .. maxNum
printf("Invalid menu choice try again\n"); // If value is invalid .. prompt that
} else validFlag = true; // Value is valid
scanf_s("%*[^\n]"); // Make sure we dump any characters in buffer
} while (validFlag == false); // Loop until valid
return (iResult); // Return result
}/* Asks user for a float within given range, with prompt text */
float GetFloatInput (char* prompt, float min, float max) {
int err;
float fResult;
do {
printf(prompt); // Put prompt on screen
err = scanf_s("%f", &fResult); // Scan for a float and hold any error
if (err == 0) { // Err = zero means entry is not a float
printf("Entry is not a float try again\n"); // Prompt entry was not a float to screen
} else {
if (fResult < min) { // Check entry is above minimum
printf("Re-enter value below minimum of %f\n", min);// Prompteven your code giving error. Have a look at screenshot please. However, Thank you for showing me the correct way to code. I got many point out of it. Screen Shot of Error = http://i64.tinypic.com/16m0zr4.jpg
-
even your code giving error. Have a look at screenshot please. However, Thank you for showing me the correct way to code. I got many point out of it. Screen Shot of Error = http://i64.tinypic.com/16m0zr4.jpg
Something funky with GCC compiler but easy to fix include float.h ... its doesn't know what FLT_EPSILON, FLT_MAX are. These are standard C++ Macro constant definitions so you can just look it up (float.h) - C++ Reference[^] So top of Source.CPP becomes
#include
#include // Added to define FLT_EPSILON & FLT_MAX
#include "calculation.h"
#include "input.h"
using namespace std;In vino veritas
-
Something funky with GCC compiler but easy to fix include float.h ... its doesn't know what FLT_EPSILON, FLT_MAX are. These are standard C++ Macro constant definitions so you can just look it up (float.h) - C++ Reference[^] So top of Source.CPP becomes
#include
#include // Added to define FLT_EPSILON & FLT_MAX
#include "calculation.h"
#include "input.h"
using namespace std;In vino veritas
More errors :( Thank you for your guidence at least i am learning new things. Screen Shot = http://i65.tinypic.com/ncjddc.jpg[^]
-
More errors :( Thank you for your guidence at least i am learning new things. Screen Shot = http://i65.tinypic.com/ncjddc.jpg[^]
That one is you ... It is basically telling you it can't find the code for GetFloatInput. GetFloatInput is defined in Input.H and Input.CPP which are included see here
#include
#include "calculation.h"
#include "input.h" // <----- GetFloatInput is defined in this include>
using namespace std;So we are down to either a typo error or you haven't included Input.CPP into your source files. I assume you cut and paste so unlikely to be a typo so lets try number two. Did you use the menu system and do .... Project... Add files... to add the new .H and .CPP to your source files to the project? I don't use code blocks but it will be something like that. Can I ask you to show me the line about the deprecated string function points to .. I am interested to see what GCC is complaining about. There is possibly another warning you are going to get on the definition of main in Source.CPP can you change it from int main() to
int main(int argc, char**argv)
That is the technically correct definition, not the Microsoft shorthand I used.
In vino veritas
-
That one is you ... It is basically telling you it can't find the code for GetFloatInput. GetFloatInput is defined in Input.H and Input.CPP which are included see here
#include
#include "calculation.h"
#include "input.h" // <----- GetFloatInput is defined in this include>
using namespace std;So we are down to either a typo error or you haven't included Input.CPP into your source files. I assume you cut and paste so unlikely to be a typo so lets try number two. Did you use the menu system and do .... Project... Add files... to add the new .H and .CPP to your source files to the project? I don't use code blocks but it will be something like that. Can I ask you to show me the line about the deprecated string function points to .. I am interested to see what GCC is complaining about. There is possibly another warning you are going to get on the definition of main in Source.CPP can you change it from int main() to
int main(int argc, char**argv)
That is the technically correct definition, not the Microsoft shorthand I used.
In vino veritas
Excellent. Working fine now however I have to remove scanef_s. that _s was creating problem so i only remove _S and scanef remain as it. Now code working fine. Thank you for ur guidance. Now i got the clear idea how to write the code. I will copy the way you wrote this code. I am not a bad student however I like programming but problem is I think i am unable to think like a programmer. I am trying hard for that but no success yet :( I want to excel in this field but sometimes I stuck :((
-
Excellent. Working fine now however I have to remove scanef_s. that _s was creating problem so i only remove _S and scanef remain as it. Now code working fine. Thank you for ur guidance. Now i got the clear idea how to write the code. I will copy the way you wrote this code. I am not a bad student however I like programming but problem is I think i am unable to think like a programmer. I am trying hard for that but no success yet :( I want to excel in this field but sometimes I stuck :((
We all get stuck at times :-) It is really pleasing to see you are willing to apply yourself to learn. None of us are born able to think like a programmer it is like any profession something you get good at by application of lots of time and effort. Your long single file code is sort of normal basic student level code, it works in a fashion. You are now seeking to move beyond that and I can only encourage that. All I did was really shorten up your code by making functions with more parameters and putting in error checking for things like typing letters instead of numbers etc. There are improvements you could do by using objects or classes but that is more stuff you are yet to learn. I looked up the problem with scanf_s it is they have changed security with this, you now need to use "%hu" rather than "%u" as meaning a pointer to an unsigned short. That tells you how many years since I have written code for a console application :-) So there are two fixes to remove the warning correctly
unsigned short iResult; // Define iResult as unsigned short not unsigned int
int err = scanf_s("%hu", &iResult); // Use %hu this should be error free on GCC
In vino veritas
-
We all get stuck at times :-) It is really pleasing to see you are willing to apply yourself to learn. None of us are born able to think like a programmer it is like any profession something you get good at by application of lots of time and effort. Your long single file code is sort of normal basic student level code, it works in a fashion. You are now seeking to move beyond that and I can only encourage that. All I did was really shorten up your code by making functions with more parameters and putting in error checking for things like typing letters instead of numbers etc. There are improvements you could do by using objects or classes but that is more stuff you are yet to learn. I looked up the problem with scanf_s it is they have changed security with this, you now need to use "%hu" rather than "%u" as meaning a pointer to an unsigned short. That tells you how many years since I have written code for a console application :-) So there are two fixes to remove the warning correctly
unsigned short iResult; // Define iResult as unsigned short not unsigned int
int err = scanf_s("%hu", &iResult); // Use %hu this should be error free on GCC
In vino veritas
You are right. i am completely new and it was my class assignment number 1. I finsihed it and submitted it already but then I came to know professionals never write everything in main that's why i decided to search in this direction. By your improved code I can see a boundery wall is set for wrong inputs. Which i wanted to set in my project as well but was not sure how to set those powerful boundaries as you set. I want to excel in this game of programming, I am not an extera ordinary student but i will keep on trying it day by day :) Following are few things I extracted from ur code which I am going to follow for the next time code or will at least try my best to do similar things. Correct me if I observed wrong. I did not follow the following things in my code. If I missed any point then point me that as well. 1) You are using braces by giving one space and starting 1st curly brace in-front of the statements like while and if. 2) All variable you wrote in small letters and to make them more clear u out _ sign to link them. 3) Function name 1st character and then the 2nd meaning name of function is capital. 3) 1 step indentation on each step involving in some kind of computation. 4) You used unsigned when you feel that value are in +ve.
-
You are right. i am completely new and it was my class assignment number 1. I finsihed it and submitted it already but then I came to know professionals never write everything in main that's why i decided to search in this direction. By your improved code I can see a boundery wall is set for wrong inputs. Which i wanted to set in my project as well but was not sure how to set those powerful boundaries as you set. I want to excel in this game of programming, I am not an extera ordinary student but i will keep on trying it day by day :) Following are few things I extracted from ur code which I am going to follow for the next time code or will at least try my best to do similar things. Correct me if I observed wrong. I did not follow the following things in my code. If I missed any point then point me that as well. 1) You are using braces by giving one space and starting 1st curly brace in-front of the statements like while and if. 2) All variable you wrote in small letters and to make them more clear u out _ sign to link them. 3) Function name 1st character and then the 2nd meaning name of function is capital. 3) 1 step indentation on each step involving in some kind of computation. 4) You used unsigned when you feel that value are in +ve.
correct on all counts, you have correctly identified that the standard makes the code much more easily readable and helps you identify errors. I enforce the standards at our company and I therefore never write without using that standard. The standard we use is essentially an extension of C99 or sometimes called ANSI-C standard and is used heavily in the sector I work, which is embedded programming. Companies in the game market you are looking at tend to use C++ (UNREAL GAME ENGINE etc) or C# (UNITY GAME ENGINE) and there standard may differ slightly as the majority of the code won't be C compliant, and they have other pressures for readability. So generally in large companies you will be expected to write to a standard and there will usually be a process to check code into the active repository. So while learning coding try and have your own standard but just remember you may have to adapt when you are employed. The company is not going to rewrite an entire repository to your standard even if you could argue it was better. So my standard is not the "absolute best" it's just a standard I would generally have to write to at work.
In vino veritas