Solved

testing for carriage return

Posted on 1997-12-14
5
389 Views
Last Modified: 2010-04-10
I would like to write a program that takes one command and sometimes one or two parameters.
I thought I would use an input statement like: cin>>Commandstr>>Paramonestr>>Paramtwostr;
I wanted to test Paramonestr for char 13 or '\r' to see if the user only entered a command (which is one possibility).
I also thought I could check the Commandstr var at the end for the carriage return char or newline char.  
How should I go about this, since my methods of testing for these values don't work?
Thanks for any help.
0
Comment
Question by:whatever080697
[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
  • 2
  • 2
5 Comments
 
LVL 11

Expert Comment

by:alexo
ID: 1176233
Why don't you check cin for eof?
0
 

Author Comment

by:whatever080697
ID: 1176234
Why test for EOF.  The user must enter one stream of input. Such as a command then a space then a name,space, company, user hits enter.  The user could also enter only a command depending on the command.  I need a way to take one char at a time then stop when a space is encountered, then save the char's taken into a string, then if more char's entered copy them into another string, until the user hits the enter key.

0
 

Accepted Solution

by:
jlilley earned 100 total points
ID: 1176235
This is overkill, but fairly general...


#include <iostream>
#include <string>
#include <list>
using namespace std;
void main()
{
   // Strings will be filled in as elements of words.
   // This inputs an arbitrary number of words on a single line.
   list<string> words;
   string tmp;
   char c;
   do {
      c = cin.get();
      if (c == ' ' || c == '\n') {
         // End of string
         words.push_back(tmp);
         tmp.resize(0);
      } else {
         tmp += c;
      }
   } while (c != '\n');

   cout << "The input strings were:" << endl;
   for (
      list<string>::iterator iter = words.begin();
      iter != words.end();
      ++iter
   ) {
      cout << *iter << endl;
   }
}

0
 

Author Comment

by:whatever080697
ID: 1176236
I think you got it, but what is list? A header file? I don't have it.  I've already basicly implemented your method, but does your method allow for more than one round of input. In other words, can I put your code in a loop and it'll still work?
0
 

Expert Comment

by:jlilley
ID: 1176237
The <list> is an STL template header file.  "Modern" C++ compilers have it.  It may be called "list.h" on some systems.  If you don't have STL, then just use an array of string instead.  If you don't have <string>, which is also a Standard C++ header, then use CString on MS-Windows.  Lacking that use a (gasp) array of char.  Good luck.
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

630 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