Close
Close

Always display error what to do ??????

   edicted_to_coding

#include<iostream>
using namespace std;
int main()
{

int age=100;
char sex,M,F;
cout<<"Hello there "<<endl;
cout<<"enter your age"<<endl;
cin>>age;
cout<<"are you male(M) or female(F)"<<endl;
cin>>sex;

if(sex==F && age>20 && age<40)
{
    cout<<"Hey lady you will work in urban areas "<<endl;

}
else if(sex==M && age>20 && age<40)
{
    cout<<"Hello there you can work anywhere "<<endl;
}
else if (sex==M && age>20 && age<60)
{

    cout<<"Hello sir you will work in urban areas only  "<<endl;

}
else
    cout<<"error"<<endl;
return 0;
}


Answers

  •   

    ‘F’ instead of F; ‘M’ instead of M

    #include<iostream>
    using namespace std;
    int main()
    {
    
    int age=100;
    char sex,M,F;
    cout<<"Hello there "<<endl;
    cout<<"enter your age"<<endl;
    cin>>age;
    cout<<"are you male(M) or female(F)"<<endl;
    cin>>sex;
    
    if(sex=='F' && age>20 && age<40)
    {
        cout<<"Hey lady you will work in urban areas "<<endl;
    
    }
    else if(sex=='M' && age>20 && age<40)
    {
        cout<<"Hello there you can work anywhere "<<endl;
    }
    else if (sex=='M' && age>20 && age<60)
    {
    
        cout<<"Hello sir you will work in urban areas only  "<<endl;
    
    }
    else
        cout<<"error"<<endl;
    return 0;
    }
    

     



  •   

    age variable is initialize in starting of a programme.age=100 that condition is out of all condition in this programme so programme will be run only last else condition and print error. 



  •    meeloun

    许多作业代写    https://www.yydaixie.com/    机构拥有专业的团队,他们对于作业有深厚的理解和丰富的经验。因此,通过作业代写服务,消费者可以获得高质量的作业。



  •    tellsteve98

    Initiatives launched during this COVID-19 crisis by Parmigiani support their retailers and the salespeople.The A386 version is an extremely faithful reproduction of the original from 196According to Zenith they actually went so far as to laser scan an original model in order to ensure as high-fidelity a re-issue as possible.dr version of all this is usually rendered as cancels out the negative effects of gravity on the accuracy of a watch which is generally enough to go on withYou simple press the lever up or down to adjust the date forward or backward one day.replica cartier horloges



  •    patricajohnson51

    It seems like the issue you're encountering is related to the program always displaying an error message. The problem lies in the way you're comparing characters in your code. When you compare sex with 'F' and 'M', you're comparing it with the ASCII values of 'F' and 'M' rather than the characters themselves. To fix this, you should compare sex with character literals 'F' and 'M' directly. Here's the corrected version of your code:

    #include
    using namespace std;
    
    int main() {
        int age = 100;
        char sex;
    
        cout << "Hello there!" << endl;
        cout << "Enter your age: ";
        cin >> age;
    
        cout << "Are you male (M) or female (F)? ";
        cin >> sex;
    
        if ((sex == 'F' || sex == 'f') && age > 20 && age < 40) {
            cout << "Hey lady, you will work in urban areas." << endl;
        }
        else if ((sex == 'M' || sex == 'm') && age > 20 && age < 40) {
            cout << "Hello there, you can work anywhere." << endl;
        }
        else if (sex == 'M' || sex == 'm' && age > 20 && age < 60) {
            cout << "Hello sir, you will work in urban areas only." << endl;
        }
        else {
            cout << "Error: Invalid input or age range." << endl;
        }
    
        return 0;
    }
    

    In this corrected version, I've changed the comparisons to directly compare sex with character literals 'F', 'f', 'M', and 'm'. This should resolve the issue of the program always displaying an error message.

    If you encounter any further challenges or need help with C++ assignment, there are resources available to support your learning journey. Whether you seek clarification on concepts, debugging assistance, or guidance in completing your tasks, seeking help from knowledgeable professionals can be invaluable. Remember, learning programming is a dynamic process that often benefits from collaboration and expert guidance. Additionally, exploring online communities or educational platforms can provide valuable insights and assistance. If you're struggling with your C++ assignment, consider leveraging the diverse resources available to enhance your understanding and proficiency in programming. You might find programminghomeworkhelp.com particularly useful for finding the assistance you need to excel in your C++ assignments.



Ask Yours
Post Yours
Write your answer