Guitar fretboard program

I have been trying to learn c++ for a few months now and all i have been really doing is reading and watching tutorials and not actually doing any real thinking and coding. I have finally decided to just start coding and put my knowledge of c++ to practical use. I decided to make a fretboard program that asks where the guitar notes are located. Here is the code
[code]

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>



using namespace std;

void fretboardTest();

int main()
{

char start = 's';

cout << "This program will help you learn the guitar fretboard.\n";
cout << "Enter s to start!";
cin >> start;
while (start == 's')
{

fretboardTest();

}




system("pause");
return 0;
}
void fretboardTest()
{

srand(time(0));

int note;
char guitarStrings[5] = { 'e', 'a', 'd', 'g', 'b' }; //only 5 strings since top and bottom strings are both e
char guitarNotes[7] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g' };

int guitarStringArray = rand() % 5;
int guitarNotesArray = rand() % 7;



if (guitarStringArray == 0)//test for e string
{
if (guitarNotesArray == 0)//test a note
{
cout << "Where is the a note on the e string?";
cin >> note;
if (note == 5)
cout << "That is correct!";
}
else if (guitarNotesArray == 1)// test b note
{
cout << "Where is the b note on the e string?";
cin >> note;
if (note == 7)
cout << "That is correct!";



}
}





} Im just going to keep writing if else statements until i have all the notes tested in that particular string. Then i move on and repeat. Is there a better way of writing out this part rather then using over 500 lines of if else?
Here's my idea, feel free to ask questions:

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
#include <iostream>
#include <string>
#include <ctime>

int main(int argc, char* argv[]) {

	srand(time(0));

	const unsigned short num_notes =	12;
	const unsigned short num_strings =	5;

	std::string note_names[num_notes] =		{ "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"};
	unsigned short string_names[num_strings] =	{4, 9, 2, 7, 11};

	unsigned short response = 0;

	while (true) {                        
		system("cls");//cough

		unsigned short note = rand() % num_notes;
		unsigned short string = rand() % num_strings;

		std::cout << "Where is the \'" << note_names[note] << "\' note on the \'" << note_names[string_names[string]] << "\' string?" << std::endl;
		std::cin >> response;
		if ((string_names[string] + response) % num_notes == note) {
			std::cout << "Good job!" << std::endl;
		}
		else {
			std::cout << "Wrong!" << std::endl;
		}
		std::cin.ignore();
		std::cin.get();
	}
	return 0;
}
Last edited on
Oh wow, such a simple way. The way i was going to write it was to keep using if statements but this is way better. Dang i need more experience. My only question is how did you figure out the formula to find where the correct note is on the fretboard? In other words how does this piece of code work?

if ((string_names[string] + response) % num_notes == note)
cant seem to make sense of it
Last edited on
Problem solving and finding algorithms that work comes with experience.
As for how this code works:

unsigned short string_names[num_strings] is an array of unsigned shorts, whose elements will be used as indices to elements of the std::string note_names[num_notes] array.

To be more specific, I could write:

std::cout << note_names[string_names[0]] << std::endl;

And it would print the fourth element of note_names, which is "E". In this way we can represent string names without having to rewrite the letters, but there's a more practical use for this as you will see.

As you correctly pointed out, line 25 is the meat of the algorithm.

To walk you through it, let's take an example - let's assume the following:

1
2
3
4
5
note = 0
//note is "C"

string = 3;
//string is "G" 


So we have:

if((string_names[string] + response) % num_notes == note)

Which, in our example, can be rewritten as:

if((string_names[3] + response) % 12 == 0)

Which can further be reduced to:

if((7 + response) % 12 ==0)

Let's say the user's response was '2':
(In other words, the user is saying "C is two half-steps above G".)

if((7 + 2) % 12 == 0)

if((9) % 12 == 0)

9 modulo 12 is nine, it's not zero. Therefore, the user's response is incorrect.

If the user had entered '5' instead of '2':
(In other words "C is five half-steps above G")

if((7 + 5) % 12 == 0)

if((12) % 12 ==0)

The user would have been correct, because 12 modulo 12 is zero.
I'll probably edit this post once I can get myself to write a more mathematical explanation.
Last edited on
Topic archived. No new replies allowed.