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

x
?
Solved

Differentiating cin >> streams

Posted on 2004-03-28
4
Medium Priority
?
387 Views
Last Modified: 2010-04-17
I'm trying to design a command line for a mock up OS simulation. I am having trouble designing the command line. What I originally tried is this:

char a[10];
char b[10];
char c[10];
int d;
int e;

while (a != quit){
 if (cin >> a){
   cout << "This is a quit command";
 else if (cin >> a >> b){
  cout << "This is a command plus a file name";
 else if (cin >> a >> b >> c >> d >> e){
  cout << "This will create the files with parameters";
}

when I run this, I can only see the first if statement regardless of how many strings I put in.

Any help,
Don
0
Comment
Question by:ddoan
[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
4 Comments
 
LVL 3

Expert Comment

by:pamboo
ID: 10702440
cin >> a will always return true regardless of how many strings u give .

So , ur first if statment will occur always .

U can just use only one input variable say a of max length
say, char a[100]

Use strtok to get vlaues seperated .

P.S: I will try to post the modified code soon

- Rajan



0
 
LVL 3

Accepted Solution

by:
pamboo earned 200 total points
ID: 10702963
#include <iostream.h>
#include <string>

using namespace std;

void main()
{

  char inputBuffer[100];
  char a[10];

  char b[10];
  char c[10];
  int d;
  int e;

  int quit=1;

  char *ptr;
  int argc;

  while (quit)
  {
 
   argc = 0;  

   cin.getline(inputBuffer,100,'\n');

   ptr = strtok(inputBuffer," ");

   while(ptr)
   {           
      
       if (argc==0) strcpy(a,ptr);
       if (argc==1) strcpy(b,ptr);
       if (argc==2) strcpy(c,ptr);
       if (argc==3) d=atoi(ptr);
       if (argc==4) e=atoi(ptr);
        
       ptr = strtok(NULL," ");           
       argc++;
      
   }  
   
   if (argc==1)
   {
      cout << "This is a quit command" <<endl;
        cout << a << endl ;
        quit =0 ;
   }
   
   if (argc==3)
   {
     cout << "This is a command plus a file name" <<endl;
       cout << a << endl << b << endl << c << endl ;
   }
   
   if (argc==5)
   {
     cout << "This will create the files with parameters" <<endl ;
       cout << a << endl << b << endl << c << endl << d << endl << e << endl ;
   }
   
     
  }

}

- Rajan
0

Featured Post

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

This is about my first experience with programming Arduino.
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

670 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