Solved

String conversions....

Posted on 2004-08-30
5
197 Views
Last Modified: 2010-04-01
Hello all,

I wrote this little program as a LAN messenger.  Being very new to C++, I was quite proud!  However, when I run the program, it takes each string in the "message" and sends it seperately.  The program basically uses "net send" to send messages.  How do I keep every word in the sentence from being sent as a seperate message?  Here is the code so far:

#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{
  std::string target;
  std::string GetStarted = "net send ";
  std::string spacer = " ";
  std::string messg;

  cout << "Welcome to LAN Messenger 1.0 beta 1.\n";
  cout << "Enter /h in any field at anytime for help.\n";
  cout << "\n";
  system("Pause");

Begin:
  system("CLS");
  cout << "Target: ";
  cin >> target;
  if(target == "/h" || target == "/H")
  {
      system("CLS");
      cout << "*Target* <- Enter target users network name, or computer name.\n";
      cout << "*Message* <- Enter your message.\n";
      cout << "To change target users, enter / in the message field.\n";
      cout << "To view help file, enter /h in any field.\n";
      cout << "To exit, enter // in any field.\n";
      cout << "\n";
      system("Pause");
      goto Begin;
  }
  if(target == "//")
  {
      system("CLS");
      cout << "\n";
      cout << "Connection terminated.\n";
      cout << "\n";
      system("Pause");
      return 0;
  }        
 
 
  do
  {
      system("CLS");
      cout << "Connection to " << target << "\n";
      cout << "Message: ";
      cin >> messg;
     
      if(messg == "//")
      {
          system("CLS");
          cout << "\n";
          cout << "Connection to " << target << " terminated!\n";
          cout << "\n";
          system("Pause");
      }  
     
      if(messg == "/")
      {
          goto Begin;
      }    
     
      if(messg == "/h" || messg == "/H")
      {
          system("CLS");
          cout << "*Target* <- Enter target users network name, or computer name.\n";
          cout << "*Message* <- Enter your message.\n";
          cout << "To change target users, enter / in the message field.\n";
          cout << "To view help file, enter /h in the message field.\n";
          cout << "To exit, enter // in the message field.\n";
          cout << "\n";
          system("Pause");
      }    
               
      std::string command = GetStarted + target + spacer + messg;
      system(command.c_str());
      system("CLS");
  }while (messg != "//");  

  return 0;    

}

Any and all help is greatly appreciated!  

Regards,

Tony
0
Comment
Question by:fattumsdad
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 19

Assisted Solution

by:mrwad99
mrwad99 earned 100 total points
ID: 11931587
What you need to do is use the getline function provided in iostream:

#include <iostream>

using namespace std;

//...

char oneLine[256];
cin.getline(oneLine, 256);

then oneLine will hold everything that the user enters before pressing enter, including any spaces.

HTH
0
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 11931701
Actually, since you already are using strings, it'd be better to use

     cout << "Connection to " << target << "\n";
     cout << "Message: ";
     getline ( cin, messg);

rather than introducing a char array.

0
 
LVL 19

Expert Comment

by:mrwad99
ID: 11931748
>> it'd be better to use...

Yeah.

1851971 : 32762 - what can I say.  Experience shows through *again* :)
0
 
LVL 19

Expert Comment

by:mrwad99
ID: 11931960
If using VC++ also it is essential to read

http://support.microsoft.com/default.aspx?scid=kb;EN-US;q240015 

first though.
0
 
LVL 1

Author Comment

by:fattumsdad
ID: 11931974
JK,

You never cease to amaze me!  Thanks again!

R/
Tony
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question