【发布时间】:2017-11-06 03:44:12
【问题描述】:
我正在制作一个程序,您可以在其中输入某些内容并检测到它。然后它改变一个变量。但问题是我需要访问 if 语句之外的新变量内容。代码如下:
#include "stdafx.h"
#include <string>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
while (1)
{
string dir = "C:/";
string line;
string afterPrint;
string prompt = "| " + dir + " |> ";
cout << prompt;
getline(cin, line);
stringstream ss(line);
while (ss)
{
string command;
ss >> command;
transform(command.begin(), command.end(), command.begin(), ::tolower);// Command, but lowercase
if (command == "cd")
{
string dir;
if (ss >> dir)
{
cout << "Directory: " << dir << "\n"; // it changes here
string prompt = "| " + dir + " |> "; // and here
}
}
else if (command == "c:" || command == "c:\\")
{
cout << "Directory: " << dir << "\n";
}
else
{
cout << "error\n";
}
break;
}
// but I need it here (when the loop restarts)
}
return 0;
}
任何帮助将不胜感激! 提前致谢!
【问题讨论】: