Home > Net >  why does using cin function give me error?
why does using cin function give me error?

Time:12-07

so i'm pretty new at coding and was solving a problem that i found in the book. Here's the code-

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

whenever im trying to run it,it's showing the error-

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

can you tell me where i'm going wrong? thank you

CodePudding user response:

hours is a function

void hours(double hours,string subs){

So this statement

cin >> hours >> subs;

is incorrect and does not make a sense.

It seems you made typos when were typing the code.

For example the name hourse2 used in main even is not declared

CodePudding user response:

There were many errors (see the comments above). I've fixed it for you, compare to your original code

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

}
  • Related