Solved

String conversions....

Posted on 2004-08-30
5
192 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
Comment Utility
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
Comment Utility
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
Comment Utility
>> it'd be better to use...

Yeah.

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

Expert Comment

by:mrwad99
Comment Utility
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
Comment Utility
JK,

You never cease to amaze me!  Thanks again!

R/
Tony
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 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

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now