救命! [英] Help!

查看:59
本文介绍了救命!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,我在这个编程方面相对较新,而且我在这里停留了

的任务。我正在尝试让下面的程序响应

整数-1来停止并计算总数。确实如此,但它让我在加仑和里程下输入-1

,我该如何避免这种情况?谢谢。


//以加仑计算汽油里程数


#include< iostream>

#include< ; conio.h>


使用std :: cout;

使用std :: cin ;;

使用std ::固定;


#include< iomanip>


使用std :: setprecision;


//函数main开始程序执行

int main()

{

int gal; //使用的加仑数

英里; //行驶里程数

int tg; //所有使用的加仑总数

int tm; //所有行驶里程总数


双mpg; //每加仑英里

总计双倍; //所有驱动器的平均值


//初始化阶段

tg = 0;

tm = 0;


while(gal!= -1){

cout<< \ n输入使用的加仑数(输入-1表示完成):\ n" ;;

cin>> gal;

cout<< 输入行驶里程:\ n;

cin>>里程;


tg = tg + gal;

tm = tm + miles;


//计算里程每加仑

mpg = static_cast<双> (英里)/加仑;


cout<< 这次旅行的每加仑是:\ n << setprecision(6)<<

fixed<< mpg;

}


if(gal!= 0)

total = static_cast<双> (tm)/ tg;

cout<< \ n总体平均值是:\ n" ;;

cout<<总计;

;返回0;


}

Hey guys, i am relatively new at this programming thing and am stuck on an
assignment here. I''m trying to get the program below to respond to the
integer -1 to stop and calculate total. It does, but it makes me enter -1
under both gallons and miles, how can i avoid this? Thanks.

//Figures gas mileage in gallons

#include <iostream>
#include <conio.h>

using std::cout;
using std::cin;;
using std::fixed;

#include <iomanip>

using std::setprecision;

// function main begins program execution
int main()
{
int gal; //number of gallons used
int miles; //number of miles driven
int tg; //total of all gallons used
int tm; //total of all miles driven

double mpg; //miles per gallon
double total; //average of all drives

//initialization phase
tg = 0;
tm = 0;

while ( gal != -1 ) {
cout << "\nEnter the gallons used (Enter -1 to finish) :\n";
cin >> gal;
cout << "Enter the miles driven:\n";
cin >> miles;

tg = tg + gal;
tm = tm + miles;

//calculate miles per gallon
mpg = static_cast < double > ( miles ) / gal;

cout << "The per gallon for this trip were :\n" << setprecision ( 6 ) <<
fixed << mpg;
}

if ( gal != 0 )
total = static_cast < double > ( tm ) / tg;
cout << "\nThe overall average was:\n";
cout << total;
;return 0;

}

推荐答案

Eric Whittaker写道:
Eric Whittaker wrote:
嘿伙计们,我对这个编程事物比较陌生,而且我在这里坚持了
任务。我正在尝试让下面的程序响应
整数-1来停止并计算总数。确实如此,但它让我在加仑和英里之间输入-1
,我怎么能避免这种情况呢?谢谢。

//数字汽油里程加仑

#include< iostream>
#include< conio.h>
使用std :: cout;
使用std :: cin ;;
使用std :: fixed;

#include< iomanip>

使用std :: setprecision;

//函数main开始程序执行
int main()
{gal /; //使用的加仑数
英里; //行驶里程数
int tg; //所有使用的加仑总数
int tm; //所有行驶里程总数

双mpg; //每加仑英里
双倍总数; //所有驱动器的平均值
//初始化阶段
tg = 0;
tm = 0;


总计= 0.0;

//而(gal!= -1){

for(;;){/ / loop forever

cout<< " \ n输入使用的加仑数(输入-1表示完成):\ n" ;;
cin>>加仑;


if(gal< 0)//在负加仑上退出循环

休息;


//忽略0加仑以防止''mpg'中的被零除错'calc

如果(gal == 0)

继续;

cout<< 输入行驶里程:\ n;
cin>>英里;

tg = tg + gal;
tm = tm +英里;

//计算每加仑英里数
mpg = static_cast<双> (英里)/加仑;

cout<< 这次旅行的每加仑是:\ n << setprecision(6)<<
fixed<< mpg;
}


// if(gal!= 0)

if(tg> 0)total = static_cast <双> (tm)/ tg;
cout<< \ n总体平均值为:\ n;
cout<<总计;


//;返回0;

返回0;
}
Hey guys, i am relatively new at this programming thing and am stuck on an
assignment here. I''m trying to get the program below to respond to the
integer -1 to stop and calculate total. It does, but it makes me enter -1
under both gallons and miles, how can i avoid this? Thanks.

//Figures gas mileage in gallons

#include <iostream>
#include <conio.h>

using std::cout;
using std::cin;;
using std::fixed;

#include <iomanip>

using std::setprecision;

// function main begins program execution
int main()
{
int gal; //number of gallons used
int miles; //number of miles driven
int tg; //total of all gallons used
int tm; //total of all miles driven

double mpg; //miles per gallon
double total; //average of all drives

//initialization phase
tg = 0;
tm = 0;
total = 0.0;
// while ( gal != -1 ) {
for (;;) { // loop forever
cout << "\nEnter the gallons used (Enter -1 to finish) :\n";
cin >> gal;
if (gal < 0) // quit loop on negative gallons
break;

// ignore 0 gallons to prevent divide-by-zero error in ''mpg'' calc
if (gal == 0)
continue;
cout << "Enter the miles driven:\n";
cin >> miles;

tg = tg + gal;
tm = tm + miles;

//calculate miles per gallon
mpg = static_cast < double > ( miles ) / gal;

cout << "The per gallon for this trip were :\n" << setprecision ( 6 ) <<
fixed << mpg;
}


// if ( gal != 0 )
if (tg > 0) total = static_cast < double > ( tm ) / tg;
cout << "\nThe overall average was:\n";
cout << total;

// ;return 0;
return 0;
}




问候,

拉里


-

反垃圾邮件地址,更改每个''X' 'to''。''直接回复。



Regards,
Larry

--
Anti-spam address, change each ''X'' to ''.'' to reply directly.


嘿谢谢拉里,使用(for)非常有帮助。我只是想在这里学习
,因为我的任务要求我使用(while)结构来完成这个任务,这是可能的,当我尝试它一直给我一个

未声明的标识符 - gal错误。


感谢您的帮助。
Hey thanks larry, the use of (for) is very helpful there. I''m just trying to
learn here, cause my assignment wants me to use a (while) structure to
accomplish this task, is this possible, when i try it keeps giving me a
"undeclared identifier - gal" error.

Thanks for all your help.


供将来参考,比帮助更具信息性的主题。将是一个好主意。

" Eric Whittaker" < EW **** @ adelphia.net>在留言中写道

新闻:oe ******************** @ adelphia.com
For future reference, a more informative subject line than "Help" would be a
good idea.
"Eric Whittaker" <ew****@adelphia.net> wrote in message
news:oe********************@adelphia.com
嘿伙计们,我我在这个编程方面相对较新,我在这里的任务上被困了。我正在尝试让下面的程序响应整数-1来停止并计算总数。它确实如此,但它让我在加仑和英里之间输入-1,我怎么能避免这种情况?
谢谢。
//数字汽油里程加仑
#include< iostream>
#include< conio.h>

使用std :: cout;
使用std :: cin ;;
使用std ::固定;

#include< iomanip>

使用std :: setprecision;

//函数main开始执行程序 int main()
{gal /; //使用的加仑数
英里; //行驶里程数
int tg; //所有使用的加仑总数
int tm; //所有行驶里程总数

双mpg; //每加仑英里
双倍总数; //所有驱动器的平均值
//初始化阶段
tg = 0;
tm = 0;


请注意,第一次遇到gal!= -1时,gal未初始化。你将
设置为等于某个不等于-1的初始值。

while(gal!= -1){
cout<< " \ n输入使用的加仑数(输入-1表示完成):\ n" ;;
cin>>加仑;


这里的问题是gal!= - 1测试*之前*用户输入

gal值,所以你必须继续循环结束前循环

结束。你实际上不必在加仑和里程下输入-1。

输入-1后加仑,你可以输入任何里程和环路

还会结束。您可以通过各种方式处理此问题。一种方法是使用


而(真实)

{

cout<< \ n输入使用的加仑数(输入-1表示完成):\ n" ;;

cin>> gal;

如果(gal == -1)

休息;


//循环的剩余部分
< br $>
}


另一种方法是在循环之前进行初始加仑查询并将所有

后续加仑查询作为最后一部分循环。


cout<< \ n输入使用的加仑数(输入-1表示完成):\ n" ;;

cin>> gal;


while(gal!= -1)

{

cout<< 输入行驶里程:\ n;

cin>>里程;


tg = tg + gal;

tm = tm + miles;


//计算里程每加仑

mpg = static_cast<双> (英里)/加仑;


cout<< 这次旅行的每加仑是:\ n << setprecision(6)<<

fixed<< mpg;


cout<< \ n输入使用的加仑数(输入-1表示完成):\ n" ;;

cin>> gal;

}


最后,你总是可以一次运行while循环,然后询问

用户*如果他们想继续或退出循环结束*并使用

答案设置你的while()条件所依赖的变量(例如,

你可以定义一个bool keepGoing,你初始化为true,当用户表示他们想要退出时设置为

false,并使用while(keepGoing))。

cout<< ; 输入行驶里程:\ n;
cin>>英里;

tg = tg + gal;
tm = tm +英里;

//计算每加仑英里数
mpg = static_cast<双> (英里)/加仑;

cout<< 这次旅行的每加仑是:\ n << setprecision(6)
<<固定<< mpg;
}
如果(gal!= 0)
total = static_cast<双> (tm)/ tg;
cout<< \ n总体平均值为:\ n;
cout<<总;


因为你除以tg,我认为你应该测试tg!= 0,而不是
gal!= 0。你也可能想要一个块中的所有三行,即


if(tg!= 0)

{

总计= static_cast<双> (tm)/ tg;

cout<< \ n总体平均值是:\ n" ;;

cout<<总计;

}


;返回0;


这是什么;在行的开头?

}
Hey guys, i am relatively new at this programming thing and am stuck
on an assignment here. I''m trying to get the program below to respond
to the integer -1 to stop and calculate total. It does, but it makes
me enter -1 under both gallons and miles, how can i avoid this?
Thanks.
//Figures gas mileage in gallons

#include <iostream>
#include <conio.h>

using std::cout;
using std::cin;;
using std::fixed;

#include <iomanip>

using std::setprecision;

// function main begins program execution
int main()
{
int gal; //number of gallons used
int miles; //number of miles driven
int tg; //total of all gallons used
int tm; //total of all miles driven

double mpg; //miles per gallon
double total; //average of all drives

//initialization phase
tg = 0;
tm = 0;
Note that the first time gal != -1 is encountered, gal is uninitialised. You
should set it equal to some initial value that is unequal to -1.
while ( gal != -1 ) {
cout << "\nEnter the gallons used (Enter -1 to finish) :\n";
cin >> gal;
The problem here is that the gal!=-1 test is made *before* the user enters
the gal value, so you have to proceed to the end of the loop before the loop
finishes. You don''t actually have to enter -1 under both gallons and miles.
After you enter -1 for gal, you can enter anything for miles and the loop
will still end. You could handle this in various ways. One way is to use

while(true)
{
cout << "\nEnter the gallons used (Enter -1 to finish) :\n";
cin >> gal;
if(gal == -1)
break;

// remainder of loop

}

Another way is to have the initial gallons query before the loop and all
subsequent gallons queries as the final part of the loop.

cout << "\nEnter the gallons used (Enter -1 to finish) :\n";
cin >> gal;

while(gal != -1)
{
cout << "Enter the miles driven:\n";
cin >> miles;

tg = tg + gal;
tm = tm + miles;

//calculate miles per gallon
mpg = static_cast < double > ( miles ) / gal;

cout << "The per gallon for this trip were :\n" << setprecision ( 6 ) <<
fixed << mpg;

cout << "\nEnter the gallons used (Enter -1 to finish) :\n";
cin >> gal;
}

Finally, you could always have one run of the while loop and then ask the
user at the *end* of the loop if they want to continue or quit and use the
answer to set the variable on which your while() condition depends (e.g.,
you could define a bool keepGoing which you initialise to true and set to
false when the user indicates they want to quit, and use while(keepGoing)).
cout << "Enter the miles driven:\n";
cin >> miles;

tg = tg + gal;
tm = tm + miles;

//calculate miles per gallon
mpg = static_cast < double > ( miles ) / gal;

cout << "The per gallon for this trip were :\n" << setprecision ( 6 )
<< fixed << mpg;
}

if ( gal != 0 )
total = static_cast < double > ( tm ) / tg;
cout << "\nThe overall average was:\n";
cout << total;
Since you are dividing by tg, I think you should be testing tg!=0, not
gal!=0. You also probably want all three lines in a block, i.e.,

if ( tg != 0 )
{
total = static_cast < double > ( tm ) / tg;
cout << "\nThe overall average was:\n";
cout << total;
}


;return 0;
What is the ; at the start of the line for?

}



-

John Carson


--
John Carson


这篇关于救命!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