| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3989 人关注过本帖
标题:求解,华氏度与摄氏度的相互转换的程序提示错误
只看楼主 加入收藏
xzq19960823
Rank: 1
来 自:浙江
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-7-14
收藏
 问题点数:0 回复次数:5 
求解,华氏度与摄氏度的相互转换的程序提示错误
程序代码:
#include<iostream>
#include<cstdlib>
#include<cstring>
using namespace std;
void mutual(char* val)
{
    unsigned int x;
    char l_s;
    double a_s,c_s;
    x=unsigned int strlen(const char* val);
    l_s=*(val+x-1);
    a_s=double atof(*(val+x-2));
    if(l_s=='C'||l_s=='c')cout<<"Degrees Fahrenheit"<<a_s*9/5+32<<"F"<<endl;
    else if(l_s=='F'||l_s=='f')cout<<"Degrees Celsius"<<5/9*(a_s-32)<<"C"<<endl;
    else cout<<"Please enter the correct temperature."<<endl;
}
int main()
{
    char value;
    cout<<"Please enter the fahrenheit or celsius.Remember plus unit."<<endl;
    cin.get(value);
    mutual(&value);
    return 0;
}

--------------------Configuration: text3 - Win32 Debug--------------------
Compiling...
text3.cpp
F:\VC Class\3rd\text3.cpp(10) : error C2062: type 'unsigned int' unexpected
F:\VC Class\3rd\text3.cpp(12) : error C2062: type 'double' unexpected
Error executing cl.exe.

text3.exe - 2 error(s), 0 warning(s)

[此贴子已经被作者于2016-7-14 09:35编辑过]

搜索更多相关主题的帖子: 摄氏度 华氏度 
2016-07-14 09:33
U201010009
Rank: 3Rank: 3
等 级:论坛游侠
威 望:6
帖 子:73
专家分:178
注 册:2013-2-25
收藏
得分:0 
x=unsigned int strlen(const char* val);中unsigned int要用括号括起来(unsigned int),表示类型转换,strlen()返回size_t也就是int类型,需要类型转换,要加括号把要转换成的类型括起来。这两个错误都是同样的问题。
2016-07-14 10:02
xzq19960823
Rank: 1
来 自:浙江
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-7-14
收藏
得分:0 
回复 2楼 U201010009
括起来了,然后提示了这个
--------------------Configuration: text3 - Win32 Debug--------------------
Compiling...
text3.cpp
F:\VC Class\3rd\text3.cpp(10) : error C2143: syntax error : missing ')' before 'const'
F:\VC Class\3rd\text3.cpp(10) : error C2660: 'strlen' : function does not take 0 parameters
F:\VC Class\3rd\text3.cpp(10) : error C2059: syntax error : ')'
F:\VC Class\3rd\text3.cpp(12) : error C2664: 'atof' : cannot convert parameter 1 from 'char' to 'const char *'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.

text3.exe - 4 error(s), 0 warning(s)

好好学习,天天向上。
2016-07-14 10:05
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9007
专家分:53942
注 册:2011-1-18
收藏
得分:0 
此句 x=unsigned int strlen(const char* val);
报错 error C2062: type 'unsigned int' unexpected

此句 a_s=double atof(*(val+x-2));
报错 error C2062: type 'double' unexpected

程序代码:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

void mutual( void )
{
    cout << "Please enter the fahrenheit or celsius (Remember plus unit)." << endl;
    string str;
    if( cin>>str )
    {
        istringstream ss( str );
        double val;
        char unit;
        if( (ss>>val>>unit) && (ss>>ws).eof() && (unit=='F'||unit=='f'||unit=='C'||unit=='c') )
        {
            if( unit=='F' || unit=='f' )
                cout << "Degrees Celsius " << 5./9.*(val-32) << 'C' << endl;
            else
                cout << "Degrees Fahrenheit " << 9./5.*val+32 << 'F' << endl;
        }
        else
        {
            cout << "Please enter the correct temperature." << endl;
        }
    }
}

int main( void )
{
    mutual();
    return 0;
}

2016-07-14 10:15
U201010009
Rank: 3Rank: 3
等 级:论坛游侠
威 望:6
帖 子:73
专家分:178
注 册:2013-2-25
收藏
得分:0 
细看了下,你的代码有较多问题
①一般计算数字的话会定义double类型的value,再来加减计算,你定义的是char类型的value,char类型的数字范围好像是-127到128
②即便用了char类型value计算,你计算后对value加减然后cout,比如char value = 15;cout << value + 6 << endl;输出的结果不是21,而是21对应的字符
③应该这么调用strlen(val),因为val在你定义void mutual(char* val)时,作为形参已经定义了,不需要再const char* val
④atof()参数是const char*,传入的*(val + x - 2)有些看不懂,即便对字符操作也应该是(*val)(这个是取该地址的首字符)后再操作

建议你代码重新构思下,用double存值计算,实现温度转换

[此贴子已经被作者于2016-7-14 10:20编辑过]

2016-07-14 10:19
happynocai
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2016-7-14
收藏
得分:0 
变量名比较乱,去参考别人怎么定义的。下划线用在变量名中间比较“丑”。
void mutual(const char* val)
strlen(const char* val)直接改为strlen(val);
a_s=double atof(*(val+x-2));参数错了(*(val+x-2));是char。可以 strncpy(st1,st2,n); 或str2 = str1.substr(pos1,len1);
2016-07-14 10:39
快速回复:求解,华氏度与摄氏度的相互转换的程序提示错误
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017107 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved