![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#11 |
|
Game Dev Beginner
Join Date: Aug 2008
Location: Manchester
Posts: 11
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#12 |
|
Programming Guru
![]() ![]() |
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. |
|
|
|
|
|
#13 |
|
Hobbyist Programmer
|
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 |
|
|
|
|
|
#14 |
|
Game Dev Beginner
Join Date: Aug 2008
Location: Manchester
Posts: 11
Rep Power: 0
![]() |
Re: String to Char Array Conversion
Shouldnt crash I think. Can you post your code?
|
|
|
|
|
|
#15 |
|
Hobbyist Programmer
|
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. |
|
|
|
|
|
#16 |
|
Game Dev Beginner
Join Date: Aug 2008
Location: Manchester
Posts: 11
Rep Power: 0
![]() |
Re: String to Char Array Conversion
Looks quite ok to me. Just one thing - you forgot about closing } for while loop
![]() |
|
|
|
|
|
#17 |
|
Hobbyist Programmer
|
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. |
|
|
|
|
|
#18 |
|
Game Dev Beginner
Join Date: Aug 2008
Location: Manchester
Posts: 11
Rep Power: 0
![]() |
Re: String to Char Array Conversion
|
|
|
|
|
|
#19 |
|
Hobbyist Programmer
|
Re: String to Char Array Conversion
Thanks very, very much - that works! Do you have any idea why?
Thanks again! Matt. I |
|
|
|
|
|
#20 |
|
Game Dev Beginner
Join Date: Aug 2008
Location: Manchester
Posts: 11
Rep Power: 0
![]() |
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 |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |