so i was wondering if my program could be converted to a string version, if so gimme a start please. I also would like to find out how to set my speed variable if when entered it is less than zero for it to display an error statement, how would I do this in switch and or string. thanks so much.

// This program will calculate the speed of sound
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int choice; //to hold a menu choice 
    int speed; //to hold the speed
    double charges; // to hold the monthly charges
     //const for air
    const double AIR = 1100.0,
                 WATER = 4900.0,
                 STEEL = 16400.0;
    //constants for menu choice
    const int air_choice = 1,
              water_choice = 2,
              steel_choice = 3,
              quit_choice = 4;

    //display menu and get a choice
    cout << "\t\tSelect one of the folowing from the menu\n\n"
         << "1. air\n"
         << "2. water\n"
         << "3. steel\n"
         << "4. quit the program\n\n"
         << "Enter your choice:";
    cin >> choice;
    // set the numeric formating.
    cout << fixed << showpoint << setprecision(4);

    // respond to the users menu choice
    switch (choice)
    {
       case air_choice:
         cout << " Enter the speed of the sound in air please:";
         cin >> speed;
         charges = AIR / speed;
         cout << "\t\tthe total speed of sound in Air is:" << charges <<"Htz"<< endl;
         break;

      case water_choice:    
         cout << " Enter the speed of the sound in water please:";
         cin >> speed;
         charges = WATER / speed;
         cout << "\t\tthe total speed of sound in water is:" << charges <<"Htz"<< endl;

      case steel_choice:    
         cout << " Enter the speed of the sound in steel please:";
         cin >> speed;

         charges = STEEL / speed;
         cout << "\t\tthe total speed of sound in steel is:" << charges <<"Htz"<< endl;
         break;

       case quit_choice:    
         cout << " This program will self destruct now!:";
         break;

       default:
          cout << " That is not a valid choice now i have to eat your soul!! Run the \n"
               << "program again and select one through four please!\n";


}

    system("PAUSE");
    return 0;
}

Recommended Answers

All 3 Replies

Well, for starters. What do you mean with turning your program into a string?

As for the negative-number check. You could either consider doing a Math.abs on the input for sanitazion, or create an if-statement around the cin, to check if the number provided is higher than 0.

well since i have been taught to do the if statement thats what i will do if thats my only option. i will look into Math.abs thopught
thank you

You could try something like this:

#include "stdafx.h"
#include <cstdlib>
#include <iostream>
#include <iomanip>

using namespace std;
    int ValidateInput(string Message);
    int main()
    {
        int choice = 0; //to hold a menu choice
        double charges = 0; // to hold the monthly charges
        //const for air
        const double AIR = 1100.0,
                     WATER = 4900.0,
                     STEEL = 16400.0;
        //constants for menu choice
        const int air_choice = 1,
                  water_choice = 2,
                  steel_choice = 3,
                  quit_choice = 4;
        //display menu and get a choice
        while(choice != quit_choice)
        {
            System::Console::Clear();
            cout << "\t\tSelect one of the folowing from the menu\n\n"
             << "1. air\n"
             << "2. water\n"
             << "3. steel\n"
             << "4. quit the program\n\n"
             << "Enter your choice:";
        cin >> choice;
        // set the numeric formating.
        cout << fixed << showpoint << setprecision(4);
        // respond to the users menu choice
        switch (choice)
        {
        case air_choice:
            charges = AIR / ValidateInput(" Enter the speed of the sound in air please: ");
            cout << "\t\tthe total speed of sound in Air is:" << charges <<"Htz"<< endl;
            break;
        case water_choice:
            charges = WATER / ValidateInput(" Enter the speed of the sound in water please: ");
            cout << "\t\tthe total speed of sound in water is:" << charges <<"Htz"<< endl;
            break;
        case steel_choice:
            charges = STEEL /  ValidateInput(" Enter the speed of the sound in steel please:");
            cout << "\t\tthe total speed of sound in steel is:" << charges <<"Htz"<< endl;
            break;
        case quit_choice:
            cout << " This program will self destruct now!:";
            break;
        default:
            cout << " That is not a valid choice now i have to eat your soul!! \n"
                 << "Select one through four please!\n";
        }
        system("PAUSE");
        }

        return 0;
    }
    int ValidateInput(string Message)
    {
        bool valid = false;
        int speed = 1;
        cout << Message.c_str() << endl;
        while(!valid)
        {
            cout << "Input cannot be less than 1: ";
            cin >> speed;
            if(speed >=1)
                valid = true;
        }
        return speed;
    }
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.