Solved

testing for carriage return

Posted on 1997-12-14
5
385 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 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.

749 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