Delay time calculator input problems

Mar 12, 2008 at 8:23pm
Hi,

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.

Cheers
Last edited on Mar 14, 2008 at 7:45am
Mar 12, 2008 at 9:29pm
Hi,
Use a temporary string variable to store the user input and atof to convert it back to double:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//---------------< Simple compatible delay time calculator >--------------------
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;

//-------------------< Declare variables and functions >------------------------

double rt4; //Return value for 1/4 note time duration
double bpm; // Beats per minute
int const 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;
			}
			else if (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');
}

dont forget to
 
#include <cstdlib> 


Note: atof returns 0 if it fails to convert but since your program does not accept 0 it will output
 
cout << "\nThat was not a valid tempo.\n\n";


http://www.cplusplus.com/reference/clibrary/cstdlib/atof.html

Jeff
Last edited on Mar 12, 2008 at 10:38pm
Mar 12, 2008 at 10:25pm
Great,

Thats so helpful, im not familiar with the cstdlib but ill have a look into it.

I assumed that the answer would be with strings however I was not sure where to start.

Oh and we can use the #include <string> so if i add that do i not have to worry about the setw(256) bit?

Thanks again,

Last edited on Mar 14, 2008 at 7:45am
Mar 12, 2008 at 11:08pm
Sorry to be a pain but how would you write it using the <string>?

Ive tried what I thought it would be but gives the errors:

error C2664: 'atof' : cannot convert parameter 1 from 'std::string' to 'const char *'

Cheers
Last edited on Mar 12, 2008 at 11:17pm
Mar 13, 2008 at 12:07am
Oh it's not a problem i am stuck in my office waiting for a response from a client so all i am doing is surfing the internet and helping people here.

If userInput was of string type:
 
bmp = atof(userInput.c_str());


Yeah like i said in the comments it is not necessary with string class, so just delete that.

http://www.cplusplus.com/reference/string/string/c_str.html
http://www.cplusplus.com/reference/string/string/


Jeff
Last edited on Mar 13, 2008 at 12:16am
Mar 14, 2008 at 7:37am
thanks for the advice, ive managed to find my own way round it using your info! thanks again.
Topic archived. No new replies allowed.