Solved

Differentiating cin >> streams

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 115
ejb wildfly example 2 71
incorrect syntax near the order by 10 57
"lblTime is not declared" 3 30
This is an explanation of a simple data model to help parse a JSON feed
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.

756 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