C ++新手,编码语法

时间:2015-02-02 18:52:36

标签: c++

我可以在空闲时间使用此代码的帮助。我是C ++的初学者。我正在寻求帮助的事情:

- 需要正确循环的帮助,特别是如果用户输入不是整数,而是字符 - 当我尝试编译时,case语句显示错误,它指向括号,我不知道原因。

代码应该通过从Physics类学习的运动方程来解决某些变量。这是我习惯C ++开头的一种方式,但可以使用高级编码员的帮助。

这也是我第一次在stackoverflow上发帖,如果有人有关于正确发布的一些提示让我知道,将在我的编程课完成后检查任何回复,也会问我的教授。

//Arthur Byra
//27 January 2015
//Program to calculate kinetic equations


/* 
vf = vi + a(t)
x = vi(t) + 1/2(a)(t)^2
vf^2 = vi^2 +2a(s) 

where:

    vi + initial velocity
    vf = final velocity
    s = distance
    a = acceleration (must be constant)
    t = time in seconds
*/

#include<iostream>
#include<cmath>
#include<iomanip>

using namespace std;

int main()
{

    int userInput = 0;
    double initialVelocity = 0;
    double finalVelocity = 0;
    double acceleration = 0;
    double time = 0;
    double deltaDistance = 0;
    char userInputSolveFor;

    cout << "This is a program to calculate certain variables using kinematic equations." << endl;
    cout << "Remember that kinematic equations only work when acceleration is constant!" << endl;
    cout << endl;
    cout << "Which equation did you want to use?" << endl;
    cout << endl;
    cout << "1) vf = vi + a(t)" << endl;
    cout << "2) x = vi(t) + 1/2(a)(t)^2" << endl;
    cout << "3) vf^2 = vi^2 + 2(a)(x)" << endl;
    cout << endl;
    cout << "Input number of the equation you want to use (1,2,3): " << endl;
    cin >> userInput;
    switch (userInput)
    {
        case (userInput == 1):

            cout << "You are using vf = vi + a(t)." << endl;
            cout << endl;
            cout << "What are you trying to solve for?" << endl;
            cout << endl;
            cout << "Use a for acceleration (in m/s/s)." << endl;
            cout << "Use vi for initial velocity (in m/s)." << endl;
            cout << "Use vf for final velocity (in m/s)." << endl;
            cout << "Use t for time (in seconds)." << endl;
            cin >> userInputSolveFor;
            cout << endl;
            switch (userInputSolveFor)
            {
                case (userInputSolveFor == a):

                    cout << "You are solving for acceleration." << endl; //Solving for acceleration
                    cout << endl;
                    cout << "What is the initial velocity (in m/s)?" << endl;
                    cin >> initialVelocity;
                    cout << "What is the final velocity (in m/s)?" endl;
                    cin >> finalVelocity;
                    cout >> "What is the time (in seconds)?" << endl;
                    cin >> time;
                    cout << "The acceleration is " << (finalVelocity - initialVelocity) / time << setprecision(10) << " m/s/s." << endl;
                break;

                case (userInputSolveFor == vi):

                    cout << "You are solving for initial velocity." << endl; //Solving for initial velocity
                    cout << endl;
                    cout << "What is your acceleration (in m/s/s)?" << endl;
                    cin >> acceleration;
                    cout << "What is your final velocity (in m/s)?" endl;
                    cin >> finalVelocity;
                    cout >> "What is the time (in seconds)?" << endl;
                    cin >> time;
                    cout << "The initial velocity is " << finalVelocity / (acceleration * time) << setprecision(10) << " m/s." << endl;
                break;

                case (userInputSolveFor == vf):

                    cout << "You are solving for final velocity." << endl; //Solving for final velocity
                    cout << endl;
                    cout << "What is your acceleration (in m/s/s)?" << endl;
                    cin >> acceleration;
                    cout << "What is your initial velocity (in m/s)?" endl;
                    cin >> initialVelocity;
                    cout >> "What is the time (in seconds)?" << endl;
                    cin >> time;
                    cout << "The final velocity is " << initialVelocity + (acceleration * time) << setprecision(10) << " m/s." << endl;
                break;

                case (userInputSolveFor == t):

                    cout << "You are solving for time." << endl; //Solving for time
                    cout << endl;
                    cout << "What is the acceleration (in m/s/s)?" << endl;
                    cin >> acceleration;
                    cout << "What is the initial velocity (in m/s)?" endl;
                    cin >> initialVelocity;
                    cout >> "What is the final velocity (in m/s)?" << endl;
                    cin >> finalVelocity;
                    cout << "The time is " << (finalVelocity - initialVelocity) / acceleration << setprecision(10) << " seconds." << endl;
                break;

                default:

                    cout <<"The input you have entered is not valid." << endl;
        break;
            }

        case (userInput == 2):

            cout << "You are using x = vi(t) + 1/2(a)(t)^2." << endl;
            cout << endl;
            cout << "What are you trying to solve for?" <<endl;
            cout << endl;
            cout << "Use x for distance (in meters)." << endl;
            cout << "Use vi for initial velocity (in m/s)." << endl;
            cout << "Use t for time (in seconds)." << endl;
            cout << "Use a for acceleration (in m/s/s)." <<endl;
            cin >> userInputSolveFor;
            cout << endl;
            switch (userInputSolveFor)
            {
                case (userInputSolveFor == a):
                    cout << "You are solving for acceleration." << endl;
                    cout << endl;
                    cout << "What is the distance (in meters)?" << endl;
                    cin >> deltaDistance;
                    cout << "What is the initial velocity (in m/s)?" << endl;
                    cin >> initialVelocity;
                    cout << "What is the time (in seconds)?" << endl;
                    cin >> time;
                    cout << "The acceleration is " << deltaDistance - (initialVelocity * time) / (0.5 * pow(time, 2.0)) << setprecision(10) << " m/s/s." << endl;
                break;

                case (userInputSolveFor == vi):

                    cout << "You are solving for initial velocity." << endl;
                    cout << endl;
                    cout << "What is the distance (in meters)?" << endl;
                    cin >> deltaDistance;
                    cout << "What is the acceleration (in m/s/s)?" << endl;
                    cin >> acceleration;
                    cout << "What is the time (in seconds)?" << endl;
                    cin >> time;
                    cout << "The initial velocity is " << (deltaDistance - ((pow(time, 2.0)) * acceleration * 0.5)) / time << setprecision(10) << " m/s." << endl;
                break;

                case (userInputSolveFor == x):

                    cout << "You are solving for distance." << endl;
                    cout << endl;
                    cout << "What is the acceleration (in m/s/s)?" << endl;
                    cin >> acceleration;
                    cout << "What is the initial velocity?" << endl;
                    cin >> initialVelocity;
                    cout << "What is the time (in seconds)?" << endl;
                    cin >> time;
                    cout << "The distance is " << (initialVelocity * time) + ((pow(time, 2.0)) * acceleration * 0.5) << setprecision(10) << " meters." << endl;
                break;

                case (userInputSolveFor == t):

                    cout << "You are solving for time." << endl;
                    cout << endl;
                    cout << "What is the acceleration (in m/s/s)?" << endl;
                    cin >> acceleration;
                    cout << "What is the initial velocity?" << endl;
                    cin >> initialVelocity;
                    cout << "What is the distance (in meters)?" << endl;
                    cin >> deltaDistance;

                    if (initialVelocity == 0):
                    {
                        cout << "The time is " << sqrt(deltaDistance - (0.5 * acceleration)) << setprecision(10) << " seconds." << endl;
                    }
                    else if (acceleration == 0):
                    {
                        cout << "The time is " << (deltaDistance / initialVelocity) << setprecision(10) << " seconds." << endl;
                    }

                break;

                default:
                    cout <<"The input you have entered is not valid." << endl;

        break;
            }

        case (userInput == 3):

            cout << "You are using vf^2 = vi^2 + 2(a)(x)." << endl;
            cout << endl;
            cout << "What are you trying to solve for?" <<endl;
            cout << endl;
            cout << "Use vf for final velocity (in m/s)." << endl;
            cout << "Use vi for initial velocity (in m/s)." << endl;
            cout << "Use a for acceleration." << endl;
            cout << "Use x for distance (in meters)." <<endl;
            cin >> userInputSolveFor;
            cout << endl;
            switch (userInputSolveFor)
            {
                case (userInputSolveFor == vf):

                    cout << "You are solving for final velocity." << endl;
                    cout << endl;
                    cout << "What is the initial velocity (in m/s)?" << endl;
                    cin >> initialVelocity;
                    cout << "What is the acceleration (in m/s/s)?" << endl;
                    cin >> acceleration;
                    cout << "What is the distance (in meters)?" << endl;
                    cin >> deltaDistance;
                    cout << "The final velocity is " << (sqrt(pow(initialVelocity, 2.0))) + (2 * acceleration * deltaDistance) << setprecision(10) << " m/s." << endl;
                break;

                case (userInputSolveFor == vi):

                    cout << "You are solving for initial velocity." << endl;
                    cout << endl;
                    cout << "What is the final velocity (in m/s)?" << endl;
                    cin >> finalVelocity;
                    cout << "What is the acceleration (in m/s/s)?" << endl;
                    cin >> acceleration;
                    cout << "What is the distance (in meters)?" << endl;
                    cin >> deltaDistance;
                    cout << "The initial velocity is " << (sqrt(pow(finalVelocity, 2.0)) / (2 * acceleration * deltaDistance) << setprecision(10) << " m/s." << endl;
                break;

                case (userInputSolveFor == a):

                    cout << "You are solving for acceleration." << endl;
                    cout << endl;
                    cout << "What is the final velocity (in m/s)?" << endl;
                    cin >> finalVelocity;
                    cout << "What is the initial velocity (in m/s)?" << endl;
                    cin >> initialVelocity;
                    cout << "What is the distance (in meters)?" << endl;
                    cin >> deltaDistance;
                    cout << "The acceleration is " << (sqrt(pow(finalVelocity, 2.0)) - (sqrt(pow(initialVelocity, 2.0)))) / (2 * deltaDistance)) << setprecision(10) << " m/s/s." << endl;
                break;

                case (userInputSolveFor == x):

                    cout << "You are solving for distance." << endl;
                    cout << endl;
                    cout << "What is the final velocity (in m/s)?" << endl;
                    cin >> finalVelocity;
                    cout << "What is the initial velocity (in m/s)?" << endl;
                    cin >> initialVelocity;
                    cout << "What is the acceleration (in m/s/s)?" << endl;
                    cin >> acceleration;
                    cout << "The distance is " << (sqrt(pow(finalVelocity, 2.0)) - sqrt(pow(initialVelocity, 2.0))) / (2.0 * acceleration) << setprecision(10) << " meters." << endl;
                break;

                default:
                    cout <<"The input you have entered is not valid." << endl;
        break;
            }

    default:
        while (userInput <= 1 || userInput >= 3)
        {
        cout << "The number you have entered is not valid." << endl;
        cin >> userInput;
        }
    }
    return 0;
}

1 个答案:

答案 0 :(得分:2)

  

case (userInput == 1):

使用switch()/case:控制结构无法做到这一点 此外,单个字符无法获取vivf等输入,您需要使用std::string userInput;代替。

您实际需要的是级联if()/else if()/else控件结构,如下所示:

if (userInputSolveFor == "a") {                 
    cout << "You are solving for acceleration." << endl;
    // ...
}
else if (userInputSolveFor == "vi") {
    cout << "You are solving for initial velocity." << endl;
    // ...
}
else if(userInputSolveFor == "vf") {
    cout << "You are solving for final velocity." << endl;
    // ...
}
// a.s.o
else { // <<< That's equivalent to default:
    cout <<"The input you have entered is not valid." << endl;
}

关于检查正确的数字输入,请像DieterLücking在评论中提出的那样:

if(cin >> userInput) {
   // ... 
} 
else { 
    /* error */ 
}

这需要为int设置userInput类型变量,因此您可能希望为主菜单选择使用不同的变量(对于等式)。