Click here to Skip to main content
14,873,468 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Large project how to seprate definition and declaration? Pin
leon de boer13-Jun-16 4:03
Memberleon de boer13-Jun-16 4:03 
GeneralRe: Large project how to seprate definition and declaration? Pin
Hassan Syed113-Jun-16 4:54
MemberHassan Syed113-Jun-16 4:54 
SuggestionRe: Large project how to seprate definition and declaration? Pin
David Crow13-Jun-16 5:23
MemberDavid Crow13-Jun-16 5:23 
GeneralRe: Large project how to seprate definition and declaration? Pin
leon de boer13-Jun-16 16:19
Memberleon de boer13-Jun-16 16:19 
GeneralRe: Large project how to seprate definition and declaration? Pin
Hassan Syed113-Jun-16 19:47
MemberHassan Syed113-Jun-16 19:47 
AnswerRe: Large project how to seprate definition and declaration? Pin
David Crow14-Jun-16 2:47
MemberDavid Crow14-Jun-16 2:47 
GeneralRe: Large project how to seprate definition and declaration? Pin
leon de boer14-Jun-16 6:06
Memberleon de boer14-Jun-16 6:06 
GeneralRe: Large project how to seprate definition and declaration? Pin
leon de boer14-Jun-16 10:01
Memberleon de boer14-Jun-16 10:01 
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_H

float 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 <iostream>
#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 that on screen
				err = 0;											// Set err to zero so we fail loop
			}
			if (fResult > max) {									// Check entry is below maximum 
				printf("Re-enter value above maximum of %f\n", max);// Prompt that on screen
				err = 0;											// Set err to zero so we fail loop
			}
		}
		scanf_s("%*[^\n]");											// Make sure we dump any characters in buffer
	} while (err == 0);												// Repeat until valid float
	return (fResult);												// Return the  result
} 

File: Source.CPP
#include <iostream>
#include "calculation.h"
#include "input.h"
using namespace std;

float input_area (void) {
	float width = GetFloatInput("Enter the width of rectangle :", FLT_EPSILON, FLT_MAX);
	float height = GetFloatInput("Enter the length of rectangle :", FLT_EPSILON, FLT_MAX);
	return (RectangleArea(width, height));							//Function Call to Rectangle and return answer
}

float input_trap(void) {
	float base1 = GetFloatInput("Enter base1 of trapezoid : ", FLT_EPSILON, FLT_MAX);
	float base2 = GetFloatInput("Enter base2 of trapezoid : ", FLT_EPSILON, FLT_MAX);
	float height = GetFloatInput("Enter the height of trapezoid : ", FLT_EPSILON, FLT_MAX);
	return (TrapezoidArea(base1, base2, height));					//Function Call to Trapezoid and return answer
}

int main()
{
	bool exitFlag = false;
	/* Menu */
	while (!exitFlag){
		printf("\nMain Menu:\n");
		printf("	0) Exit program\n");
		printf("	1) Calculate area of rectangle\n");
		printf("	2) Calculate area of trapezoid\n");
		unsigned int choice = MenuInput("Enter your choice : ", 2);
		switch (choice) {
			case 1:
				printf("Area of rectangle = %f\n", input_area());
				break;
			case 2:
				printf("Area of trapezoid = %f\n", input_trap());
				break;	
			default:
				exitFlag = true;
		}
	};

	return 0;
}

In vino veritas

GeneralRe: Large project how to seprate definition and declaration? Pin
Hassan Syed114-Jun-16 20:34
MemberHassan Syed114-Jun-16 20:34 
GeneralRe: Large project how to seprate definition and declaration? Pin
leon de boer15-Jun-16 3:53
Memberleon de boer15-Jun-16 3:53 
GeneralRe: Large project how to seprate definition and declaration? Pin
Hassan Syed115-Jun-16 4:32
MemberHassan Syed115-Jun-16 4:32 
GeneralRe: Large project how to seprate definition and declaration? Pin
leon de boer15-Jun-16 6:21
Memberleon de boer15-Jun-16 6:21 
GeneralRe: Large project how to seprate definition and declaration? Pin
Hassan Syed115-Jun-16 7:01
MemberHassan Syed115-Jun-16 7:01 
GeneralRe: Large project how to seprate definition and declaration? Pin
leon de boer15-Jun-16 8:04
Memberleon de boer15-Jun-16 8:04 
GeneralRe: Large project how to seprate definition and declaration? Pin
Hassan Syed115-Jun-16 8:48
MemberHassan Syed115-Jun-16 8:48 
GeneralRe: Large project how to seprate definition and declaration? Pin
leon de boer15-Jun-16 20:00
Memberleon de boer15-Jun-16 20:00 
QuestionGlut errors with Mingw on windows Pin
Ratul Thakur11-Jun-16 7:03
MemberRatul Thakur11-Jun-16 7:03 
AnswerRe: Glut errors with Mingw on windows Pin
Ratul Thakur11-Jun-16 20:02
MemberRatul Thakur11-Jun-16 20:02 
GeneralRe: Glut errors with Mingw on windows Pin
leon de boer11-Jun-16 22:59
Memberleon de boer11-Jun-16 22:59 
GeneralRe: Glut errors with Mingw on windows Pin
Ratul Thakur11-Jun-16 23:59
MemberRatul Thakur11-Jun-16 23:59 
QuestionWhy no boundry condition set for C++ Pin
Hassan Syed111-Jun-16 4:52
MemberHassan Syed111-Jun-16 4:52 
AnswerRe: Why no boundry condition set for C++ Pin
Richard MacCutchan11-Jun-16 5:08
mveRichard MacCutchan11-Jun-16 5:08 
GeneralRe: Why no boundry condition set for C++ Pin
Hassan Syed111-Jun-16 5:27
MemberHassan Syed111-Jun-16 5:27 
AnswerRe: Why no boundry condition set for C++ Pin
Patrice T11-Jun-16 6:02
mvePatrice T11-Jun-16 6:02 
AnswerRe: Why no boundry condition set for C++ Pin
leon de boer11-Jun-16 22:53
Memberleon de boer11-Jun-16 22:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.