我需要拼命的帮助,因为我无法弄清楚为什么和做错了什么。我正在尝试向用户询问有关长度转换的多个问题。我遇到一个问题,仅问第一个问题,而下一个将没有机会被问到。我尝试了两个版本,但是它们都有缺陷。
Sample output:
Sisestage pikkus meetrides:
500
Sisestage uue uhiku kood (alj, ver, kyn, toll, jalg, jard, lii):
Sisestasite vale vaartuse:500 void vale uhiku:
我的代码:
#include <iostream>
#include <iomanip>
#include <string>
#include <string.h>
using namespace std;
int main()
{
float P; /* Pikkus meetrites */ /*Length P in metres*/
string U; /* M66du:hik*/ /* Unit of measure*/
float ALJ;
float VER;
float KYN;
float TOLL;
float JALG;
float JARD;
float LII;
do
{
cout<<"Sisestage pikkus meetrides: \n"; /*Ku:sin kasutajalt pikkust P*/ /* Asking user for the length P in metres*/
cin>>P;
if (P > 0)
cout<<"Sisestage uue uhiku kood (alj, ver, kyn, toll, jalg, jard, lii): \n"; /* Ku:sin kasutajalt u:hiku koodi U*/ /* Asking user for the length U in metres*/
getline(cin, U);
} while (P > 0);
cout << "Sisestasite vale vaartuse:"<<P<< endl; /*Veateade*/
ALJ = P / 5572;
VER = P / 1066.781;
KYN = P / 0.53;
TOLL = P / 0.0254;
JALG = P / 0.3048;
JARD = P / 0.9144;
LII = P / 500;
if( U == "alj")
{
cout<< setprecision(2)<<fixed<< P <<"m = "<< ALJ << U <<"." ;
}
else if( U == "ver")
{
cout<< setprecision(2) << fixed << P <<" m ="<< VER << U <<".";
}
else if( U == "kyn")
{
cout<< setprecision(2) << fixed << P <<" m ="<< KYN << U <<".";
}
else if( U == "toll")
{
cout<< setprecision(2) << fixed << P <<" m ="<< TOLL << U <<".";
}
else if( U == "jalg")
{
cout<< setprecision(2) << fixed << P<<" m ="<< JALG << U <<".";
}
else if( U == "jard")
{
cout<< setprecision(2) << fixed << P <<" m ="<< JARD << U <<".";
}
else if( U == "lii")
{
cout<< setprecision(2) << fixed << P <<" m ="<< LII << U <<".";
}
getchar();
getchar();
}
最佳答案
很难说出这种缩进是怎么回事,但是您的意思是将getline包含在if()块中吗?也许您想念{}
if (P > 0)
cout<<"Sisestage uue uhiku kood (alj, ver, kyn, toll, jalg, jard, lii): \n"; /* Ku:sin kasutajalt u:hiku koodi U*/ /* Asking user for the length U in metres*/
getline(cin, U);
关于c++ - 我很难调试很长的编译时间并且需要多个用户输入,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14633164/