if i enter the first command as create
it should print incomplete command...
and then when i again enter create file
as the command it should print Hello
. But it is only printing incomplete command...
but not printing the output Hello
the second time. Please help. I think the global string command is not getting reset every time although i have set it to empty every time the function accept_command
is called. I might be wrong.
enter code here
#include<iostream>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<conio.h>
#include<stdio.h>
using namespace std;
struct node
{
string details;
int shirtnumber;
struct node* next;
};
struct node* head=NULL;
struct node1
{
string details;
int shirtnumber;
struct node1* row;
struct node1* column;
};
struct node1* head1=NULL;
string command="";
string command_words[5]="";
void validate_command()
{
void command_words_calculate();
void accept_command();
command_words_calculate();
if(command_words[1]=="")
{
cout<<"incomplete command...\n"<<endl;
accept_command();
}
if(command_words[0].compare("create")==0)
{
cout<<"Hello\n"<<endl;
accept_command();
}
}
void command_words_calculate()
{
int i,k=0;
char ch,ch1;
string duplicate="";
for(i=0;i<command.length();i++)
{
ch=command.at(i);
if(ch==' ')
continue;
else
break;
}
command=command.substr(i,command.length());
for(i=0;i<command.length();i++)
{
ch=command.at(i);
if(i!=command.length()-1)
ch1=command.at(i+1);
if(ch==' ')
{
if(i!=command.length()-1&&ch1!=' ')
{
k++;
continue;
}
else if(i==command.length()-1)
{
break;
}
else
continue;
}
else
command_words[k]=command_words[k]+ch;
}
}
void display()
{
void accept_command();
cout<<"O.S"<<endl;
accept_command();
}
void accept_command()
{
command="";
void validate_command();
cout<<"root\\:>";
getline(cin,command);
validate_command();
}
int main()
{
void display();
display();
return 0;
}
Well as you say the global string is not getting reset each time. But why should it, global variable don't work that way. You could explcitly reset the global variable but the correct way to do this is to use local variables instead. Generally you should avoid global variables.
How about this, I deleted the global variables command
and command_words
and replaced them with local ones.
void accept_command()
{
string command=""; // local variable
void validate_command(string command);
cout<<"root\\:>";
getline(cin,command);
validate_command(command);
}
void validate_command(string command)
{
void command_words_calculate(string command, string* command_words);
void accept_command();
string command_words[5]={""}; // local variable
command_words_calculate(command, command_words);
if(command_words[1]=="")
{
cout<<"incomplete command...\n"<<endl;
accept_command();
}
if(command_words[0].compare("create")==0)
{
cout<<"Hello\n"<<endl;
accept_command();
}
}
void command_words_calculate(string command, string* command_words)
{
int i,k=0;
char ch,ch1;
string duplicate="";
for(i=0;i<command.length();i++)
{
ch=command.at(i);
if(ch==' ')
continue;
else
break;
}
command=command.substr(i,command.length());
for(i=0;i<command.length();i++)
{
ch=command.at(i);
if(i!=command.length()-1)
ch1=command.at(i+1);
if(ch==' ')
{
if(i!=command.length()-1&&ch1!=' ')
{
k++;
continue;
}
else if(i==command.length()-1)
{
break;
}
else
continue;
}
else
command_words[k]=command_words[k]+ch;
}
}
This is how you should work, with local variables which use parameters and return values to pass data to each other.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.