0

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;



}
Vishnu CS
  • 748
  • 1
  • 10
  • 24
Alok
  • 39
  • 7
  • 1
    You're using *recursion* to read and validate input. Sooner or later that *will* lead to problems. Please try to use a *loop* instead. – Some programmer dude Mar 27 '20 at 08:07
  • Normally you put function prototypes at the start of your code, before any function definitions, not inside each function where they are used. – john Mar 27 '20 at 08:07
  • @Someprogrammerdude To be fair, beginners who naturally write recursive code probably have potential as good programmers. – john Mar 27 '20 at 08:08
  • @john i did put all functions prototypes at the start of my code but it still gives the same logical error. does print the second output. You can probably run the code and see. – Alok Mar 27 '20 at 08:16
  • @Alok Yes I wasn't saying that was the cause of your error. Just a style tip – john Mar 27 '20 at 08:28
  • As a piece of advice: if you like to code, specially c/c++, mastering the debugger is essential. – bichito Mar 27 '20 at 18:17

1 Answers1

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.

john
  • 85,011
  • 4
  • 57
  • 81
  • Thanks. This code works perfectly. From now on i will remember to use local variables only. – Alok Mar 27 '20 at 09:47
  • just out of curiosity is there any scenario where using a global variable is better than using it as a local variables. – Alok Mar 27 '20 at 09:48
  • Yes it happens, in particular read-only data that needs to be accessed in several different parts of code can be put into global variables. The main problem with globals is that they quickly lead to badly structured code as your program gets bigger. See here for more reading https://stackoverflow.com/questions/484635/are-global-variables-bad – john Mar 27 '20 at 09:54