首页 > 解决方案 > c ++字符串没有被重置

问题描述

如果我输入第一个命令,create它应该打印incomplete command...,然后当我再次输入create file它应该打印的命令时Hello。但它只是打印而不是第二次incomplete command...打印输出。Hello请帮忙。我认为全局字符串命令不会每次都被重置,尽管每次accept_command调用函数时我都将它设置为空。我可能错了。

在此处输入代码

#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;



}

标签: c++stringglobal-variables

解决方案


正如您所说,全局字符串不会每次都重置。但是为什么要这样做,全局变量不能那样工作。您可以明确地重置全局变量,但正确的方法是使用局部变量。通常,您应该避免使用全局变量。

这个怎么样,我删除了全局变量commandcommand_words用本地变量替换它们。

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;
    }

}

这就是你应该如何工作,使用使用参数和返回值来相互传递数据的局部变量。


推荐阅读