使用功能程序/显示结果帮助定义变量 [英] Help with defining my variable with functional program/displaying results

查看:62
本文介绍了使用功能程序/显示结果帮助定义变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将我编写的相当基本的程序转换为功能正常的程序,并且研究和修改了数小时的代码却没有结果.我的DisplayResults函数调用未返回值,我也不明白为什么.我也无法弄清楚如何在我的代码中正确定义区域",我将在其旁边放置注释以便于指导.非常感谢任何提示或提示,我只想了解为什么它不起作用

直接在int main()上方的区域定义了问题...

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...

// 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;
}







推荐答案

有时盯着代码看几个小时可以使您对简单的项目有所了解,而所需要做的只是新鲜的眼睛.

缺少变量区域的类型声明.编译器认为您已声明类型"area",并选择不给它命名.
另外,您的DisplayResults函数不会返回任何值,因此您应该将返回类型更改为void.

试试这个:

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:

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


这篇关于使用功能程序/显示结果帮助定义变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