C++ C+的新功能+;,编码语法
我在空闲时间编写的代码可以使用帮助。我是C++初学者。我正在寻求帮助的事情: -需要正确循环的帮助,特别是当用户输入不是整数而是字符时 -当我试图编译时,case语句出现错误,它指向括号,我不知道为什么 该代码应该用从物理课上学到的运动学方程来求解某些变量。这是一种让我习惯C++开始的方法,但是可以使用高级程序员的帮助。 这也是我第一次在stackoverflow上发帖,如果有人对如何正确发帖有一些建议,请告诉我,在我的编程课结束后,会检查是否有任何回复,也会询问我的教授C++ C+的新功能+;,编码语法,c++,C++,我在空闲时间编写的代码可以使用帮助。我是C++初学者。我正在寻求帮助的事情: -需要正确循环的帮助,特别是当用户输入不是整数而是字符时 -当我试图编译时,case语句出现错误,它指向括号,我不知道为什么 该代码应该用从物理课上学到的运动学方程来求解某些变量。这是一种让我习惯C++开始的方法,但是可以使用高级程序员的帮助。 这也是我第一次在stackoverflow上发帖,如果有人对如何正确发帖有一些建议,请告诉我,在我的编程课结束后,会检查是否有任何回复,也会询问我的教授 //Arthur By
//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;
}
//亚瑟·贝拉
//2015年1月27日
//动力学方程计算程序
/*
vf=vi+a(t)
x=vi(t)+1/2(a)(t)^2
vf^2=vi^2+2a(s)
哪里:
vi+初始速度
vf=最终速度
s=距离
a=加速度(必须为常数)
t=以秒为单位的时间
*/
#包括
#包括
#包括
使用名称空间std;
int main()
{
int userInput=0;
双初始速度=0;
双最终概率=0;
双加速度=0;
双倍时间=0;
双三角距离=0;
char userInputSolveFor;
库特
case(userInput==1):
您不能使用开关()/case:
控制结构来执行此操作。
此外,单个字符不能接受像vi
或vf
这样的输入,您需要使用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;
}
对于userInput
,这将需要一个int
类型变量,因此您可能希望在主菜单选择中使用另一个变量(用于等式)。只要是case 1:
而不是case(userInput==1)
,只要您得到“there show a error”您需要告诉我们错误。请检查每个输入:if(cin>>userInput){…}else{/*error*/}
更好的问题:你需要发布一个,因为你有几十行与你的错误无关的代码。另一个必须做的是在代码中发布实际的错误消息以及注释,以指示错误出现的位置(我们这里的代码中没有行号)编程技巧:经常编译,尤其是当你对语法不确定的时候。我已经改变了代码的形式,将if和elseif语句包括在内,而不是case和break语句,并且还制作了post-mcve(希望如此).不知道是否有可能解开这条线,但希望我能找人帮我完成这最后一点errors@ArtByra对你问题的编辑,使得这个答案现在毫无价值。无论如何,据我所知(编译器错误告诉你),你只是缺少了一些右括号}
。我是如何使你的答案毫无价值的?我将代码的格式更改为您在第一节中建议的格式place@ArtByra这样现在就没人能看到原来的问题了。您想让我恢复编辑吗?--我还修复了第一个错误,我丢失了一个右大括号,但另一个错误仍然存在。
if(cin >> userInput) {
// ...
}
else {
/* error */
}