【问题标题】:why does using cin function give me error?为什么使用 cin 函数会给我错误?
【发布时间】:2022-01-11 19:31:44
【问题描述】:

所以我在编码方面还很陌生,并且正在解决我在书中发现的一个问题。 这是代码-

#include <iostream>
#include <string>
using namespace std;
void hours(double hours, string subs)
{
    if (hours > 12 || subs != "AM" || "PM") {
        int tries = 0;
        while (tries <= 50)
            ;
        {
            cout << "please check your input.\n";
            tries++;
        }
    }
    int i;
    if (subs == "AM") {
        int i = 0;
    }
    else {
        int i = 1;
    }
    switch (i) {
        {
        case 0:
            int newhours1 = hours * 60;
            break;
        }
        {
        case 1:
            int newhours2 = hours + 12;
            int newhours3 = newhours2 * 60;
            break;
        }
    }
}

int main()
{
    cout << "Welcome to the time convertor.\n";
    cout << "What's your initial time?.\n";
    cin >> hours >> subs;
    void hours(hours, subs);
    cout << "What's your second number?.\n";
    cin >> hours2 >> subs2;
    void hours(hours2, subs2);
    if (newhours3 = > newhours1) {
        cout << "Your answer is"
             << "" << newhours3 - newhours1 << "\n";
    }
    else if (newhours1 = > newhours3) {
        cout << "Your answer is"
             << "" << newhours1 - newhours3 << "\n";
    }
    return 0;
}

每当我尝试运行它时,它都会显示错误-

C:\Users\adhis\Documents\codes\Untitled1.cpp|30|error: no match for 'operator>>' (operand types are 'std::istream' {aka 'std::basic_istream<char>'} and 'void(double, std::__cxx11::string)' {aka 'void(double, std::__cxx11::basic_string<char>)'})|

你能告诉我哪里出错了吗? 谢谢你

【问题讨论】:

  • if (hours &gt; 12 || subs != "AM" || "PM") 不会像您认为的那样做。此外,在一个函数中声明newhours1newshour2newhours3 并不意味着它们可以像那样在完全不同的函数中使用。 C++ 不能以这种方式工作。也许您可能想回到教科书中的一两章,先尝试一些更简单的练习程序?

标签: c++ c++11 compiler-errors undeclared-identifier


【解决方案1】:

有很多错误(参见上面的 cmets)。我已经为您修复了,与您的原始代码进行比较

#include <iostream>
#include <string>
using namespace std;

double calculate_hours(double hours, string subs)
{
    if( hours <= 0 || hours > 12 || ( subs != "AM" && subs != "PM")){
        cout << "please check your input.\n";
        return -1;
    }
   
    if(subs == "AM"){
         return hours * 60;
    }else{
        return  (hours + 12) * 60;
    }
}

int main(){
    double h1,h2, t1,t2;
    std::string s1,s2;
    cout << "Welcome to the time convertor.\n";
    cout << "What's your initial time?.\n";
    
    do{
       cin >> h1 >> s1;
       t1 = calculate_hours(h1,s1);
    } while (t1 < 0);
    cout << "What's your second number?.\n";
   
    do {
        cin >> h2>>s2;
        t2 = calculate_hours(h2,s2);
    } while (t2 < 0);
    
    if(t2 >= t1){
        cout << "Your answer is " << "" << t2 - t1 << "\n";
    }else  {
        cout << "Your answer is "<< "" << t1 - t2 << "\n";
    }
    return 0;

}

【讨论】:

  • 谢谢,我注意到我奇怪地使用了在它之外的 void 函数中声明的变量。而且我可以通过像你这样使用 do 循环来使事情变得更容易,我想我还有很多东西要学,再次感谢
【解决方案2】:

小时是一个函数

void hours(double hours,string subs){

所以这个说法

cin >> hours >> subs;

不正确,没有意义。

您在输入代码时似乎打错了。

例如 main even 中使用的名称 hourse2 没有声明

【讨论】:

  • thankssssss,不敢相信我忽略了这一点
  • 需要详细说明吗?
  • @AdhishrayaSharma 什么不清楚?该程序包含许多未声明的标识符。
  • 重试循环是无限的,实际上并没有重试,存在标识符作用域问题等等。
  • + 有许多局部变量被视为全局变量。并且您转发声明该函数而不是调用它。还有更多
猜你喜欢
  • 2019-05-17
  • 1970-01-01
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
  • 2019-12-09
  • 1970-01-01
  • 2013-12-10
  • 2013-11-14
相关资源
最近更新 更多