Программа не возвращает значения в функцию - PullRequest
0 голосов
/ 13 октября 2018

Я пытаюсь сделать калькулятор 2-го закона Ньютона, и при вводе значений для расчета ничего не происходит.Я не уверен, возвращаются ли они в основную функцию или как.Я хотел, чтобы пользователь вводил символ, который затем принимался функцией, содержащей оператор 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 variable\n");

    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'.\n\n");
    scanf_s("%f\n%f\n", &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'.\n\n");
    scanf_s("%f\n%f\n", &force, &mass, 1);

        *afm = force / mass;

    return;

}

void mass(float *fam)
{
    float force, acceleration;

    printf("Enter a value for 'Force', then 'Acceleration'.\n\n");
    scanf_s("%f\n%f\n", &force, &acceleration, 1);

    *fam = force / acceleration;

    return;
}

1 Ответ

0 голосов
/ 13 октября 2018

Я думаю, что это не относится к вашему коду ... Что я могу сказать, так это то, что если у вас есть функция типа int, double или float, она работает так же, как void.Вы вводите параметры, которые вам нужны, например, для второго закона Ньютона, или все, что вам нужно.И там вы можете добавить функцию переключения.А после переключения возвращаешь вывод.После переключателя после его {} вы помещаете возвращаемый результат или имя переменной, которую хотите вернуть.Чтобы получить его на консоли, вы просто должны сделать так: std :: cout << (имя функции) output (/ * переменные, которые находятся в параметре функции или как угодно * / 13, 4.0f)

...