我的代码的第一部分就像我想要的那样运行,但是当我使用net localgroup管理员创建帐户管理员代码崩溃时,我收到一条消息“在线阅读我们的隐私声明......”所以我是假设我的代码在我的电脑中引发了一些红旗。我只是想让这段代码正常运行,我不知道如何修复这样的问题。
#include <iostream>
#include <string>
#include <stdio.h>
#include <cstdlib>
int main()
{
std::string name;
std::string admin;
std::string pass;
std::string full;
std::string mkusr;
std::string mkadm;
std::string wait;
std::string passreq;
std::string times;
std::string passchg;
std::string comment;
std::string expires;
std::cout << "What is the new username?\n";
getline (std::cin, name);
std::cout << "What is the full name? FIRSTNAME MIDDLEINITIAL LASTNAME\n";
getline (std::cin, full);
std::cout << "What password should "<< name <<" have?\n";
getline (std::cin, pass);
std::cout << "Is " << name << " an admin?\nY or N\n";
getline (std::cin, admin);
std::cout << "Does " << name << " need to have a password\nY or N\n";
getline (std::cin, passreq);
std::cout << "When is "<< name <<" authorised to be on? For 24/7 access please enter all\n\nFormat <M,T,W,Th,F,Sa,Su OR M-Su>,<1AM-12PM OR 01:00-23:00>\nTo input multiple time across different days use the ; symbol \nEXAMPLE: M,4AM-5PM;T,1PM-3PM;W-F,8:00-17:00\n";
getline (std::cin, times);
std::cout << "Is " << name << " allowed to change his/her password\nY or N\n";
getline (std::cin, passchg);
std::cout << "Please give a description for " << name << "\n";
getline (std::cin, comment);
std::cout << "When will " << name << "s account expire? If it will never expire please type never\nFormat <MM/DD/YYYY> OR <DD/MM/YYYY> OR <mmm,dd EX:jan,9>\n";
getline (std::cin, expires);
system ("cls");
mkusr = "net user ";
mkusr += name;
mkusr += " ";
mkusr += pass;
mkusr += " /add ";
if (passreq == "Y"){
mkusr += " /passwordreq:yes";
}
else {
mkusr += " /passwordreq:no";
};
mkusr += (" /times:");
times = '"' + times +'"';
mkusr += times;
mkusr += " /fullname:";
full = '"' + full +'"';
mkusr += full;
if (passchg == "Y"){
mkusr += " /passwordchg:yes";
}
else{
mkusr += " /passwordchg:no";
};
mkusr += " /comment:";
comment = '"' + comment +'"';
mkusr += comment;
mkusr += " /expires:";
expires = '"' + expires +'"';
mkusr += expires;
//net user 'name' 'pass' /add /passwordreq:'yes/no' /times:'times' /fullname:"'full name'" /passwordchg:'yes/no' /expires:'mm/dd/yyyy||dd/mm/yyy||never' /comment:'cmt'
//times format M,4AM-5PM;T,1PM-3PM;M-F,08:00-17:00 all
std::cout << mkusr;
const char* mkuser = mkusr.c_str();
system(mkusr.c_str());
//system("net user /add << name << pass << /FULLNAME:<<full<<");
// THIS IS THE PROBLEM AREA
if (admin == "Y"){
std::cout << "Ok, I will make " << name << " an admin\n Please press any key to make "<< name <<"an admin.";
getline (std::cin, wait);
mkadm = "net localgroup administrators ";
mkadm += name;
mkadm += " /add";
std::cout << "\n " << mkadm;
const char* mkadm = mkadm;
system(mkadm);
//system("net localgroup administrators << name << /add");
// THIS IS THE PROBLEM AREA
}
getline (std::cin, wait);
return 0;
}
当我创建一个只有问题部分的代码时,我得到同样的崩溃`
#include <iostream>
#include <string>
#include <stdio.h>
#include <cstdlib>
int main()
{
std::string name;
std::string admin;
std::string wait;
std::string mkadm;
getline (std::cin, admin);
if (admin == "Y"){
std::cout << "Ok, I will make " << name << " an admin\n Please press any key to make "<< name <<"an admin.";
getline (std::cin, wait);
mkadm = "net localgroup administrators ";
mkadm += name;
mkadm += " /add";
std::cout << "\n " << mkadm;
const char* mkadm = mkadm;
system(mkadm);
}
getline (std::cin, wait);
return 0;
}`
但是当我只做
时system(net localgroup administrators Test /add)
工作正常。当我在那里使用varible和/或当我使用“const char *”时它不喜欢 抱歉可怕的格式化
答案 0 :(得分:0)
这是你的问题:
const char* mkadm = mkadm;
那并不是你所希望的。右侧的mkadm
是您刚刚定义的变量,而不是外部块的std::string
。换句话说,你将变量初始化为与自身相等,然后用随机指针到内存中的某个地方。
给两个变量命名不同,你的问题就会消失:
const char * mkadm2 = mkadm.c_str();
system(mkadm2);