Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 27th, 2008, 9:56 PM   #11
al3x
Game Dev Beginner
 
al3x's Avatar
 
Join Date: Aug 2008
Location: Manchester
Posts: 11
Rep Power: 0 al3x is on a distinguished road
Re: String to Char Array Conversion

if (txtarr[n] = "a") cout << "1";
else if (txtarr[n] = "b") cout << "2";

Use == not =. You're assigning a value instead comparing.
al3x is offline   Reply With Quote
Old Aug 27th, 2008, 10:08 PM   #12
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,028
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Re: String to Char Array Conversion

He also wrote "txtarr[n]" rather than "txtarr[i]". The list goes on.
__________________
Looking for tough programming challenges? Try participating in Sane's Monthly Algorithms Challenges!
Composing Techno is a little side hobby of mine. Techno by DJ Sane. All free for download.
Sane is offline   Reply With Quote
Old Aug 28th, 2008, 10:29 AM   #13
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 everyone for all your help! I've learnt a lot from you and I really appreciate it.

For some reason, my program crashes if there is a space in the first string, as in L7Sqr's program. Is this normal? Is there any way to avoid it?

@The Dark: Thanks, I see where I was oing wrong now. Thanks very much!

@Sane: Thanks very much! I can see what I was trying to do and failing there.

@L7Sqr: Thanks! I've based my program on that code.

Thanks very much again everyone!

Matt. Ireland
__________________
Matt Ireland
http://www.mattireland.org
matt@mattireland.co.uk
mattireland is offline   Reply With Quote
Old Aug 28th, 2008, 10:36 AM   #14
al3x
Game Dev Beginner
 
al3x's Avatar
 
Join Date: Aug 2008
Location: Manchester
Posts: 11
Rep Power: 0 al3x is on a distinguished road
Re: String to Char Array Conversion

Shouldnt crash I think. Can you post your code?
al3x is offline   Reply With Quote
Old Aug 28th, 2008, 10:47 AM   #15
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. I've missed out a few of the irrelevant chunks but here's the bit that's concerned. It's a morse playing program:

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(100); }
}

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] == ' ')
             play(27, pitch);
          else
              cout << s[i];
    i++;
}

Thanks again.
__________________
Matt Ireland
http://www.mattireland.org
matt@mattireland.co.uk
mattireland is offline   Reply With Quote
Old Aug 28th, 2008, 10:54 AM   #16
al3x
Game Dev Beginner
 
al3x's Avatar
 
Join Date: Aug 2008
Location: Manchester
Posts: 11
Rep Power: 0 al3x is on a distinguished road
Re: String to Char Array Conversion

Looks quite ok to me. Just one thing - you forgot about closing } for while loop
al3x is offline   Reply With Quote
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
Old Aug 28th, 2008, 11:25 AM   #18
al3x
Game Dev Beginner
 
al3x's Avatar
 
Join Date: Aug 2008
Location: Manchester
Posts: 11
Rep Power: 0 al3x is on a distinguished road
Re: String to Char Array Conversion

instead
cin >> s;
try:
getline(cin, s);
al3x is offline   Reply With Quote
Old Aug 28th, 2008, 11:31 AM   #19
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 very, very much - that works! Do you have any idea why?

Thanks again!

Matt. I
__________________
Matt Ireland
http://www.mattireland.org
matt@mattireland.co.uk
mattireland is offline   Reply With Quote
Old Aug 28th, 2008, 11:41 AM   #20
al3x
Game Dev Beginner
 
al3x's Avatar
 
Join Date: Aug 2008
Location: Manchester
Posts: 11
Rep Power: 0 al3x is on a distinguished road
Re: String to Char Array Conversion

Seems like cin stops after a space char (now speculating) and probably puts the rest of the string in next variable (?). I'm not great if it comes to console apps.

Glad I could help
al3x is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem processing file into a char array csrocker101 C++ 1 May 9th, 2007 12:50 AM
Problems with String to MD5 Conversion emdiesse Visual Basic .NET 0 Feb 2nd, 2006 11:25 AM
some funky string array thing Intimidat0r C# 2 Nov 24th, 2005 8:18 AM
string array problem MADTech C++ 8 Jul 27th, 2005 7:19 AM
Installing IPB 2.03 bh4575 Other Web Development Languages 0 Apr 23rd, 2005 3:36 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:50 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC