困扰小弟我许久的一道难题,哪位大神帮忙解决一下
困扰我许久的一道难题,哪位大神帮忙解决一下
#include <iostream>
#include <cctype>
#include <iomanip>
using namespace std;
int promptAndReadInt ( string promtMessage);
int convertToGrams( int pounds, int ounces);
void outputResults(int grams,int kilograms);
int main(){
int pounds, ounces, grams,kilograms;
cout << "~Welcome to the weight converter program~" << endl << endl;
cout << " This progam will convert pounds and ounces to kilograms and grams " <<endl;
cout << " First I will prompt you for number of pounds and then the number of ounces " << endl;
pounds =promptAndReadInt ("Pounds: ");
ounces = promptAndReadInt("Ounces: ");
grams = convertToGrams(pounds, ounces);
outputResults(grams,kilograms);
return 0;
}
int promptAndReadInt(string promtMessage)
{
cout << promtMessage;
char ch;
cin >> ch;
while (isdigit(ch)==false)
cin >> ch;
cin.putback(ch);
int response;
cin >> response;
return response;
}
int convertToGrams(int pounds, int ounces)
{
int grams,kilograms;
grams = pounds*453.6 + ounces*28.3495+0.5;
kilograms = grams/1000;
grams = grams-kilograms*1000;
return grams;
return kilograms;
}
void outputResults( int grams,int kilograms){
cout << kilograms <<endl;
cout << grams <<endl;
}
这是我的程序,我想输入磅和盎司,然后转化为千克和克,其中第二个函数int promptAndReadInt(string promtMessage)是保证程序可以正确读入数字的,例如输入aass12asd45fg程序会自动识别出12和45,我的问题是计算得到的结果不正确,不知道为什么kilograms总是一个特别大的数,~Welcome to the weight converter program~
This progam will convert pounds and ounces to kilograms and grams
First I will prompt you for number of pounds and then the number of ounces
Pounds: 1
Ounces: 1
1978381005
482
Process returned 0 (0x0) execution time : 6.412 s
Press any key to continue.
这是我的结果,而正确结果应该是 ~ Welcome to the weight converter program ~
This progam will convert pounds and ounces to kilograms and grams
First I will prompt you for number of pounds and then the number of ounces
Pounds: 1
Ounces: 1
RESULTS:
1 Pounds 1 Ounces converts to
0 Kilograms 482 Grams
请按任意键继续. . .
请帮我看一下,这是为什么呢?困扰我好久了。
------解决思路----------------------
原因很简单,你输出的kilograms变量是main函数里未初始化的,其它函数里用的kilograms是函数里面定义的局部变量
#include <iostream>
#include <cctype>
#include <iomanip>
using namespace std;
int promptAndReadInt ( string promtMessage);
int convertToGrams( int pounds, int ounces);
void outputResults(int grams,int kilograms);
int main(){
int pounds, ounces, grams,kilograms;
cout << "~Welcome to the weight converter program~" << endl << endl;
cout << " This progam will convert pounds and ounces to kilograms and grams " <<endl;
cout << " First I will prompt you for number of pounds and then the number of ounces " << endl;
pounds =promptAndReadInt ("Pounds: ");
ounces = promptAndReadInt("Ounces: ");
grams = convertToGrams(pounds, ounces);
outputResults(grams,kilograms);
return 0;
}
int promptAndReadInt(string promtMessage)
{
cout << promtMessage;
char ch;
cin >> ch;
while (isdigit(ch)==false)
cin >> ch;
cin.putback(ch);
int response;
cin >> response;
return response;
}
int convertToGrams(int pounds, int ounces)
{
int grams,kilograms;
grams = pounds*453.6 + ounces*28.3495+0.5;
kilograms = grams/1000;
grams = grams-kilograms*1000;
return grams;
return kilograms;
}
void outputResults( int grams,int kilograms){
cout << kilograms <<endl;
cout << grams <<endl;
}
这是我的程序,我想输入磅和盎司,然后转化为千克和克,其中第二个函数int promptAndReadInt(string promtMessage)是保证程序可以正确读入数字的,例如输入aass12asd45fg程序会自动识别出12和45,我的问题是计算得到的结果不正确,不知道为什么kilograms总是一个特别大的数,~Welcome to the weight converter program~
This progam will convert pounds and ounces to kilograms and grams
First I will prompt you for number of pounds and then the number of ounces
Pounds: 1
Ounces: 1
1978381005
482
Process returned 0 (0x0) execution time : 6.412 s
Press any key to continue.
这是我的结果,而正确结果应该是 ~ Welcome to the weight converter program ~
This progam will convert pounds and ounces to kilograms and grams
First I will prompt you for number of pounds and then the number of ounces
Pounds: 1
Ounces: 1
RESULTS:
1 Pounds 1 Ounces converts to
0 Kilograms 482 Grams
请按任意键继续. . .
请帮我看一下,这是为什么呢?困扰我好久了。
------解决思路----------------------
原因很简单,你输出的kilograms变量是main函数里未初始化的,其它函数里用的kilograms是函数里面定义的局部变量