?
Solved

String conversions....

Posted on 2004-08-30
5
Medium Priority
?
200 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 400 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 800 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses
Course of the Month14 days, 14 hours left to enroll

770 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