大神!求助!初学!C++想编写一个基于命令提示符的日记本

初学C++想编写一个基于命令提示符的日记本代码没有报错,但是程序不能按步骤执行。
菜单项
1、写日记
2、查看历史(暂时不实现)
3、退出
编译环境用的visual studio 2013
代码如下:
/*
menu
input
save
*/
#include
using namespace std;
const int d_array=20;
void diary_menu();
int judge_dparameter();
void cycle_menu(int x);
void diary_write();

int main()
{
int t_a = 0;

diary_menu();
t_a = judge_dparameter();
cycle_menu(t_a);
//receive parameter excution step
//judge,excution
cin >>t_a;

}

//input menu
void diary_menu()
{
cout << "***1、写日记 " << endl;
cout << "
2、查看日记***" << endl;
cout << "***3、退出 ***" << endl<<endl;

}

int judge_dparameter() //judge parameter
{
int a=0 ;
cout << "a的范围:0 cin >> a;
while ((a>0 && a <= 3) == 0)
{
cout <<"你输入的值不正确,请重新输入"< cin >> a;
}
return a;
}

void cycle_menu(int x)
{
while (x != 3)
{
switch (x)
{
case 1:
{
diary_write();
}; break;
/*case 2:
{

        }; break;*/
    default:
        break;
    }
}

}

void diary_write()
{

char diary_array[d_array];

//Cycle the input string
for (int t_a = 0; t_a<d_array; ++t_a)
{
    cin.get(diary_array, d_array);
}

}

该回答引用ChatGPT-3.5

您好,看到您的代码,我注意到有几个问题可能导致程序不能按步骤执行:

  • 在 main 函数中,调用了 diary_menu() 和 judge_dparameter() 函数后,直接跳转到了 cycle_menu() 函数,而没有对用户输入的参数进行判断和执行相应的操作。您可以使用类似以下的代码来实现:

    int main()
    {
      int t_a = 0;
    
      diary_menu();
      t_a = judge_dparameter();
    
      // 循环判断用户输入的参数,并执行相应的操作
      while (t_a != 3) {
          cycle_menu(t_a);
          diary_menu();
          t_a = judge_dparameter();
      }
    
      return 0;
    }
    
  • 在 diary_write() 函数中,您使用了 cin.get() 函数来获取用户输入的字符串,但是没有对获取到的字符串进行任何处理和保存。您可以考虑将获取到的字符串存储到一个文件中,代码示例如下:

    void diary_write()
    {
      char diary_array[d_array];
    
      // 提示用户输入日记内容
      cout << "请输入日记内容(最多" << d_array - 1 << "个字符):" << endl;
    
      // 获取用户输入的字符串
      cin.get(diary_array, d_array);
    
      // 将用户输入的字符串保存到文件 diary.txt 中
      ofstream diary_file("diary.txt", ios::app);
      if (diary_file.is_open()) {
          diary_file << diary_array << endl;
          cout << "日记已保存" << endl;
      }
      else {
          cout << "打开文件失败" << endl;
      }
      diary_file.close();
    }
    
  • 在 switch 语句中,您只实现了 case 1 的功能,而没有实现 case 2 的功能。如果您需要实现查看历史日记的功能,可以考虑从文件中读取日记内容并输出,代码示例如下:
    case 2:
    {
      // 从文件中读取日记内容并输出
      ifstream diary_file("diary.txt");
      if (diary_file.is_open()) {
          string line;
          while (getline(diary_file, line)) {
              cout << line << endl;
          }
      }
      else {
          cout << "打开文件失败" << endl;
      }
      diary_file.close();
    }; break;
    

希望以上内容能对您有所帮助!