PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, June 26, 2022

[FIXED] why does using cin function give me error?

 June 26, 2022     c++, c++11, compiler-errors, undeclared-identifier     No comments   

Issue

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


Solution

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;

}


Answered By - SHR
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing