Solved

testing for carriage return

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 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 user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

746 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

11 Experts available now in Live!

Get 1:1 Help Now