code to sound

Pages: 12
...
Last edited on
...isn't that a little bit harsh? The code isn't anything awesome and it could have been written much better (i.e. no system() ), but does that warrant a statement that the person who posted it should feel bad? :/

This comment was in reference to a comment just above this one made by helios, which has since been deleted.

-Albatross
Last edited on
helios wrote:
Your code is bad and you should feel bad.

It's a sign of bad childhood. That's probably what his father would
tell him whenever helios attempted to show off his coding abilities.

Disch (below) refers to:
http://www.youtube.com/watch?hl=en&v=jG2KMkQLZmI
Last edited on
am I the only one that got the Futurama reference?
On topic, there are many things you can do to improve it -> http://pastebin.com/fc04LdFD

Try to make it able to load a frequency stream from a file and then play it.

Useful link -> http://en.wikipedia.org/wiki/Note#Note_frequency_.28hertz.29

PS: Can you complete the song?
Last edited on
do you need to freking mock me helios?!?!?!?!
besids whats wrong with it enyway?
Last edited on
ok i cold down a little bit. so how would i get it to read a file enyway?
do you need to freking mok me helios?!?!?!?!


myte hv sumthin 2 do with tha way u tipe

besids whats wrong with it enyway?


1) it uses system
2) 'cls' doesn't fit in a single char
3) no way to gracefully exit, program just loops forever.
4) it uses beep (although I suppose this isn't "bad")
then how do you clear the screan with out useing the cls command?!?!?
oh then giv me an idea on how to exit the program!!!!
Did you know that i'm not in college like the rest of you?
Well... technically, you're not supposed to clear the console, as it's supposed to be more of a log of what happens in a program with some basic input capabilities as well. However, Duoas did write an article on the topic some time ago:
http://www.cplusplus.com/forum/articles/10515/

Happy programming.

-Albatross

P.S: There are some college graduates and experienced high-school students here. Just letting you know. :)
1
2
3
4
int main() {
  ...
  return 0; //exits the program (with the standard successful exit code)
}


You don't need to clear the screen. Moreover the C++ program doesn't know it's running on a screen, it only sees an output stream, so it doesn't make sense that it would have a built-in way to do this. “Clearing the screen” is a UI feature.
...
closed account (zb0S216C)
Clearing the screen isn't necessary. A call to system( ) isn't bad for your program, but it will mean your program will become platform-Dependant. Again, Beep( ) is called from the Windows API, making it more platform-Dependant.

It would be nice to inform the user what the request is. For example:
1
2
3
4
5
6
7
8
9
10
int main( )
{
    cout << "This program will play a sound using your system speaker based on your input." << endl;
    cout << "Input: 1 - Frequency: 100, Duration: 100" << endl;
    cout << "Input: 2 - Frequency: 200, Duration: 100" << endl;
    // And so fourth....
    cout << "Code choice >> ";

    // ...
}
A default case might be a good thing to add too.
As it stands - the code will play a note for code numbers 1 - 9, but enter a letter and you get nada. Adding below your case 'cls' ('cls' needs to be changed to a single character too, like 'x'):

1
2
3
4
default:
cout << "Input Error - Please enter an integral number from 1 to 9." << endl;
//No break needed - final clause of switch statement
} // close switch statement 


Will let the user know the input was not valid, and tell them what is valid.
adding on to Framework for a way to 'gracefully exit'

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main( )
{
    cout << "This program will play a sound using your system speaker based on your input." << endl;
    cout << "Input: 1 - Frequency: 100, Duration: 100" << endl;
    cout << "Input: 2 - Frequency: 200, Duration: 100" << endl;
    // And so fourth....
    cout << "Input: -1 - Exits the program" << endl;
    cout << "Code choice >> ";

    //before for loop
    bool exit = false;

    // in your switch statement
    case -1: exit = true;
                 break;

    //after your switch statement, still inside the for loop
    if(exit)
        break;
}


EDIT:
For the record, I'm a sophomore in high-school.
Last edited on
closed account (zb0S216C)
Thumper, -1 is two characters. I suppose x would be suitable, as NoteToSelf said. And, to control characters, I suppose a simple if statement would be enough. For example:
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
int main( )
{
    char Input;
    cout << "This program will play a sound using your system speaker based on your input." << endl;
    cout << "Input: 1 - Frequency: 100, Duration: 100" << endl;
    cout << "Input: 2 - Frequency: 200, Duration: 100" << endl;
    // And so fourth....
    cout << "Input: -1 - Exits the program" << endl;
    cout << "Code choice >> ";

    cin >> Input;

    if( ( Input >= '1' ) && ( Input <= '9' ) )
    {
        switch( Input )
        {
            // Cases...
        }
    }
    else // Anything that isn't between '1' and '9'...
         // Do exit code here...


    return 0;
}
you know I wrote the program the way I would write it. If you think its bad then copy it
and chang it the way you would make it!!!
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

//beep.cpp

#include <iostream>
#include <cstdlib>
#include <string>
#include <windows.h>

using namespace std;

int ask(istream &in, string msg = "") {
	string str;
	cout << msg;
	getline(in, str);
	int n = atoi( str.c_str() );
	if( n <= 0 )
		throw str;
	return n;
}

int main() {
	try {
		int feq = ask(cin, "Enter the tone's frequency (in hz): "),
			dur = ask(cin, "Enter the tone's duration (in ms): ");
		Beep(feq, dur);
	} catch(string badInput) {
		cerr << "Bad input \"" << badInput << "\"\nTerminating...\n";
		return 1;
	}
	return 0;
}
Last edited on
Pages: 12