Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

testing for carriage return

Posted on 1997-12-14
5
Medium Priority
?
395 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 400 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
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…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

721 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