vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

not having a go, dont worry! just saying that if there is anything that cant be sorted then a few people have discussed the topic of going both ways (b2d, d2b) on a different thread. btw where is the pow function defined ? is it in <cmath>? would certainly simplify my own code version... :)

Yes, pow() is in cmath, but likes to see doubles for arguments, or at least pow(2.0, k--)

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

[IMG]http://img64.exs.cx/img64/2314/delete3zj.gif[/IMG]

That deletes selected code, not a project or file! Let's try it again ...

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

fair enough. Im used to Programming in VB (6 years and before i learned c++) where you can observe variables at run time (ie wats in the string). however my c++ IDE doesnt.... so i dont know what is going on i have to make certain nothing can go wrong :(
Do you use Dev-C++? I have 4.9.9.0 and if anyone knows how can i observe vaiables at run time??

Console debugging has problems with old Windows and Dev C++ versions. You should be okay with version 4.9.9.0
Here is the poop:
Debugging with Dev C++:
Make sure you are using a project!
Go to Project Options > Compiler > Linker and set Generate Debugging Information to yes. Make sure you are not using any optimization options or the strip option!!!
Now do a full rebuild with Ctrl-F11, then set breakpoints where you want the debugger to stop. To set a breakpoint on a line, put your cursor on the line and press Ctrl-F5 (or click on the gutter left of the line). You can add or remove breakpoints during the debug session, but need to have at least one to start with.
Launch the debugger clicking the debug button (or F8). The program stops at the first breakpoint. You can step through the code and enter function calls, by clicking on the "step into" button (or Shift-F7), or stepping over the function calls, by clicking the "next step" …

Dave Sinkula commented: Thank you very much for that! --Dave +1
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

There is another way to make a sound, quite simple, you load a wave file and play it:

// play a wave file sound using the winmm library
// For DevCpp add the library libwinmm.a to linker via 
// Project/Project Options/Parameters/Add Library
// remember this is a console program!

#include <iostream>
#include <windows.h>   // for PlaySound()

#define SND_FILENAME 0x20000
#define SND_LOOP 8
#define SND_ASYNC 1

using namespace std;

int main()
{
  // play sound as loop (remove SND_LOOP for one time play)
  // file boing.wav has be in the working directory
  PlaySound("boing.wav",NULL,SND_FILENAME|SND_LOOP|SND_ASYNC); 
  
  cin.get(); // wait
  return 0;
}
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

I'm using DevC++ from Bloodshed and my OS is XP. I looked at the libraries and have no idea which is sound :cry:

Dem

Okay, that helps. Now you can get beyond Beep() and use your soundchip. There is a little code snippet called "Play a MIDI voice (Dev C++ console program)" on DaniWeb at:

http://www.daniweb.com/code/snippet97.html

For those who need some hand holding with the Dev C++ IDE:
In the IDE go to FILE, then NEW, then Project, select (in this case) Console Application, give it a name like Sound1 then click OK. A filesave dialog box comes up, create a new folder, might as well call it Sound1, open it and save project file Sound1.dev there. The DevCpp IDE comes up with a bare bones template, select and delete that and cut and paste the snippet code into the empty editor page. Now compile and run. Don't forget to turn your speakers on.

Killer_Typo commented: it is posts like these that are a prime example of a good forum member. +5
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

clrscr() is a presumptuous Borland fetish. Not every user of your program wants it!!!

In Dev C++ you can use system("CLS") to do the trick, the old DOS command. Include the stdlib.h for the system() function.

By the way, Dev C++ is a very nice IDE for a set of open source GNU compilers (mainly GCC and G++). The IDE comes in Windows and Linux versions. For Linux CLS has to be replaced with something else.

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Jpowers22 is right, you need to use clock() to get to something that is about a millisecond. You also have to do the calculation about a million times to get a meaningful time to measure. Yes Ruby, computers are fast nowadays!

// time the sqrt() function    Dev-C++

#include <iostream>
#include <cmath>
#include <time.h>

using namespace std;

int main()
{
  int k;
  double a,x,y,z;
  clock_t start, end;
 
  z = 0.01; 
  y = 2;
  cout << "Enter the number you want to find the squareroot of : ";
  cin >> x;

  start = clock();
  for(k = 0; k < 1000000; k++)
    a = sqrt(x);
  end = clock();
  cout << "1M iterations of sqrt() from cmath took "; 
  cout << (end - start) << " ticks\n"; // a tick is about 1 ms 
 
  // now do a similar thing for sqroot(x,y,z)

  system("PAUSE");	
  return 0;
}
Asif_NSU commented: helped me too-- Asif_NSU +1
vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

double fmod(double x, double y) is a function, so you have to rewrite your code a little. It returns the remainder of the division x / y. Make sure to #include <math.h>

vegaseat 1,735 DaniWeb's Hypocrite Team Colleague

Here is some C code that might help you get started.

//  A linear list of data accessed first in, first out (FIFO) is called a QUEUE.
//  Pelles C  (vegaseat) 

#include <stdio.h>   // in/out function header
#include <string.h>  // string function header
#include <stdlib.h>  // malloc()

void enter(void);
void list(void);
void q_store(char *ptr);
void q_delete(void);
int store = 0;         // next storage position in queue[] 
int retrieve = 0;      // retrieve position in queue[] 
char *queue[100];      // this array forms the queue 

void main(void)
{
  enter();     // enter some data in the queue and list the data 
  puts("\n\nThis is all the data in the fifo queue:");
  list();   
  q_delete();  // delete first entry in queue and list again
  puts("\n\nThis the data after one deletion (first in, first out):");
  list();    
  getchar();
}

//
// prompt for data entry and store in queue via q_store()
//
void enter(void) 
{
  static char str[100], *ptr;

  do {
    printf("Enter name (ENTER only will exit) : ");
    gets(str);
    ptr = (char *) malloc(strlen(str));  // starting address of memory 
    strcpy(ptr,str);
    if (*str)  
      q_store(ptr);   // store in queue if string has info 
  } while (*str);     // until no entry
}

void list(void)   // list the contents of the queue 
{
  int k;

  for (k = retrieve; k < store; k++)
    printf("\n%d) %s",k+1,queue[k]);
}

//
// store data items in the queue 
//
void q_store(char *ptr)
{   
  if (store == 100) { 
    puts("\nList is full!");  
    return; 
  }
  queue[store] = ptr;
  store++;  // point to next available storage position …