I am supposed to write a program that asks for the name of a pole vaulter and the dates and vault heights ( in meters) of the athlete’s three best vaults. It should then report in height order ( best first), the date on which each vault was made, and its height. Input Validation: Only accept values between 2.0 and 5.0 for the heights.

I have been working on this for hours and trying so many different things! Please help! I have no idea how to put the results in highest to lowest order, and I thought I had the input validation right, but when I type a number greater than 5.0 ( or even one within the range of 2.0 to 5.0 the program shuts down (though when I type a number less than 2.0, it works fine!).

#include <iostream>
#include <string>

using namespace std;

int main()

{
    // Define variables.

    string name;
    string month;
    string day;
    string year;
    bool height;

    // Get inputs.

    // Get the name of the pole vaulter. 

    cout << "Enter the first and last name of the pole vaulter: \n";
        cin >> name >> name;

    // Get the date of the first vault.

    cout << " Enter the numeric month, day, and year of the first vault, putting a space between each entry: \n";
        cin >> month >> day >> year;

    // Get the height of the first vault. 

    cout << " Enter the height (between 2.0 and 5.0 meters) of the first vault: \n";
        cin >> height;

    // Do not accept valutes greater than 5.0

        if (height > 5.0)
    {
        cout << " The number you entered is too large. \n";
        height = false;
    }

    // Do not accept values less than 2.0 

    if (height < 2.0)
    {
        cout << " The number you entered is too small. \n";
        height = false;
    }

    // Get the date of the second vault.

    cout << " Enter the numeric month, day, and year of the second vault, putting a space between each entry: \n";
        cin >> month >> day >> year;

    // Get the height of the second vault. 

    cout << " Enter the height (between 2.0 and 5.0 meters) of the second vault: \n";
        cin >> height;


    //Do not accept valutes greater than 5.0

        if (height > 5.0)
    {
        cout << " The number you entered is too large. \n";
        height = false;
    }

    // Do not accept values less than 2.0 

    if (height < 2.0)
    {
        cout << " The number you entered is too small. \n";
        height = false;
    }

    // Get the date of the third vault.

    cout << " Enter the numeric month, day, and year of the third vault, putting a space between each entry: \n";
        cin >> month >> day >> year;

    // Get the height of the third vault. 

    cout << " Enter the height (between 2.0 and 5.0 meters) of the third vault: \n";
        cin >> height;


    //Do not accept valutes greater than 5.0

        if (height > 5.0)
    {
        cout << " The number you entered is too large. \n";
        height = false;
    }

    // Do not accept values less than 2.0 

    if (height < 2.0)
    {
        cout << " The number you entered is too small. \n";
        height = false;

    cin.ignore();
    cin.get();

    }

    if (height == true)
    {
    cout << " The height of the best vault was " << height << " meters and was made on " << month << "/"
         << day << "/" << year << " . \n";
    cout << " The height of the second best vault was " << height << " meters and was made on " << month << "/"
         << day << "/" << year << " . \n";
    cout << " The height of the third best vault was " << height << " meters and was made on " << month << "/"
         << day << "/" << year << " . \n";

    cin.ignore();
    cin.get();

    }



    return 0;

}

Dani AI

Generated

The program posted by has a few distinct root causes: a wrong type for height (so values become true/false), reusing the same variables for all three vaults (so each entry overwrites the previous), and no proper input-validation loop or sorting step. and correctly pointed out type and variable issues, and ’s suggestion to plan with pseudo-code is good practice here.

A clear, simple approach:

  • Read the athlete name with getline so spaces are preserved.
  • Model each vault as a small struct (month, day, year, height) and store three of them in a vector.
  • For each vault use a validation loop that checks both stream errors and range (2.0–5.0); on bad input clear cin and ignore the rest of the line before retrying.
  • After collecting all three, sort the vector by height descending (use std::sort with a lambda) and print in order.

Small, copyable examples for the key parts (type, validation, sort):

struct Vault { int month, day, year; double height; };

double readHeight() {
  double h;
  while (!(std::cin >> h) || h < 2.0 || h > 5.0) {
    std::cin.clear();
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    std::cout << "Enter a number between 2.0 and 5.0: ";
  }
  return h;
}

std::sort(vaults.begin(), vaults.end(),
          [](const Vault& a, const Vault& b){ return a.height > b.height; });

Troubleshooting notes: do not use bool for numeric heights (and int loses fractional meters); avoid reusing one set of date/height variables; after any >> failure always clear() and ignore() to recover the input stream; use std::fixed<<std::setprecision(2) for consistent meters formatting. This yields robust input, correct ordering, and easy-to-read output.

Recommended Answers

All 3 Replies

You have to change the data type of height. At this yime it can store a 0 or 1. Not 2-5 ok so you have to switch to int type.
int height;

if you get solve your problem I will also tell you some tricks to short this code.

first fix it.

This is a good example of why you want to write pseudo-code and model your processes first - how to solve the problem in abstract terms - before you write the program. I have been writing a LOT of complex software for over 30 years, and I NEVER start a project before I design and model what it is going to do. It is an iterative process, and as I begin the implementation I change the model to reflect what I have learned when writing the program. My current project is a PHP web-based project - a language I never worked with directly until last week. This process has helped me to get over the many hurdles of presenting forms, getting data into other modules, and then using that to extract the desired data from a MySQL database.

1)you are using the same height for all the three vaults..
2)declare three separate height variables for each vault e.g height1,height2,height3..
3)change the data type of height,you are using bool which takes only true or false use float or double for height.
4)other common mistake is you are using same date for all three vaults,use separate dates(int day,int month,int year) similarly day2,month2,year2,same goes for three like day3,month3,year3...hope it works

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.