温度转换计划 - 仍然是一个新秀 [英] Temperature Conversion Program - Still a Rookie

查看:123
本文介绍了温度转换计划 - 仍然是一个新秀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿所有人〜

我还是一个C ++新手所以请耐心等待。

我正在做一个带原型功能的温度转换程序。

基本上,我想知道你们中的一些人是否会看一下我的代码

并为我批评它。我主要关注原型

功能如何工作以及我是否在我的代码中正确设计它们。

您的参与将不胜感激!


我的代码:


#include" stdafx.h"

#include< iostream>

#include< cstdlib>


双FahrenheittoCelsius(double)= 0.0; //函数原型

double CelsiustoFahrenheit(double)= 0.0;


int _tmain(int argc,_TCHAR * argv [])

{

double tempFahrenheit,tempCelsius;

double temp;

char f;

char c;

cout<< "输入温度:; //提示用户输入temp

cin> temp;

cout<< "华氏度或摄氏度?; //如果用户没有指定f或c

问他们

if(temp == f)

{

c = FahrenheittoCelsius;

cout<< f华氏度是 c摄氏度" ;;

cout<< "继续? ;


其他

f = CelsiustoFahrenheit;

cout<< c摄氏度是 f华氏度;

cout<< "继续? ;


}

返回临时数;

}

双FahrenheitoCelsius(双f)

{

返回(f - 32)*(5.0 / 9);

}

double CelsiustoFahrenheit(双c)

{

返回(9 / 5.0 * c)+ 32;

}

解决方案

As******@msn.com 写道:


嘿所有人〜

我仍​​然是一个C ++新手所以请耐心等待。

我正在做一个带原型功能的温度转换程序。

基本上,我想知道你们中的一些人是否会看看我的代码

和批评它对我来说。我主要关注原型

功能如何工作以及我是否在我的代码中正确设计它们。

您的参与将不胜感激!


我的代码:



我假设你还没有尝试编译这个!


一些提示:


#include" stdafx.h"



非标准标题。


#include< iostream>

#include< cstdlib>


双FahrenheittoCelsius(double)= 0.0; //函数原型

double CelsiustoFahrenheit(double)= 0.0;



这些是语法错误,而不是原型。松散= 0.0。


int _tmain(int argc,_TCHAR * argv [])



是_TCHAR?使用


int main(int argc,char * argv [])


{

double tempFahrenheit,tempCelsius;

双倍温度;

char f;

char c;


cout<< "输入温度:; //提示用户输入temp

cin> temp;

cout<< "华氏度或摄氏度?; //如果用户没有指定f或c

问他们

if(temp == f)



分配到哪里?


{

c = FahrenheittoCelsius;



这里你尝试为char分配一个函数,这是一个非常奇怪的
要做的事情!你的意思是写


c = FahrenheittoCelsius(temp);


cout<< f华氏度是 c摄氏度" ;;

cout<< "继续? " ;;



缺少}


else


缺少
{


f = CelsiustoFahrenheit;

cout<< c摄氏度是 f华氏度;

cout<< "继续? ;


}


返回温度;

}

双倍华氏摄氏度(双f)

{

返回(f - 32)*(5.0 / 9);

}

double CelsiustoFahrenheit(双c)

{

返回(9 / 5.0 * c)+32;

}



-

Ian Collins。


< as ****** @ msn。消息中写道

news:11 ********************** @ y66g2000hsf.googlegr oups.com ...


嘿所有人〜

我还是一个C ++新手所以请耐心等待。

我''我正在做一个带有原型功能的温度转换程序。

基本上,我想知道你们中的一些人是否会看看我的代码

并为我批评它。我主要关注原型

功能如何工作以及我是否在我的代码中正确设计它们。

您的参与将不胜感激!


我的代码:


#include" stdafx.h"

#include< iostream>

#include< cstdlib>


双FahrenheittoCelsius(double)= 0.0; //函数原型



这不是纯虚方法。这是一个功能原型。它应该是

看起来像这样:

双倍FarenheitToCelcius(双倍);


double CelsiustoFahrenheit(double)= 0.0;



double CelciusToFarenheit(double);


int _tmain(int argc,_TCHAR * argv [])



这是非标准的,因为你做了一个Windows项目并且正在使用

标准头文件。这个新闻组不赞成。


project-> properties-> c / C ++ - >预编译标题

更改创建/使用预编译标题不使用预编译的头文件。

然后从项目中删除stdafx.cpp。将您的主要内容更改为:

int main()


