所以我在編碼方面很新,正在解決我在書中發現的一個問題。這是代碼-
#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>)'})|
你能告訴我我哪里出錯了嗎?謝謝你
uj5u.com熱心網友回復:
小時是一個函式
void hours(double hours,string subs){
所以這個說法
cin >> hours >> subs;
是不正確的,沒有任何意義。
看來你在輸入代碼時打錯了。
例如,未宣告 main even 中使用的名稱 hourse2
uj5u.com熱心網友回復:
有很多錯誤(見上面的評論)。我已經為你修復了,與你的原始代碼進行比較
#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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/376893.html
下一篇:為什么嵌套宏沒有展開?
