DreadedEntity
DreadedEntity

Reputation: 173

How can I get the pound(or hash) symbol in a string in C?

I'm writing a program so I can practice/memorize the notes on each string of my guitar. The way I see it, I can have some practice with writing a practical program (let's be honest, my "labs" aren't all that challenging), and when it's done I'll have a useful tool to help me memorize notes on my guitar. I searched google for a while before coming here and apparently I'm the only person that has ever had this problem.

Anyway, I'm using this to define all of the notes in western music:

char *notes[12] = {"A","A#","B","C","C#","D","D#","E","F","F#","G","G#"};

The problem is that when I list all of the elements, the pounds(or hashes) do not show up. As music does contain accidental notes, I need to have these in my program. So how can I fix this?

I don't really know if this matters, but I'm sending the array into another function by reference:

int notePosition(char *notes[], char *strings[], int *string)
{
    int i;
    for (i = 0; i <= 11; i++)
    {
        printf("%c", *notes[i]);
    };
    return 0;
};

Sample output: AABCCDDEFFGG

To be honest, I'm still not completely sure how to use pointers, and I'm shaky at-best with pass-by-reference, but this seems to work out. I just don't know why those symbols aren't in my strings.

Upvotes: 1

Views: 3231

Answers (4)

DreadedEntity
DreadedEntity

Reputation: 173

Thanks for everyone's help answering my original question. That was a dumb mistake, I thought there was a problem somewhere else and I didn't think about other things that could cause issues. Anyway, I finished the program so I wanted to share it, maybe other people would like to use it.

I'm also wondering if anybody sees anything that I could do to improve my programs in the future. I know that scanf is bad and can be used to cause an overflow, but I don't really get fgets, there was some strange behavior when a user entered more characters than the input array was defined to have, so I just used scanf instead. Until I figure out a solution for that, just keep your input at 9 characters (for this program you will only need a maximum of two characters for your answers, anyway).

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>

/*
Author: DreadedEntity
Date: 4/8/15
*/

int calculateNote(char *notes[], char *string[], int fret);
int notePosition(char *notes[], char *string[]);

int main()
{
    srand(time(NULL));

    char input[10];

    int playerPoints = 0;
    int compPoints = 0;

    int string;
    int fret;

    int round = 1;

    int answer;

    char *notes[12] = {"A","A#","B","C","C#","D","D#","E","F","F#","G","G#"};
    char *strings[6] = {"E","B","G","D","A","E"};

    printf("Let's play a game!\n\nI will give you a string and a fret on a guitar and you tell me what note it is!\nIf you answer correctly, you get a point, but if you answer incorrectly I get a point.\n\nWhoever has the most points after 20 rounds wins!\n");
    system("pause");

    do
    {
        string = (rand() % 6) + 1;
        fret = (rand() % 24);

        printf("%d. Tell me what the note is on string %d, fret %d: ", round, string, fret);
        //printf("\n%s\n", strings[string-1]);

        answer = calculateNote(notes, strings[string-1], fret);

        //printf("%s", notes[answer]); //take the comment away from this line to get all the answers

        scanf("%s", &input);

        if (strcmp(input, notes[answer]) == 0)
        {
            printf("Wow, you got that one! One point for you!\n");
            playerPoints++;
        } else
        {
            printf("Sorry, that's not right! One point for me!\n");
            compPoints++;
        };

        round++;
    } while (round <= 20);

    printf("\nThe game is over! Let's take a look at our points!\n\nYou: %d\nMe:  %d\n\n", playerPoints, compPoints);

    system("pause");

    if (playerPoints > compPoints)
    {
        printf("\nYou beat me! I'll get you next time! ");
    } else
    {
        if (playerPoints == compPoints)
        {
            printf("\nWow a tie, great job! ");
        } else
        {
            printf("\nBetter luck next time! ");
        };
    };

    system("pause");

    return 0;
}

//get position of the actual note on the fret for "notes" array
int calculateNote(char *notes[], char *string[], int fret)
{
    int position = notePosition(notes, string);

    position += fret;

    while (position >= 12)
    {
        position -= 12;
    };

    return position;
};

//get position of the note of the input string(unfretted) in "notes" array
int notePosition(char *notes[], char *string[])
{
    int i = 0;

    while (strcmp(notes[i], string) != 0)
    {
        i++;
    };

    return i;
};

Upvotes: 1

paxdiablo
paxdiablo

Reputation: 881643

The statement

printf("%c", *notes[i]);

will give you a character, located at the address specified by notes[i]. In other words, the first character of the string.

If you want the entire string, you should be using:

printf ("%s", notes[i]);

You can see the difference in the following program:

#include <stdio.h>
int main (void) {
    char *notes[] = {
        "A", "A#", "B", "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#"
    };
    for (int i = 0; i < sizeof(notes) / sizeof(*notes); i++)
        printf("bad = %c, good = %s\n", *notes[i], notes[i]);
    return 0;
}

which outputs:

bad = A, good = A
bad = A, good = A#
bad = B, good = B
bad = C, good = C
bad = C, good = C#
bad = D, good = D
bad = D, good = D#
bad = E, good = E
bad = F, good = F
bad = F, good = F#
bad = G, good = G
bad = G, good = G#

Upvotes: 0

Rahul Tripathi
Rahul Tripathi

Reputation: 172458

You need to use %s format specifier instead of %c. %c is used for character literal and %s is used for string literal. So try this:

printf("%s", notes[i]);

Upvotes: 1

PaulProgrammer
PaulProgrammer

Reputation: 17630

%c is for a single character. %s is what you want, which will output the entire string. When you change to %s you will no longer need to dereference the notes[] string element:

printf("%s", notes[i]);

Upvotes: 2

Related Questions