{

double tempFahrenheit,tempCelsius;



最好叫这些Farenheit和Celcius让你不要迷惑自己。


双倍Farenheit,Celcious;


double temp;


char f;

char c;



这些不是字符f和c,而是char类型的变量

称为f和C。你可以摆脱这些。


cout<< "输入温度:; //提示用户输入temp

cin> temp;

cout<< "华氏度或摄氏度?; //如果用户没有指定f或c

问他们

if(temp == f)



你问的是temp == f。温度是双倍的。 f是一个char。我想你

其实想问问用户但是忘了这么做。


char torf;

cin> torf ;


if(torf ==''f'')


{

c = FahrenheittoCelsius;



你需要在那里调用一个函数。你的原型是:

双倍FarenheitToCelcius(双倍);

所以它将是:


Celcius = FarenheitToCelcius(temp);


cout<< f华氏度是 c摄氏度;



这不会起作用。它应该是:

cout<< Farenheit<< "华氏度是 << Celcius<< "度

摄氏度" ;;


cout<< "继续? ;


其他

f = CelsiustoFahrenheit;

cout<< c摄氏度是 f华氏度;



请参阅有关从FarenheitToCelcius转换的注释并将其修复以反映。


cout<< "继续? ;


}


返回温度;

}

双倍华氏摄氏度(双f)

{

返回(f - 32)*(5.0 / 9);

}

double CelsiustoFahrenheit(双c)

{

返回(9 / 5.0 * c)+ 32;

}



好​​的,尝试所有tis,然后实际尝试编译它。您将获得编译

找不到错误变量等。修复错误。如果您仍然卡住

再次发布新的固定代码。


您可以获得很多帮助!而且......我对我所犯的一些错误感到愚蠢。我不知道我想出了什么。但是,我还在学习。所以,我们走了。我在调试时遇到问题。它不会执行该程序。
执行该程序。所以,我可以构建并查看出现的错误

但它不会执行。我错过了什么?


这是我的*新*代码:


// Temperature.cpp:定义控制台的入口点

申请。

//

#include" stdafx.h"

#include< iostream>

#include< cstdlib>

using namespace std;


double FahrenheittoCelsius(double); //函数原型

double CelsiustoFahrenheit(double);


int main()

{

double tempFahrenheit,tempCelsius;

double temp;

char corf;

cout<< "输入温度:; //提示用户输入temp

cin> corf;


if(corf ==''f'')

{

tempCelsius = FahrenheittoCelsius(temp);

cout<< tempFahrenheit<< "华氏度是 << tempCelsius

<< "摄氏度" ;;

cout<< "继续? ;

如果

tempFahrenheit = CelsiustoFahrenheit(临时);

cout<< tempCelsius<< "摄氏度是 << tempFahrenheit<<

华氏度;

cout<< "继续? ;

}


返回0;

}

双倍华氏度摄氏度(双倍f)

{

返回(f - 32)*(5.0 / 9);

}

double CelsiustoFahrenheit(双c)

{

返回(9 / 5.0 * c)+32;

}



Hey Everyone~
I''m still a C++ Rookie so please bear with me on this.
I''m doing a temperature conversion program with prototype functions.
Basicly, I was wondering if some of you would take a look at my code
and critique it for me. I''m mostly concerned with how prototype
functions work and if I designed them correctly in my code.
Your participation would be greatly appreciated!

My Code:

#include "stdafx.h"
#include <iostream>
#include <cstdlib>

double FahrenheittoCelsius(double)= 0.0; //Function Prototype
double CelsiustoFahrenheit(double)= 0.0;

int _tmain(int argc, _TCHAR* argv[])
{
double tempFahrenheit, tempCelsius;
double temp;
char f;
char c;
cout << " Enter the Temperature: "; //Prompt user to enter temp
cin >temp;
cout << " Fahrenheit or Celsius?"; // If user does not specify f or c
ask them
if (temp == f)
{
c = FahrenheittoCelsius;
cout << f " degrees Fahrenheit is" c " degrees celsius ";
cout << " Continue? ";

else
f = CelsiustoFahrenheit;
cout << c " degrees Celsius is " f " degrees Fahrenheit ";
cout << " Continue? ";

}
return temp;
}
double FahrenheitoCelsius (double f)
{
return (f - 32) * (5.0/9);
}
double CelsiustoFahrenheit (double c)
{
return (9/5.0 * c) + 32;
}

解决方案

As******@msn.com wrote:

Hey Everyone~
I''m still a C++ Rookie so please bear with me on this.
I''m doing a temperature conversion program with prototype functions.
Basicly, I was wondering if some of you would take a look at my code
and critique it for me. I''m mostly concerned with how prototype
functions work and if I designed them correctly in my code.
Your participation would be greatly appreciated!

My Code:

I assume you haven''t tried to compile this!

Some hints:

#include "stdafx.h"

Non-standard header.

#include <iostream>
#include <cstdlib>

double FahrenheittoCelsius(double)= 0.0; //Function Prototype
double CelsiustoFahrenheit(double)= 0.0;

These are syntax errors, not prototypes. Loose the = 0.0.

int _tmain(int argc, _TCHAR* argv[])

That''s _TCHAR? Use

int main( int argc, char* argv[] )

