Solved

Differentiating cin >> streams

Posted on 2004-03-28
4
318 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

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

Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 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