为什么使用 cin 函数会给我错误?

why does using cin function give me error?

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

#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>)'})|

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

小时是一个函数

void hours(double hours,string subs){

所以这个声明

cin >> hours >> subs;

不正确,没有意义。

您输入代码时好像打错了。

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

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

#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;

}