程序不向函数返回值
Program doesn't return values to function
我正在尝试制作牛顿第二定律计算器,当我输入计算值时没有任何反应。我不确定它们是否没有返回到主功能还是什么。我想做的是让用户输入一个字符,然后由包含 switch 语句的函数获取,在 switch 语句的情况下,函数调用执行算术的函数,然后这些函数将返回一个值给开关函数,switch 函数将返回一个值给主函数,然后将值打印到屏幕上。
// Newtons2ndlaw.cpp : This file contains the 'main' function. Program
execution begins and ends there.
//
#include "pch.h"
#include <stdio.h>
#include <stdlib.h>
#include<iostream>
void varselect(char, float *);
void force(float *);
void acceleration(float *);
void mass(float *);
int main()
{
float eqvalue;
char operation;
printf("Welcome to Newtons 2nd law solver!n");
system("PAUSE");
printf("Would you like to solve for a Force, Acceleration or Mass?nType 'F'(for force), 'A'(for acceleration), 'M'(for mass) to select a variablen");
scanf_s("%c", &operation,1);
if (operation == 'f' || operation == 'F' || operation == 'a' || operation == 'A' || operation == 'm' || operation == 'M') //logic for determing what the user entered
{}
else
{
printf("Please enter a valid character.");
}
varselect(operation,&eqvalue); //function call to receive float value from varselect function
if (operation == 'f' || operation == 'F') //{
{
printf("The force = %f",eqvalue);
}
//this block determines what character string to display with calculated float value
else if (operation == 'a' || operation == 'A')
{
printf("The acceleration = %f", eqvalue);
}
else if (operation == 'm' || operation == 'M')
{
printf("the Mass = %f", eqvalue);
}
} //}
void varselect(char x, float *j)
//this function recieves the user inputed char value and returns the calculated float value to function call.
{ //switch allows program to "understand" users unwillingness to press shift before a,f,m like printf statement tells them to do.
switch (x) // also allows each variable to call its own function.
{
case 'f':
float getval;
force(&getval);
*j = getval;
return;
break;
}
switch (x)
{
case 'F':
float getval;
force(&getval);
*j = getval;
return;
break;
}
switch (x)
{
case 'a':
float getval;
acceleration(&getval);
*j = getval;
return;
break;
}
switch (x)
{
case 'A':
float getval;
acceleration(&getval);
*j = getval;
return;
break;
}
switch (x)
{
case 'm':
float getval;
mass(&getval);
*j = getval;
return;
break;
}
switch (x)
{
case 'M':
float getval;
mass(&getval);
*j = getval;
return;
break;
}
return;
}
void force(float *fma)
{
float acceleration, mass;
printf("Enter a value for 'Mass', then 'Acceleration'.nn");
scanf_s("%fn%fn", &mass, &acceleration, 2);
*fma = mass * acceleration;
return;
}
void acceleration(float *afm)
{ //functions to take input from user and return float to varselect function
float force, mass;
printf("Enter a value for 'Force', then 'Mass'.nn");
scanf_s("%fn%fn", &force, &mass, 1);
*afm = force / mass;
return;
}
void mass(float *fam)
{
float force, acceleration;
printf("Enter a value for 'Force', then 'Acceleration'.nn");
scanf_s("%fn%fn", &force, &acceleration, 1);
*fam = force / acceleration;
return;
}
我认为它与您的代码有关...我能说的是,如果你有一个 int 或 double 或浮点函数,它的工作原理与 void 相同。你输入你需要的参数,也许像牛顿第二定律或任何你需要的参数。在那里您可以添加切换功能。切换后,您返回输出。 在 {} 之后的开关之后,您输入返回输出或要返回的变量的名称。 要在控制台上获取它,您只需这样做: std::cout<<(函数名称( 输出(函数参数中的变量或任何 */13, 4.0f( <</div>
相关文章:
- 从python中调用C++函数并获取返回值
- 编译器警告:执行到达值返回函数的末尾而不返回值
- 将返回值存储在函数指针数组的指针中是如何工作的?
- 程序不向函数返回值
- 如何在另一个函数中使用返回值作为参数?
- 编写以 C++ 为单位返回值的函数
- 了解在返回值之前和之后使用 EAX 的函数调用
- 当我使用 void 函数的返回值(通过强制转换函数指针)时,究竟会发生什么?
- 如何在不使用临时变量的情况下取消引用返回指针的函数的返回值?
- 如何将不可移动和不可复制的函数返回值获取到数组中
- G++ 编译器是否在未使用返回值的情况下将 constexpr 函数视为常规函数?
- 为什么当你将函数的返回值乘以零时它不会短路?
- 函数的返回值类型是别名 ***或 *** 布尔值
- 如何在没有返回值的情况下使用(特征)unaryExpr 和 lambda 函数?
- 创建 std::函数,它返回具有函数成员值的变量.分段错误
- 为什么这个"ctypes"包装函数的返回值是"c_long(0)"而不是&quo
- 函数不显示返回值 - C++
- 结构新手,我对如何从 void 函数中返回值并将其放入另一个函数感到困惑
- 将 void 函数更改为返回值函数
- 返回值函数中结构成员赋值的额外拷贝