gpt4 book ai didi

c++ - 运行时错误 #3

转载 作者:行者123 更新时间:2023-11-30 04:05:10 26 4
gpt4 key购买 nike

我正在学习 C++ 类(class),我们正在使用 visual studio。我们的任务是创建一个程序来计算不同的东西,用户可以通过输入相应的字符来选择这些东西。该事件旨在向我们展示如何在代码中使用函数。我已经设置了第一个计算,但每次运行它时,我都会收到运行时错误 #3,当我在主程序中初始化 circleArea 时,circleArea 将打印为我初始化它的值,而不是 areaCircle 函数返回的值。谢谢你帮我解决问题,如果这对我来说是非常初级的,我很抱歉。


/// Lab6Marcelino.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

double pi = 3.14;

// Protoypes
void showMenu(char & c);
void getRadius(double & r, bool &nega);
bool isNegative(double val);
double areaCircle(double &r);

// Main
int _tmain(int argc, _TCHAR* argv[])
{
char menuChoice;
double radius;
bool negative = true;
double circleArea;

showMenu(menuChoice);

if (menuChoice = 'c')
{
getRadius(radius, negative);
areaCircle(radius);

cout << "The radius is " << radius << endl
<< "The area of the circle is: " << circleArea << endl;
}



system("Pause");
return 0;
}

//*************
// FUNCTIONS***
//*************

// Definition of function showMenu
// Shows Menu and asks for user choice.

void showMenu(char &menuChoice)
{
cout << "Enter C or c for the area of a Circle" << endl
<< "Enter T or t for the area of a Triangle" << endl
<< "Enter S or s for the area of a Sphere" << endl
<< "Enter P or p for the area of a triangular Prism" << endl
<< "Enter A or a for information about the author" << endl
<< "Enter Q or q to quit this program" << endl;
cin >> menuChoice;
}

// Definition of function getRadius
// Gets radius from user

void getRadius(double &r, bool &nega)
{
cout << "Enter the radius: ";
cin >> r;
isNegative(r);

while (isNegative(r) == true)
{
cout << "Enter the radius: ";
cin >> r;
isNegative(r);
}
}

bool isNegative(double val)
{
if (val < 0)
return true;

if (val > 0)
return false;
else
{
cout << "Invalid input" << endl;
return true;
}
}

double areaCircle(double &r)
{
double circleArea;
circleArea = pi * r * r;
return circleArea;
}

最佳答案

areaCircle(radius); 返回一个值,但您没有将它分配给任何东西。然后,当您尝试将 circleArea 与 cout 一起使用时,它尚未初始化,所以这可能是您遇到运行时错误的地方。

关于c++ - 运行时错误 #3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23414390/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com