Jump to content

Help with program in C.


Ointhedwarf

Recommended Posts

First I must say that I'm quite a newbie in C programming.I've made the following program for practice with note reading: http://www.dpaste.org/3V2fc/

 

As you can see the idea is:

1) to produce a random note on a staff. Simplest way is making it visual and shaping it by using lots of these - and O for the note. The randomness is achieved by placing a number to each staff (i.e. note), and running a random function covering each possible note/number. At the same time in each staff the note determines the value of the variable 'truenote'.

2) to ask the user to find the note. The user can type in the notes A,B,.., G and for each note a number is given to a variable 'note'.

3) to verify the answer. The program compares the value of the variable 'note' with the value of the variable 'truenote'. If the answer is correct the whole sequence is repeated from start. If its wrong only the user-part of the program is repeated.

4)When the user presses 's' the program prints the number of tries and errors and then ends.

 

There are a number of issues with this program which I can't solve. The first has to do with the program as it is and I believe its a logical problem, if you try the program out you'll find that after the second try the previous value of the 'note' variable runs the if function and a standard message is printed in the start of each try. This affects the value of the errors variable making it practically useless. The solutions of this program is, either finding the logical problem :P, or un-assigning the value of 'note' in the end of the loop. Both I dont know how to do :P.

The second optional problem is that I dont know how to allow answers like do re mi fa sol la si.. I know its because of the char variable 'NOTE' but I dont know how to use arrays in such a fashion. Thanks in advance!

 

PS. Scroll all the way down to pass the printing in the beginning ;)

Edited by Ointhedwarf
Link to comment
Share on other sites

I don't understand your first problem. What does "the previous value of the 'note' variable runs the if function and a standard message is printed in the start of each try" mean? For the 2nd problem, you want to use a different data type to read the user input, namely a string or a "char*" rather than a char. However, my suggestion would be to use a C++ compiler and C++ data structures for your task (all of your code already compiles with a C++ compiler, so there is no need to start from scratch).

 

In that case, your code would become something like

// c++ includes and a "using" command you shouldn't bother about for now.
#include <iostream>
#include <string>
using namespace std;

...
       do {
          // ask user for a guess.
          cout<<"\nFind the note (A to G): ";
          string guess; // "string" is the c++ datatype for texts. Oh, and I renamed it from "NOTE" to something that seemed more sensible to me.
          cin>>guess; // this reads a line from the input into the variable "guess".
          // note that you can compare to more than a single character. Also note that '' has been replaced with ""
          // also note the extra "|| guess=="do"" (or "mi", or "fa", or whatever would be appropriate).
           if (guess == "A" || guess == "a" || guess=="do") 
               note = 6;
           else if (guess == "B" || guess == "b")
           ...
           else if (guess == "S" || guess == "s") {
               truetimes = tries - 1;
               cout<<"\nYou tried "<<truetimes<<" times"  // I replaced the C output with C++ output. Strictly speaking there is, however, no need to do so.
                      <<"You made "<<errors<<" errors"<<endl;
               system("pause");
               return 0;
           } else {
              cout<<"cannot make sense of guess "<<guess<<". Please repeat guess."<<endl;
              continue;
           }
           if (note!=truenote) {
               cout<<"Try again!"<<endl;
               errors += 1;
           }
       } while (note!=truenote); // keep guessing until the guess was correct.

 

It is of course not necessary to use C++ code, your issue can be solved in C, too. I would, however, strongly recommend using a C++ compiler and C++ datatypes for C programming. It offers a lot of nice extras "for free" without forcing you to actually program C++ (e.g. you can still ignore object orientation or arbitrarily many aspects of the C++ standard library).

Link to comment
Share on other sites

I see, by using C++ it becomes quite simple. I actually understand the format without any knowledge on C++ programming. I was thinking of switching to C++ before I get to deep though I'm still curious about how you would tackle this problem in C. Thanks a lot!

 

My first problem will be evident if you try it out. For some reason after the first try a problem occurs, I will not try to explain it but I'll give you an example.

-Program starts.

-Prints a staff. (lets say its a C)

-The user types in a, he is asked to try again like this

Find the note (A to G): Try again
Find the note (A to G): | (<- blinking)

So the program ran the if function and started again from the second do command, no problem here.

 

-The user types in c, the program prints a new staff

Now if the note of the new staff is something other than C for some reason the if function is ran prematurely an instead of this

Find the note (A to G): | (<- blinking)

you see this again

 

Find the note (A to G): Try again
Find the note (A to G): | (<- blinking)

 

My guess is that the value of the 'note' variable (or as you aptly changed it 'guess' variable) is considered different from the 'truenote' variable and the if function runs prematurely. Try it out and you'll see

 

 

PS The parts I've coded are supposed to be the console

Edited by Ointhedwarf
Link to comment
Share on other sites

Okay, I get your explanation now. I am not sure if that problem may specific to Windows, and at the moment I cannot look into it in detail (partly because I don't use Windows). However, my guess is that if you enter the answer "a" and hit enter, scanf will read "a", and then the next time it is called will read "\n". Try the following fix (as a blind guess; I am not a C programmer):

scanf ("%c",&NOTE);  /* old line you had in your code */
scanf ("%c",&A); /* add this new line exactly after the one above, to capture the "\n" character */

It's just a blind guess of mine, but try it out. Note that it's a nasty hack. Also note, that I am abusing the fact that you declared variables A,B,.. that are not used in the program. For proper code, define a proper variable. To make one more point for c++ related to your unused variables: Contrary to C, C++ allows to define variables where you need them, whereas in C you need to define them at the beginning of the function. So in C++ you could write

char guess, cr_catch;
scanf ("%c",&guess);  
scanf ("%c",&cr_catch);

and have the definition of the variables and their usage at one place in the code (the definition at the beginning of the function could be removed in this case).

Edited by timo
Link to comment
Share on other sites

SPOT ON!! Ty man great job. But this makes me feel uncertain about things I thought I understood, I'll have to read some chapters again and do some experimenting.

 

Now if you don't mind I'd like to take this program to the next level and by this I mean adding sound. So I'd probably need a function that makes a sound when given the frequency, or simply a function that imports files to the program. This would be implemented to the program by giving to each function the random number and using a similar sequence. Now I have no idea how to do this, I've found some reference about a beep function but I don't know if my compiler supports it or if I have the correct library...

Edited by Ointhedwarf
Link to comment
Share on other sites

Being a scientist my knowledge about producing sound is naturally non-extensive - especially in a programming language I never used myself. Generally, for questions like "how do I do X in programming language Y", Google is an excellent source of information. For example, the very first google hit I got was http://www.daniweb.com/software-development/c/threads/57693/how-do-you-make-the-computer-speaker-beep-in-c , which looks promising for a start. Good luck!

Link to comment
Share on other sites

Success again! Great, I managed to make a similar program but with sounds and now I can focus on more structural problems. Thanks again timo!

 

This is the second program for anyone who cares

 

 


/*PROGRAM : Note recognition*/
#include <stdio.h>
#include <windows.h>
#include <time.h>
#include <stdlib.h>
int main()
{
   int r, value;
   char guess, A;
   printf("This program will test your ability to recognize notes by sound");
   do
   {
   srand ( time(NULL) );
   r = rand() % 7 + 1;
   if (r == 1)
        Beep(226,1000);
   else if (r == 2)
        Beep(293,1000);
   else if (r == 3)
        Beep(329,1000);
   else if (r == 4)
        Beep(349,1000);
   else if (r == 5)
        Beep(391,1000);
   else if (r == 6)
        Beep(440,1000);
   else if (r == 7)
        Beep(493,1000);
   do
   {
   printf ("\nFind the guess (A to G): ");
   scanf ("%c",&guess);
   scanf ("%c",&A);
   if (guess == 'A' || guess == 'a')
      value = 6;
   else if (guess == 'B' || guess == 'b')
      value = 7;
   else if (guess == 'C' || guess == 'c')
      value = 1;
   else if (guess == 'D' || guess == 'd')
      value = 2;
   else if (guess == 'E' || guess == 'e')
      value = 3;
   else if (guess == 'F' || guess == 'f')
      value = 4;
   else if (guess == 'G' || guess == 'g')
      value = 5;
   else if (guess == 'S' || guess == 's')
   {
      system("pause");
      return 0;
   }     
   if (guess!=r)
      printf("Try again");
   } while ( value != r);
   puts ("\n\n\nCongratulations!!\n\n\n\n");
   printf("\n Lets try again \n\n");
 } while (guess != 'S');
 system("pause");
}

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.