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
Solved

String conversions....

Posted on 2004-08-30
5
196 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
  • 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

839 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