View Single Post
Old Aug 28th, 2008, 11:09 AM   #17
mattireland
Hobbyist Programmer
 
mattireland's Avatar
 
Join Date: Jul 2007
Location: Wales, United Kingdom
Posts: 210
Rep Power: 2 mattireland is on a distinguished road
Send a message via MSN to mattireland Send a message via Skype™ to mattireland
Re: String to Char Array Conversion

Thanks for the reply, but after it's asked me to input the pitch, it just jumps straight to "Press any Key to Continue" and ends the program, if there's a space in the string. Here's the entire code, if it helps:

#include <cstdlib>
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

void play (int cn, int pit)
{
  if (cn == 1)  { Beep(pit,66);  Sleep(40); Beep(pit,200); Sleep(80); }
  else if (cn == 2)  { Beep(pit,200); Sleep(40); Beep(pit,66); Sleep(40); Beep(pit,66); Sleep(40); Beep(pit,66); Sleep(80); }
  else if (cn == 3)  { Beep(pit,200); Sleep(40); Beep(pit,66); Sleep(40); Beep(pit,200); Sleep(40); Beep(pit,66); Sleep(80); }
  else if (cn == 11) { Beep(pit,200); Sleep(40); Beep(pit,66); Sleep(40); Beep(pit,200); Sleep(80); }
  else if (cn == 27) { Sleep(90); }
}



int main(int argc, char *argv[])
{
    
    string s;
    int pitch;
    int i = 0, j = 0;
    cout << "Please enter a string: " ;
    cin >> s;
    cout << endl << "Please enter a pitch in Hz (recommended 750): ";
    cin >> pitch;
    j = s.length ();
    while (i < j) {
          if (s[i] == 'a')
             play(1, pitch);
          else if (s[i] == 'b')
             play(2, pitch);
          else if (s[i] == 'c')
             play(3, pitch);
          else if (s[i] == 'd')
             play(4, pitch);
          else if (s[i] == 'e')
             play(5, pitch);
          else if (s[i] == 'f')
             play(6, pitch);
          else if (s[i] == 'g')
             play(7, pitch);
          else if (s[i] == 'h')
             play(8, pitch);
          else if (s[i] == 'i')
             play(9, pitch);
          else if (s[i] == 'j')
             play(10, pitch);
          else if (s[i] == 'k')
             play(11, pitch);
          else if (s[i] == ' ')
             play(27, pitch);
          else
              cout << s[i];
    i++;

}

    system("PAUSE");
    return EXIT_SUCCESS;
}

Many thanks.
__________________
Matt Ireland
http://www.mattireland.org
matt@mattireland.co.uk
mattireland is offline   Reply With Quote