Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was attempting to convert and pretty basic program i wrote into a functional one and I have researched and altered code for hours with no results. My DisplayResults function call is not returning a value and I don't understand why. Also i cannot figure out how to properly define "area" in my code which i will put comments next to for easier direction. Any tips or hints are greatly appreciated I just want to understand why it's not working

Immediate above int main() is where area is having problems being defined...

C++
// TriangleWFunctions.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

void pause()
{char junk;
	cout << "Press enter to continue...";
	cin.ignore();
	cin.get(junk);

	}

string Readstring ( string prompt )
{
	string result;

	cout << prompt;
	cin >> result;

	return (result);
}
int ReadInt ( string prompt )
{
	int result;

	cout << prompt;
	cin >> result;

	return (result);
}

int GetLength()
{
	int result;
	result=ReadInt("Enter the length of the rectangle: ");
		return (result);
}


int GetWidth()
{
	int result;
	result=ReadInt("Enter the width of the rectangle: ");
		return (result);
}

int ComputeResult( int length, int width)
{ 

int result;
	result = length * width;
	return(result);
}

int DisplayResults(area) //This is where area is saying it's not defined...
{
	cout << "\n" << "The area of the rectangle is: " << area; 
}

int main()
{
	
	
	int length, width, area;
	
	length = ReadInt("Enter the Length of the rectangle: ");
	width = ReadInt("Enter the Width of the rectangle: ");



	// calculate
	area = ComputeResult(length, width);

	// display results
	DisplayResults (area);

	// freeze screen to see solution

	pause ();
	return 0;
}



[Edit: Paul Watt, improved code formatting of angle brackets]
Posted
Updated 30-Nov-11 15:59pm
v2

1 solution

Sometimes staring at code for hours can make you gloss over the simple items, and all it needs is a fresh set of eyes.

The type declaration for the variable area is missing. The Compiler thinks that you have declared a type "area" and have chosen to not give it a name.
Also, your DisplayResults function doesn't return any values, so you should change the return type to void.

Try this:

C++
void DisplayResults(int area) //This is where area is saying it's not defined...
{
  cout << "\n" << "The area of the rectangle is: " << area << "\n"; 
}
 
Share this answer
 
v2
Comments
xaviorin 30-Nov-11 23:25pm    
Thank you so much! That did it! Much appreciated! So, just so i completely understand why we use void:

Is void used in functions that don't return variables and "void" pretty much tells the compiler that? That seems so simple I'm kind of shocked right now...
Paul M Watt 1-Dec-11 2:22am    
void: Correct.

If you had a function with no parameters, it is legal and some developers prefer to write something like this:

void function(void);
xaviorin 5-Dec-11 16:00pm    
Thank you!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900