c++ - 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;
}
解决方案
正如您所说,全局字符串不会每次都重置。但是为什么要这样做,全局变量不能那样工作。您可以明确地重置全局变量,但正确的方法是使用局部变量。通常,您应该避免使用全局变量。
这个怎么样,我删除了全局变量command
并command_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;
}
}
这就是你应该如何工作,使用使用参数和返回值来相互传递数据的局部变量。
推荐阅读
- r - 在 R 中使用 kable / kableExtra 将一列分成两列
- java - 如何从 Docker 连接到外部 oracle 数据库
- android - AVD 的模拟器进程被杀死
- c# - 在 Visual Studio 中增加构建版本号
- sql - 基于 IF 和组合的 SQL SELECT 行
- c# - 使用 Gembox.Document 的 azure 函数中的“无法加载文件或程序集‘PresentationCore’”错误
- logging - Magento 2 - 相同的登录,模态身份验证显示
- cypress - Cypress - 如何在 Cypress 中进行投票?
- c# - 用除括号外的子字符串拆分字符串
- python - 如何在不使用 generate 函数的情况下编写自定义工厂属性