Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Differentiating cin >> streams

Posted on 2004-03-28
4
Medium Priority
?
394 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 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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 …
Six Sigma Control Plans

916 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