{
double tempFahrenheit, tempCelsius;
double temp;
char f;
char c;
cout << " Enter the Temperature: "; //Prompt user to enter temp
cin >temp;
cout << " Fahrenheit or Celsius?"; // If user does not specify f or c
ask them
if (temp == f)

Where is f assigned?

{
c = FahrenheittoCelsius;

Here you attempt to assign a function to a char, which is a very strange
thing to do! Did you mean to write

c = FahrenheittoCelsius( temp );

cout << f " degrees Fahrenheit is" c " degrees celsius ";
cout << " Continue? ";

missing }

else

missing {

f = CelsiustoFahrenheit;
cout << c " degrees Celsius is " f " degrees Fahrenheit ";
cout << " Continue? ";

}
return temp;
}
double FahrenheitoCelsius (double f)
{
return (f - 32) * (5.0/9);
}
double CelsiustoFahrenheit (double c)
{
return (9/5.0 * c) + 32;
}


--
Ian Collins.


<As******@msn.comwrote in message
news:11**********************@y66g2000hsf.googlegr oups.com...

Hey Everyone~
I''m still a C++ Rookie so please bear with me on this.
I''m doing a temperature conversion program with prototype functions.
Basicly, I was wondering if some of you would take a look at my code
and critique it for me. I''m mostly concerned with how prototype
functions work and if I designed them correctly in my code.
Your participation would be greatly appreciated!

My Code:

#include "stdafx.h"
#include <iostream>
#include <cstdlib>

double FahrenheittoCelsius(double)= 0.0; //Function Prototype

This is not a pure virtual method. It is a function prototype. It should
look like this:
double FarenheitToCelcius(double);

double CelsiustoFahrenheit(double)= 0.0;

double CelciusToFarenheit(double);

int _tmain(int argc, _TCHAR* argv[])

This is non standard, becuase you did a windows project and are using
standard headers. That is frowned upon in this newsgroup.

project->properties->c/C++->Precompiled Headers
Change Create/Use Precompiled Header to Not Using Precompiled Header.
Then remove stdafx.cpp from your project. Change your main to:
int main()

{
double tempFahrenheit, tempCelsius;

Better to call these Farenheit and Celcius so you don''t confuse yourself.

double Farenheit, Celcious;

double temp;

char f;
char c;

These are not the characters ''f'' and ''c'' but variables of type char that are
called f and c. You can get rid of these.

cout << " Enter the Temperature: "; //Prompt user to enter temp
cin >temp;
cout << " Fahrenheit or Celsius?"; // If user does not specify f or c
ask them
if (temp == f)

You are asking if temp == f. temp is a double. f is a char. I think you
actually wanted to ask the user but forgot to do it.

char torf;
cin >torf;

if ( torf == ''f'' )

{
c = FahrenheittoCelsius;

You need to call a function there. Your prototype is:
double FarenheitToCelcius(double);
so it would be:

Celcius = FarenheitToCelcius( temp );

cout << f " degrees Fahrenheit is" c " degrees celsius ";

That ain''t going to work. It should be:
cout << Farenheit << " degrees Fahrenheit is" << Celcius << " degrees
celsius ";

cout << " Continue? ";

else
f = CelsiustoFahrenheit;
cout << c " degrees Celsius is " f " degrees Fahrenheit ";

See comments for converting from FarenheitToCelcius and fix this to reflect.

cout << " Continue? ";

}
return temp;
}
double FahrenheitoCelsius (double f)
{
return (f - 32) * (5.0/9);
}
double CelsiustoFahrenheit (double c)
{
return (9/5.0 * c) + 32;
}

Okay, try all tis, then actually try to compile it. You''ll get compilation
errors variables not found, etc.. Fix the errors. If you still get stuck
post again with your new fixed code.


You are a lot of help! And...I feel dumb about some of the mistakes
that I made. I have no clue where I came up with that. But, I''m still
learning. So, here we go. I am having a problem debugging. It won''t
execute the program. So, I can build and see the errors that come up
but it won''t execute. What am I missing?

Here is my *NEW* code:

// Temperature.cpp : Defines the entry point for the console
application.
//
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
using namespace std;

double FahrenheittoCelsius(double); //Function Prototype
double CelsiustoFahrenheit(double);

int main()
{
double tempFahrenheit, tempCelsius;
double temp;
char corf;
cout << " Enter the Temperature: "; //Prompt user to enter temp
cin >corf;

if (corf == ''f'')
{
tempCelsius = FahrenheittoCelsius( temp );
cout << tempFahrenheit << " degrees Fahrenheit is" << tempCelsius
<< " degrees celsius ";
cout << " Continue? ";
if
tempFahrenheit = CelsiustoFahrenheit(temp);
cout << tempCelsius << " degrees Celsius is " << tempFahrenheit << "
degrees Fahrenheit";
cout << " Continue? ";
}

return 0;
}
double FahrenheittoCelsius (double f)
{
return (f - 32) * (5.0/9);
}
double CelsiustoFahrenheit (double c)
{
return (9/5.0 * c) + 32;
}



这篇关于温度转换计划 - 仍然是一个新秀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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