Solved

Differentiating cin >> streams

Posted on 2004-03-28
4
331 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
  • 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 50 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exchange and Third Party application. 8 68
base64 decode encode 12 119
cmd.exe will not close when running .bat file to perform FTP upload 18 94
Apps blocked by Java 9 63
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
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 …

910 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

23 Experts available now in Live!

Get 1:1 Help Now