0

I am making a game and I have to add some sounds effects and Music.

I Googled it and I found The flowing Code:

#include <conio.h>
#include "inc/fmod.h"

FSOUND_SAMPLE* handle;

int main ()
{
   // init FMOD sound system
   FSOUND_Init (44100, 32, 0);

   // load and play mp3
   handle=FSOUND_Sample_Load (0,"my.mp3",0, 0, 0);
   FSOUND_PlaySound (0,handle);

   // wait until the users hits a key to end the app
   while (!_kbhit())
   {
   }

   // clean up
   FSOUND_Sample_Free (handle);
   FSOUND_Close();
}

But when I compile it I got the flowing error:

➜  Desktop gcc main.c
main.c:1:10: fatal error: 'conio.h' file not found
#include <conio.h>
         ^~~~~~~~~
1 error generated.
DarkSide77
  • 719
  • 1
  • 4
  • 21
  • conio.h is not available with gcc, use `curses.h` instead https://stackoverflow.com/questions/8792317/where-is-the-conio-h-header-file-on-linux-why-cant-i-find-conio-h – susenj Oct 26 '20 at 12:44
  • `_kbhit()` is a MS function too. Possible duplicate; [How do I port this program from conio to curses?](https://stackoverflow.com/questions/12247674/how-do-i-port-this-program-from-conio-to-curses) – Weather Vane Oct 26 '20 at 13:00
  • 1
    It's operating-system-specific unfortunately. – user253751 Oct 26 '20 at 13:26
  • The most basic way I can think of: `putchar('\a'); /* and maybe */fflush(stdout);` – pmg Oct 26 '20 at 14:25
  • Apparently this code is many, many years old. Depending on your target system (that is which one, by the way?) you need to research other ways, like playing a WAV or MP3. – the busybee Oct 26 '20 at 16:16

1 Answers1

-2

Well, firstly <conio.h> is a C++ library and you're programming in C. It's different! Then, I remember a C code I wrote years ago, main.c has got the following code (comments are in italian because I am italian):

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include "header.h"
int main(){
register unsigned char x='2';
printf("digitare tasti:\n");
while(1){
while(1){
if(x=='2'){/*blocco2*/ while(x!='1' && x!='3'){x=getch(); scala2(x);}}
if(x=='1'){/*blocco1*/ while(x!='2' && x!='3'){x=getch(); scala1(x);}}
if(x=='3'){/*blocco3*/ while(x!='1' && x!='2'){x=getch(); scala3(x);}}
}
}
system("PAUSE");
return 0;
}

Then, this is the other source file, called file.c:

 #include <stdio.h>
 #include <stdlib.h>
 #include <windows.h>
 #include "header.h"
 void scala1(unsigned char x){
 if(x=='a')beep(131,50);
 if(x=='s')beep(147,50);
 if(x=='d')beep(165,50);
 if(x=='f')beep(175,50);
 if(x=='g')beep(196,50);
 if(x=='h')beep(220,50);
 if(x=='j')beep(247,50);
 if(x=='k')beep(262,50);
 if(x=='l')beep(294,50);
 if(x=='w')beep(139,50);
 if(x=='e')beep(156,50);
 if(x=='r')beep(185,50);
 if(x=='t')beep(208,50);
 if(x=='y')beep(233,50);    
 }
 void scala2(unsigned char x){
 if(x=='a')beep(262,50);
 if(x=='s')beep(294,50);
 if(x=='d')beep(330,50);
 if(x=='f')beep(349,50);
 if(x=='g')beep(392,50);
 if(x=='h')beep(440,50);
 if(x=='j')beep(494,50);
 if(x=='k')beep(523,50);
 if(x=='l')beep(587,50);
 if(x=='w')beep(277,50);
 if(x=='e')beep(311,50);
 if(x=='r')beep(370,50);
 if(x=='t')beep(415,50);
 if(x=='y')beep(466,50); 
 }
 void scala3(unsigned char x){
 if(x=='a')beep(523,50);
 if(x=='s')beep(587,50);
 if(x=='d')beep(659,50);
 if(x=='f')beep(698,50);
 if(x=='g')beep(784,50);
 if(x=='h')beep(880,50);
 if(x=='j')beep(988,50);
 if(x=='k')beep(1046,50);
 if(x=='l')beep(1175,50);
 if(x=='w')beep(554,50);
 if(x=='e')beep(622,50);
 if(x=='r')beep(740,50);
 if(x=='t')beep(831,50);
 if(x=='y')beep(932,50);  
 }

The last one, the file header.h. It's code is the following one:

void scala1(unsigned char x);
void scala2(unsigned char x);
void scala3(unsigned char x);

All the source files must be in the same directory. You compile main.c and then, you just need to press a,s,d,..y and 1,2,3. Try! It works, of course if you want to change part of the code, you can do. I hope you enjoy my program, it's funny :)

  • 3
    `firstly is a C++ library` No, it is not. – KamilCuk Oct 26 '20 at 14:14
  • Right, but usually when you create a C code you add these libraries: ** and ** – Giulio Marchesi Oct 26 '20 at 14:18
  • 2
    That's... unrelated? `conio.h` is a DOS library for interfacing with the terminal (CONsole Input/Output). It's not portable, was used only in DOS and early windows. `stdio.h` and `stdlib.h` are [very standard](https://port70.net/~nsz/c/c11/n1570.html#7.1.2p2) part of C standard library. So... yes usually I do add `stdio.h` and `stdlib.h` if I write C code that does some I/O and allocates memory. – KamilCuk Oct 26 '20 at 14:23
  • Would you mind to format your code, please? -- Do you think this works on present systems? You don't know which one the OP uses. – the busybee Oct 26 '20 at 16:14
  • Well, I used it on windows 7 and windows 10. With other OS you have to change the function system(). The other code should work without any problem – Giulio Marchesi Oct 26 '20 at 16:35