Solved

testing for carriage return

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ assignment question 7 194
SNMP error No Such Object available on this agent at this OID 3 237
max float value 3 39
Android development question 2 34
  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 …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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++.

815 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

7 Experts available now in Live!

Get 1:1 Help Now