...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.
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")
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. :)
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.
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.
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.
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: