C++ C++;,程序输出无效输入,但继续循环

C++ C++;,程序输出无效输入,但继续循环,c++,call-by-value,C++,Call By Value,我正在尝试编写一个程序,根据通话时间、一周中的哪一天和通话时间计算通话成本。它必须是所有按值调用函数,并输出一个选项以重复程序 我的问题是,当我输入一个无效的时间输入时,例如:37,它输出无效的输入,但继续到日输入,而不是返回到时间输入。我是一个新的程序员,已经尝试了我能想到的一切来修复它,但它要么陷入整个程序的无限循环中 提前感谢您的帮助 #include <iostream> using namespace std; bool validateUserInputTime(int

我正在尝试编写一个程序,根据通话时间、一周中的哪一天和通话时间计算通话成本。它必须是所有按值调用函数,并输出一个选项以重复程序

我的问题是,当我输入一个无效的时间输入时,例如:37,它输出无效的输入,但继续到日输入,而不是返回到时间输入。我是一个新的程序员,已经尝试了我能想到的一切来修复它,但它要么陷入整个程序的无限循环中

提前感谢您的帮助

#include <iostream>
using namespace std;

bool validateUserInputTime(int,char,int);
bool validateUserInputDay(string);
bool validateUserInputCallLength(int);
double calculateTotalCost(int,int,string,int);

string days[]={"Mo" , "Tu" , "We" , "Th" , "Fr" , "Sa" , "Su"};
float cost,fcost;

int main()
{
int hour;
int min;
int time;
char colon;
char answer = 'y';
string day;
string s;
bool result;

while(answer =='y')
{
    cout<<"Enter the time the call starts in 24-hour rotation: "<<endl;
    cin>>hour>>colon>>min;


    result=validateUserInputTime(hour,colon,min);
    if(cin.fail())
    {
        cout << "Invalid time input."<<endl;
        cout<<"Please try again."<<endl<<endl<<endl;
        cin.clear();

    }

    day=validateUserInputDay(s);
    if(cin.good())
    {
        cout<<"Enter the first two letters of the day of the week:";
        cin>>day;
    }


    cout<<"Enter the length of the call in minutes:"<<endl;
    cin>>time;

    result=validateUserInputCallLength(time);

    if(result==0)
    {
        cout<<"Invalid minute Input."<<endl;
        cout<<"Please try again."<<endl<<endl<<endl;

        continue;
    }

    fcost= calculateTotalCost(hour,min,day,time);

    cout<<"Cost of the call: $" << fcost<<endl;
    cout<<"Do you want to repeat the program?";

    cin>>answer;


}

return 0;

}
bool validateUserInputTime(int hour1,char ch,int min1)
{
    if (cin.fail())
    {
        cout << "Invalid time input."<<endl;
        cout<<"Please try again."<<endl<<endl<<endl;
        cin.clear();
    }
    if(hour1 < 0)
    {
        return false;
    }

    if(min1 < 0)
    {
        return false;
    }
    if(ch!=':')
    {
        return false;
    }
    else
        return true;

}
bool validateUserInputDay(string s)
{
    int next=0;

    for(int i = 0; i < 7; i++)
    {
        if(days[i] == s){

        next=1;
    }
        if(cin.fail())
        {
            cout<<"Invalid day inpuT."<<endl;

            cin.clear();
        }    

}

    if(next==1)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool validateUserInputCallLength(int time2)
{

if(time2<0)
{
    return false;
}
else
{
    return true;
}

}
double calculateTotalCost(int hour3,int min3,string d,int time3)
{

if((d=="Sa")||(d=="Su"))

{

    cost=0.15*time3;

}

else

{

    if((hour3>=8)&&(min3<18))

    {

        cost=0.40*time3;

    }

    else

        cost=0.25*time3;

}

return cost;

}
#包括
使用名称空间std;
bool validateUserInputTime(int,char,int);
bool validateUserInputDay(字符串);
bool validateUserInputCallLength(int);
双重计算总成本(int,int,string,int);
字符串天[]={“Mo”、“Tu”、“We”、“Th”、“Fr”、“Sa”、“Su”};
浮动成本;
int main()
{
整小时;
int-min;
整数时间;
半结肠;
char-answer='y';
弦日;
字符串s;
布尔结果;
而(答案=='y')
{
库特克隆>>分钟;
结果=validateUserInputTime(小时、冒号、分钟);
if(cin.fail())
{

cout尝试使用循环。循环将停止程序进行,直到结果值为真

result = false;
while (!result)
{
    cout<<"Enter the time the call starts in 24-hour rotation: "<<endl;
    cin>>hour>>colin>>min;
    result=validateUserInputTime(hour,colin,min);
}
result=false;
而(!结果)
{
库特林>>分钟;
结果=validateUserInputTime(小时,科林,分钟);
}
您还忘了在validateUserInputTime上放置错误的返回语句。输入非数字字符也可能会使程序崩溃,cin.ignore恰好修复了它

if (cin.fail())
{
    cout << "Invalid time input."<<endl;
    cout<<"Please try again."<<endl<<endl<<endl;
    cin.clear();
    cin.ignore();
    return false;
}
if(cin.fail())
{

cout如果要解析用户输入的内容,请执行类似的操作

std::string time;
std::getline(std::cin, time);
现在检查是否有一个“:”
auto pos=time.find(“:”);if(pos!=-1){}
然后取出小时部分
time.substr(0,pos)
然后是分钟部分
time.substr(pos+1)
然后使用例如
stoi()
检查它们是否为有效的小时和分钟


另外,最好是
do{}while()
而不是
while(答案='y'){…}

您没有检查
validateUserInputTime
返回的内容。如果(结果==false){…},则需要执行类似
的操作
大括号中的代码与您在
validateUserInputCallLength
之后的代码类似。您的问题没有带空格就离题了。我之所以这样评论,是因为我花了一点时间才意识到“colin”是什么。:“拼写为冒号。我一定是无意中删除了if语句,我刚刚编辑了它,我该如何更正我的问题请讲主题,我很抱歉我会将其改为冒号。我建议您改为阅读用户的行,然后分析该行。例如,使用std::getline,然后解析字符串。这样您可以更好地控制用户输入的内容。