Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

char arrays and help with program

Status
Not open for further replies.

glitch003

Programmer
Jan 6, 2003
9
US
hi im trying to make a program that utilizes the net send command to
make a neat messenger. the only problem im having is that with char
arrays, i cannot make it send messages with spaces. another problem
that im having is with if statements using char arrays. for example:


Code:
char cmd[2];
cin >> cmd;
if (cmd=="y"){
cout << &quot;messaging again&quot;;
}
else if(cmd==&quot;n&quot;){
cout << &quot;not messaging again&quot;;
}
else if{cmd==&quot;ip&quot;){
cout << &quot;displaying ip info again&quot;;
}



so anyway here is my program. !!note: this program will only run if
you have windows XP or NT and your main drive is C.!!


Code:
#include <stdio.h>
#include <iostream.h>
#include <process.h>
#include <conio_mingw.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
int main(int argc, char *argv[])
{
 bool magain = true;
 bool ipconf = true;
 bool nameagain = true;
 char ip[50];
 char message[999999];
 char cmd[1];
 while (ipconf==true){
       nameagain=true;
       magain=true;
       spawnl(P_WAIT, &quot;c:\\WINDOWS\\system32\\ipconfig.exe&quot;,
&quot;c:\\WINDOWS\\system32\\ipconfig.exe&quot;, NULL);
       while (nameagain==true)
       {
             magain=true;
             cout << &quot;enter IP or name of computer to send message to:
&quot;;
             cin >> ip;
             while (magain==true)
             {
                   cout << &quot;\nEnter message to send to &quot;<<ip<<&quot; : &quot;;
                   cin >> message;
                   cout << &quot;sending, please wait\n&quot;;
                   spawnl(P_WAIT, &quot;c:\\WINDOWS\\system32\\net.exe&quot;,
&quot;c:\\WINDOWS\\system32\\net.exe&quot;, &quot;send &quot;, ip, &quot; &quot;, message, NULL);
                   cout << &quot;\nsend another message(y/n), change
user(c), display ipconfig(ip)\n&quot;;
                   cin >> cmd;
                   cout << cmd;
                   if (cmd==&quot;y&quot;){
                      magain=true;
                   }
                   else if(cmd==&quot;n&quot;){
                        magain=false;
                        nameagain=false;
                        ipconf=false;
                   }
                   else if(cmd==&quot;c&quot;){
                        magain=false;
                   }
                   else if(cmd==&quot;ip&quot;){
                        magain=false;
                        nameagain=false;
                   }
                   else{
                        cout << &quot;im sorry, that was an invalid
command.  program is now exiting\n&quot;;
                        magain=false;
                        nameagain=false;
                        ipconf=false;
                   }
             }
       }
 }

system(&quot;PAUSE&quot;);
  return 0;
}

thanx in advance!!
-glitch
 
Without looking through all your code or testing it I can spot one serious problem straight away: you are doing comparisons on c style strings like they are integers or something (ie. with overloaded '==' && '!=' operators). A prime example is this:[tt]

char cmd[2];
cin >> cmd;
if (cmd==&quot;y&quot;)[/tt]

with c style strings you should use the strcmp() function:[tt]

if (strcmp(cmd,&quot;y&quot;)==0)[/tt]

if (strcmp() returns zero then the two arguments are equal. If it is less than or more than zero then they are not equal (and you can determine string ordering that way!).

You could always use the std::string or MFC's CString classes or even write your own string class with the necessary overloaded operators if you don't want to use the C style strings. Otherwise, there are several useful ANSI string manipulation and comparison functions defined in <string.h> for your use.

[rockband]
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
thanx, this works great. the only other problem im having is storing char arrays with spaces in them, like a message, like if someone wants to send &quot;hello how are you doing&quot; over this program. how do i do spaces in char arrays???


-glitch
 
You shouldn't need to worry about spaces! They are included in a string, eg:

[tt]char myString[100] = &quot;Hello World!&quot;;

printf(&quot;%s\n&quot;,myString);[/tt]

Each character has an ASCII value - the value of a &quot;space&quot; character is 32. The space is treated just like any other character (except of course that it doesn't print anything!).

Also, I noticed your declaration:[tt]

char message[999999];[/tt]

I think you should be re-thinking this one!

You'll probably find things easier if you write this program to use connectionless UDP sockets and reduce the buffer size for the message (perhaps to 1024 bytes!).

[deejay]
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
Ok, i fixed changes the char message[999999] to char message[1024];. thanx for that tip. ok so ur right on that one, but how do i input strings?? if i type
Code:
cin >> message;
, it only gets the first word... thank you for all your help!
 
Also, what is the command to locate the home drive? EX my drive is F:\ instead of C:\...

thank you so much, you are such a big help!
 
does the [tt]gets()[/tt] function work??

[tt]char text[100];

printf(&quot;Enter something: &quot;);
gets(text);

printf(&quot;\n\n%s\n\n&quot;,text);[/tt]
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
By the way, this will get you the full path to your application (including the disk identifier!):[tt]

char path[MAX_PATH];
::GetModuleFileName(NULL,path,MAX_PATH);
tellis.gif

programmer (prog'ram'er), n A hot-headed, anorak wearing, pimple-faced computer geek.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top