Im new to C++ but we have been doing a module on it at uni. We had to create a simple reverb time calculator, to anyone not familiar it just works out different note durrations ie 1/4 or 1/16 notes at a different tempos in milliseconds.
The problem is that when the user enters a BPM value which is not a number it causes the program to loop infinately, i know its due to using a double data type to store the BPM but just wondered if there is a way around it or alternative.
Any help would be great, our lecturer said it wasnt vital that this was corrected but would gain marks by resolving it.
//---------------< Simple compatible delay time calculator >--------------------
#include <iostream>
#include <iomanip>
#include <cstdlib>
usingnamespace std;
//-------------------< Declare variables and functions >------------------------
double rt4; //Return value for 1/4 note time duration
double bpm; // Beats per minute
intconst mseconds = 60000; // ms in a minute
char repeatValidation; // Choice for repeating application
char quit;
// Function to calculate time duration for quater notes
double rtCalc(double beatPM){
double r; // Value to return
r = (mseconds/beatPM);
return (r);
}
//-------------------------< Main Program Function >-----------------------------
int main(){
do{
for(;;){
char userInput[20]; // string from <string> highly recommended but i am not sure if you are allowed to use it.
cout << "Please enter your tempo for a list of suitable delay times: (between 1 and 500 bpm)\n\n\tTempo = ";
cin.sync(); // discards any trash characters in the stream
cin >> setw(20) >> userInput; // setw(20)makes sure cin only reads 20 characters and
//reserves the last one for '\0' (null). Not necessary with string from <string>.
bpm = atof(userInput);
if (bpm >= 1 && bpm <= 500){
//you should inform the user what was his input. just a suggestion though
cout << "Ok the results for: "<< bpm << " is: " << endl;
rt4 = rtCalc(bpm);
double rt2 = rt4*2; //Return value for 1/2 note time duration
double rt8 = rt4/2; //Return value for 1/8 note time duration
double rt16 = rt4/4; //Return value for 1/16 note time duration
double rt32 = rt4/8; //Return value for 1/32 note time duration
// results from calculations displayed formatted:
cout << "\n\nNote Duration\t\tTime in ms";
cout << setprecision(2);//sets precision of output to 2 dp
cout << fixed;// sets results to display with fixed point
cout << "\n1/2 note\t\t" << rt2 << endl;
cout << "Dotted 1/2 note\t\t" << rt2*1.5 << endl;
cout << "\n\n1/4 note\t\t" << rt4 << endl;
cout << "Dotted 1/4 note\t\t" << rt4*1.5 << endl;
cout << "\n1/8 note\t\t" << rt8 << endl;
cout << "Dotted 1/8 note\t\t" << rt8*1.5 << endl;
cout << "\n1/16 note\t\t" << rt16 << endl;
cout << "Dotted 1/4 note\t\t" << rt16*1.5 << endl;
cout << "\n1/32 note\t\t"<< rt32 << endl;
cout << "Dotted 1/32 note\t" << rt32*1.5 << endl;
break;
}
else{
cout << "\nThat was not a valid tempo.\n\n";
}
}
for(;;){
cout << "\n\n\nDo you wish to repeat? (y for yes or n for no): ";
cin >> repeatValidation;
if (repeatValidation == 'y'||repeatValidation == 'Y'){
cout << "\n\n\n";// loops back to start
break;
}
elseif (repeatValidation == 'n'||repeatValidation == 'N'){
quit = 'y';// Quits for loop and changes the value of the quit var to y to terminate the app.
break;
}
else{
cout << "\n\nThat was not a valid reply.";
}
}
}while(quit != 'y');
}