need help on cpp file
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: need help on cpp file

Hybrid View

  1. #1
    Join Date
    Feb 2013
    Posts
    1

    Question need help on cpp file

    I am suppose to make a program that will output a line for a specific age group,but everytime I execute the exe file it gives me the desired line and also the last line included everytime.Where did I go wrong with it including the last line everytime?

    #include <iostream>
    using namespace std;
    int main()
    {
    double age;
    cout<<"Enter your age:";
    cin>>age;
    if (0<age && age <6)
    cout<< "You are considered a young minor. But, in approximately ___ year(s) you will be able to attend school!"<<endl;

    else if (6<=age && age<16)
    cout<< "In approximately ___ year(s) you will be able to drive!"<<endl;

    else if (16<=age && age<21)
    cout<< "In approximately ___ year(s) you will be of legal drinking age."<<endl;

    else if (21<= age && age<35)
    cout<< "You are of legal drinking age. In approximately ___ year(s) you will be able to run for president!"<<endl;

    else if (35<= age && age<120)
    cout<< "You are old enough to run for U.S. President."<<endl;

    else(age >=120);
    cout<< "You are too old to run for U.S. President."<<endl;

    }

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,517

    Re: need help on cpp file

    Quote Originally Posted by trashedsoul View Post
    I am suppose to make a program that will output a line for a specific age group,but everytime I execute the exe file it gives me the desired line and also the last line included everytime.Where did I go wrong with it including the last line everytime?

    #include <iostream>
    using namespace std;
    int main()
    {
    double age;
    cout<<"Enter your age:";
    cin>>age;
    if (0<age && age <6)
    cout<< "You are considered a young minor. But, in approximately ___ year(s) you will be able to attend school!"<<endl;

    else if (6<=age && age<16)
    cout<< "In approximately ___ year(s) you will be able to drive!"<<endl;

    else if (16<=age && age<21)
    cout<< "In approximately ___ year(s) you will be of legal drinking age."<<endl;

    else if (21<= age && age<35)
    cout<< "You are of legal drinking age. In approximately ___ year(s) you will be able to run for president!"<<endl;

    else if (35<= age && age<120)
    cout<< "You are old enough to run for U.S. President."<<endl;

    else(age >=120);
    cout<< "You are too old to run for U.S. President."<<endl;

    }
    Your last else expression is the reason!
    It should be either
    Code:
    else
        cout<< "You are too old to run for U.S. President."<<endl;
    or
    Code:
    else if(age >=120);
        cout<< "You are too old to run for U.S. President."<<endl;
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center