#Issue when validating input where a negative number prints the invalid message twice

7 messages · Page 1 of 1 (latest)

thorny solsticeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

soft raven
#

Oh here's the console output in both scenarios:

Select a medium:
a) Air
b) Water
c) Steel
Enter your choice (a/b/c): 3
Invalid choice. Please enter a, b, or c:```
#
a) Air
b) Water
c) Steel
Enter your choice (a/b/c): -3
Invalid choice. Please enter a, b, or c: Invalid choice. Please enter a, b, or c:```
thorny solsticeBOT
#

@soft raven

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.

soft raven
#
#include <iomanip>
using namespace std;

int main() {
    // Set speeds
    const double airSpeed = 1100;
    const double waterSpeed = 4900;
    const double steelSpeed = 16400;

    char choice;
    double distance, time;

    // Display menu of materials
    cout << "Select a medium:\n"
         << "a) Air\n"
         << "b) Water\n"
         << "c) Steel\n"
         << "Enter your choice (a/b/c): ";
    cin >> choice;

    // Validate user input
    while (choice != 'a' && choice != 'b' && choice != 'c') {
        cout << "Invalid choice. Please enter a, b, or c: ";
        cin >> choice;
    }

    // Get distance from the user
    cout << "Enter the distance a sound wave will travel in the selected medium (in feet): ";
    cin >> distance;

    // Validate distance
    while (distance < 0) {
        cout << "Distance cannot be less than 0. Please enter a valid distance: ";
        cin >> distance;
    }

    // Calculate time
    time = distance / airSpeed;

    // Change output depending on material
    if (choice == 'a') {
        cout << "A sound wave takes " << fixed << setprecision(4) << time
             << " seconds to travel " << distance << " feet through air.\n";
    } else if (choice == 'b') {
        cout << "A sound wave takes " << fixed << setprecision(4) << time
             << " seconds to travel " << distance << " feet through water.\n";
    } else if (choice == 'c') {
        cout << "A sound wave takes " << fixed << setprecision(4) << time
             << " seconds to travel " << distance << " feet through steel.\n";
    }

    return 0;
}```
Fixed this code by adding cin.ignore();
#
#include <iomanip>
using namespace std;

int main() {
    // Set speeds
    const double airSpeed = 1100;
    const double waterSpeed = 4900;
    const double steelSpeed = 16400;

    char choice;
    double distance, time;

    // Display menu of materials
    cout << "Select a medium:\n"
         << "a) Air\n"
         << "b) Water\n"
         << "c) Steel\n"
         << "Enter your choice (a/b/c): ";
    cin >> choice;

    // Validate user input
    while (choice != 'a' && choice != 'b' && choice != 'c') {
        cout << "Invalid choice. Please enter a, b, or c: ";
        cin.ignore();
        cin >> choice;
    }

    // Get distance from the user
    cout << "Enter the distance a sound wave will travel in the selected medium (in feet): ";
    cin >> distance;

    // Validate distance
    while (distance < 0) {
        cout << "Distance cannot be less than 0. Please enter a valid distance: ";
        cin >> distance;
    }

    // Calculate time
    time = distance / airSpeed;

    // Change output depending on material
    if (choice == 'a') {
        cout << "A sound wave takes " << fixed << setprecision(4) << time
             << " seconds to travel " << distance << " feet through air.\n";
    } else if (choice == 'b') {
        cout << "A sound wave takes " << fixed << setprecision(4) << time
             << " seconds to travel " << distance << " feet through water.\n";
    } else if (choice == 'c') {
        cout << "A sound wave takes " << fixed << setprecision(4) << time
             << " seconds to travel " << distance << " feet through steel.\n";
    }

    return 0;
}```
#

!solved