鱼C论坛

 找回密码
 立即注册
12
返回列表 发新帖
楼主: 小凯2013

switch 语句报错

[复制链接]
 楼主| 发表于 2022-12-28 11:06:28 | 显示全部楼层
那还不是为了初始化结构体指针
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-28 11:47:58 | 显示全部楼层
小凯2013 发表于 2022-12-28 11:06
那还不是为了初始化结构体指针

初始化了吗?没有吧?
C++有自己的new/delete你不用,你偏要用C的malloc/free
那也可以呀,只是你得自己调用对象的构造函数和析构函数
问题是你调用了吗?

  1. #include <iostream>
  2. #include <string>
  3. #include <cstdlib>

  4. using std::cout, std::endl;
  5. using std::string;
  6. using std::malloc, std::free;

  7. struct test_t {
  8.     string str;
  9.     int value;
  10. };

  11. int main() {
  12.     test_t *t = (test_t *)malloc(sizeof(*t));   // 申请内存
  13.     new(&t->str) string();      // 创建string对象
  14.     t->str = "1234";
  15.     t->value = 1234;
  16.     cout << t->str << endl;
  17.     cout << t->value << endl;
  18.     t->str.~basic_string();     // 销毁string对象
  19.     free(t);                    // 释放内存
  20.     return 0;
  21. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-12-28 11:48:54 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-12-29 08:29:22 | 显示全部楼层
小凯2013 发表于 2022-12-25 09:27
实际上有511行的,我怕鱼C论坛炸掉
所以我就扔了个模型。

9000行也不会炸
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-29 18:07:35 | 显示全部楼层
小凯2013 发表于 2022-12-25 09:39
现在变这样子了:
C:\Program Files\Embarcadero\Dev-Cpp\TDM-GCC-64\x86_64-w64-mingw32\bin\ld.exe        C:%u ...

你看看switch后面括号里东西的类型和后面常量类型是否相同
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-29 18:08:33 | 显示全部楼层
小凯2013 发表于 2022-12-25 09:39
现在变这样子了:
C:\Program Files\Embarcadero\Dev-Cpp\TDM-GCC-64\x86_64-w64-mingw32\bin\ld.exe        C:%u ...

你read和write的问题,不是switch
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-12-31 09:07:39 | 显示全部楼层
小凯2013 发表于 2022-12-25 19:22
源码【不许侵权】:

你这 readme 也不是 markdown 啊
而且条款不是 License 吗,你见过那个条款不让私自编译的
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

 楼主| 发表于 2023-1-1 15:33:13 | 显示全部楼层
liuzhengyuan 发表于 2022-12-31 09:07
你这 readme 也不是 markdown 啊
而且条款不是 License 吗,你见过那个条款不让私自编译的

今天你就见到了!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 0 反对 1

使用道具 举报

发表于 2023-1-31 13:50:09 | 显示全部楼层
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cerrno>
  5. #include <bits/extc++.h>

  6. using namespace std;

  7. typedef unsigned int uint, *puint;
  8. typedef const char cchar, *pcchar;

  9. #define randint(a, b) ((rand() % (b-a+1)) + a)
  10. #define MAX_LEVEL 15



  11. struct Account {
  12.         string name = "Player";
  13.         uint max_score = 0;
  14.         uint score = 0;
  15.         uint level = 0;
  16.         uint exp = 0;
  17.         long long atid = randint(100000000000000, 999999999999999);
  18. };

  19. bool add(uint a, uint b);
  20. bool sub(uint a, uint b);
  21. bool mcl(uint a, uint b);
  22. bool div(uint a, uint b);
  23. bool mwriteInfo(string file, Account *account);
  24. bool mreadInfo(string file, Account *account);

  25. bool mywriteInfo(pcchar file, Account *account) {
  26.         FILE *fp = NULL;
  27.         
  28.         if ((fp = fopen(file, "w")) == NULL) {
  29.                 cout << "打开文件失败:[Errno %d]: %s" << errno, strerror(errno);
  30.                 return false;
  31.         }
  32.         
  33.         fwrite(account, sizeof(Account), 1, fp);
  34.         return true;
  35. }

  36. bool myreadInfo(pcchar file, Account *account) {
  37.         FILE *fp = NULL;
  38.         char choose;
  39.         
  40.         if ((fp = fopen(file, "r")) == NULL) {
  41.                 cout << "打开文件失败:[Errno %d]: %s" << errno, strerror(errno);
  42.                 return false;
  43.         }
  44.         
  45.         cout << "确定覆盖现在的信息吗?【Y/n】";
  46.         cin >> choose;
  47.         
  48.         switch (choose) {
  49.                 case 'Y':
  50.                 case 'y':
  51.                         break;
  52.                 case 'N':
  53.                 case 'n':
  54.                         cout << "用户取消了操作。" << endl;
  55.                         return true;
  56.                         
  57.                 default:
  58.                         cout << "请认真输入!" << endl;
  59.                         return true;
  60.         }
  61.         
  62.         fread(account, sizeof(Account), 1, fp);
  63.         return true;
  64. }

  65. int main(void) {
  66.         short int code;
  67.         int number, score, operatorNum;
  68.         Account *account;
  69.         bool savable = false;
  70.         string rfile, wfile;
  71.         
  72.         cout << "---欢迎来到口算小能手---" << endl;
  73.         
  74.         while (code != 0) {
  75.                 cout << "请选择模式:" << endl;
  76.                 cout << "1 -- 纯加法[+1-3]" << endl;
  77.                 cout << "2 -- 纯减法[+1-3]" << endl;
  78.                 cout << "3 -- 纯乘法[+2-4]" << endl;
  79.                 cout << "4 -- 纯除法[+2-4]" << endl;
  80.                 cout << "5 -- 混合运算[作对一题就+1]" << endl;
  81.                 cout << "6 -- 死亡算法[作对一题就+3]" << endl;
  82.                 cout << "7 -- 导入数据[+0]" << endl;
  83.                 cout << "8 -- 保存数据[+0]" << endl;
  84.                 cout << "9 -- 查看账户信息[+0]" << endl;
  85.                 cout << "0 -- Exit退出[+1]" << endl;
  86.                 cout << "注:200积分升一级,共15级qwq!" << endl;
  87.                 cout << "请输入指令代码:";
  88.                 cin >> code;

  89.                 if ((*account).exp >= 200 && (*account).level < 15) {
  90.                         (*account).exp -= 200;
  91.                         (*account).level += 1;
  92.                 }
  93.                
  94.                 switch (code) {
  95.                         case 0:
  96.                                 if ((*account).score > 0) {
  97.                                         if (savable) {
  98.                                                 exit(EXIT_SUCCESS);
  99.                                         }
  100.                                         else {
  101.                                                 char choose;
  102.                                                 
  103.                                                 cout << "你还没有保存信息哦!" << endl;
  104.                                                 cout << "确定要退出吗?【N/y】";
  105.                                                 cin >> choose;
  106.                                                 
  107.                                                 switch (choose) {
  108.                                                         case 'Y':
  109.                                                         case 'y':
  110.                                                                 exit(EXIT_SUCCESS);
  111.                                                                 break;
  112.                                                                
  113.                                                         case 'N':
  114.                                                         case 'n':
  115.                                                                 break;
  116.                                                 }
  117.                                         }
  118.                                 }
  119.                                 else {
  120.                                         cout << "别想钻空子!加分不是白加的!" << endl;
  121.                                 }
  122.                                 code = 10;
  123.                                 break;
  124.                                 
  125.                         case 1:
  126.                                 cout << "请输入题目数:";
  127.                                 cin >> number;
  128.                                 
  129.                                 for (int count = number;count > 0;count--) {
  130.                                         if (add(randint(100, 999), randint(100, 999))) {
  131.                                                 score++;
  132.                                         }
  133.                                 }
  134.                                 
  135.                                 (*account).score += score;
  136.                                 
  137.                                 if (score >= (*account).max_score) {
  138.                                         (*account).max_score = score;
  139.                                 }
  140.                                 
  141.                                 if (score >= number/5) {
  142.                                         (*account).exp += 1;
  143.                                 }
  144.                                 else if (score >= number/3) {
  145.                                         (*account).exp += 2;
  146.                                 }
  147.                                 else if (score >= number/1) {
  148.                                         (*account).exp += 3;
  149.                                 }
  150.                                 
  151.                                 score = 0;
  152.                                 break;
  153.                                 
  154.                         case 2:
  155.                                 cout << "请输入题目数:";
  156.                                 cin >> number;
  157.                                 
  158.                                 uint a, b;
  159.                                 
  160.                                 while (true) {
  161.                                         a = randint(100, 999);
  162.                                         b = randint(100, 999);
  163.                                        
  164.                                         if (a >= b) {
  165.                                                 break;
  166.                                         }
  167.                                 }
  168.                                 
  169.                                 for (int count = number;count > 0;count--) {
  170.                                         if (sub(a, b)) {
  171.                                                 score++;
  172.                                         }
  173.                                 }
  174.                                 
  175.                                 (*account).score += score;
  176.                                 
  177.                                 if (score >= (*account).max_score) {
  178.                                         (*account).max_score = score;
  179.                                 }
  180.                                 
  181.                                 if (score >= number/5) {
  182.                                         (*account).exp += 1;
  183.                                 }
  184.                                 else if (score >= number/3) {
  185.                                         (*account).exp += 2;
  186.                                 }
  187.                                 else if (score >= number/1) {
  188.                                         (*account).exp += 3;
  189.                                 }
  190.                                 
  191.                                 score = 0;
  192.                                 break;
  193.                         
  194.                         case 3:
  195.                                 cout << "请输入题目数:";
  196.                                 cin >> number;
  197.                                 
  198.                                 for (int count = number;count > 0;count--) {
  199.                                         if (mcl(randint(100, 999), randint(100, 999))) {
  200.                                                 score++;
  201.                                         }
  202.                                 }
  203.                                 
  204.                                 (*account).score += score;
  205.                                 
  206.                                 if (score >= (*account).max_score) {
  207.                                         (*account).max_score = score;
  208.                                 }
  209.                                 
  210.                                 if (score >= number/5) {
  211.                                         (*account).exp += 2;
  212.                                 }
  213.                                 else if (score >= number/3) {
  214.                                         (*account).exp += 3;
  215.                                 }
  216.                                 else if (score >= number/1) {
  217.                                         (*account).exp += 4;
  218.                                 }
  219.                                 
  220.                                 score = 0;
  221.                                 break;
  222.                                 
  223.                         case 4:
  224.                                 cout << "请输入题目数:";
  225.                                 cin >> number;
  226.                                 
  227.                                 uint c, d;
  228.                                 
  229.                                 while (true) {
  230.                                         c = randint(100, 999);
  231.                                         d = randint(100, 999);
  232.                                        
  233.                                         if (c % d == 0) {
  234.                                                 break;
  235.                                         }
  236.                                 }
  237.                                 
  238.                                 for (int count = number;count > 0;count--) {
  239.                                         if (div(randint(c, d), randint(c, d))) {
  240.                                                 score++;
  241.                                         }
  242.                                 }
  243.                                 
  244.                                 (*account).score += score;
  245.                                 
  246.                                 if (score >= (*account).max_score) {
  247.                                         (*account).max_score = score;
  248.                                 }
  249.                                 
  250.                                 if (score >= number/5) {
  251.                                         (*account).exp += 2;
  252.                                 }
  253.                                 else if (score >= number/3) {
  254.                                         (*account).exp += 3;
  255.                                 }
  256.                                 else if (score >= number/1) {
  257.                                         (*account).exp += 4;
  258.                                 }
  259.                                 
  260.                                 score = 0;
  261.                                 break;
  262.                                 
  263.                         case 5:
  264.                                 cout << "请输入题目数:";
  265.                                 cin >> number;
  266.                                 
  267.                                 for (int count = number;count > 0;count--) {
  268.                                         operatorNum = randint(1, 4);
  269.                                         uint exp;
  270.                                        
  271.                                         switch (operatorNum) {
  272.                                                 case 1:
  273.                                                         if (add(randint(100, 999), randint(100, 999))) {
  274.                                                                 score++;
  275.                                                                 exp++;
  276.                                                         }
  277.                                                         
  278.                                                         break;
  279.                                                         
  280.                                                 case 2:
  281.                                                         uint a, b;
  282.                                                         
  283.                                                         while (true) {
  284.                                                                 a = randint(100, 999);
  285.                                                                 b = randint(100, 999);
  286.                                                                
  287.                                                                 if (a >= b) {
  288.                                                                         break;
  289.                                                                 }
  290.                                                         }
  291.                                                         
  292.                                                         if (sub(a, b)) {
  293.                                                                 score++;
  294.                                                                 exp++;
  295.                                                         }
  296.                                                         
  297.                                                         break;
  298.                                                         
  299.                                                 case 3:
  300.                                                         if (mcl(randint(100, 999), randint(100, 999))) {
  301.                                                                 score++;
  302.                                                                 exp++;
  303.                                                         }
  304.                                                         
  305.                                                         break;
  306.                                                         
  307.                                                 case 4:
  308.                                                         uint c, d;
  309.                                                         
  310.                                                         while (true) {
  311.                                                                 c = randint(100, 999);
  312.                                                                 d = randint(100, 999);
  313.                                                                
  314.                                                                 if (c % d == 0) {
  315.                                                                         break;
  316.                                                                 }
  317.                                                         }
  318.                                                         
  319.                                                         if (div(c, d)) {
  320.                                                                 score++;
  321.                                                                 exp++;
  322.                                                         }
  323.                                         }
  324.                                 }
  325.                                 
  326.                                 (*account).score += score;
  327.                                 
  328.                                 if (score >= (*account).max_score) {
  329.                                         (*account).max_score = score;
  330.                                 }
  331.                                 
  332.                                 score = 0;
  333.                                 break;
  334.                                 
  335.                         case 6:
  336.                                 cout << "请输入题目数:";
  337.                                 cin >> number;
  338.                                 
  339.                                 for (int count = number;count > 0;count--) {
  340.                                         operatorNum = randint(1, 4);
  341.                                         uint exp;
  342.                                        
  343.                                         switch (operatorNum) {
  344.                                                 case 1:
  345.                                                         if (add(randint(10000, 99999), randint(10000, 99999))) {
  346.                                                                 score++;
  347.                                                                 exp += 3;
  348.                                                         }
  349.                                                         
  350.                                                         break;
  351.                                                         
  352.                                                 case 2:
  353.                                                         uint a, b;
  354.                                                         
  355.                                                         while (true) {
  356.                                                                 a = randint(10000, 99999);
  357.                                                                 b = randint(10000, 99999);
  358.                                                                
  359.                                                                 if (a >= b) {
  360.                                                                         break;
  361.                                                                 }
  362.                                                         }
  363.                                                         
  364.                                                         if (sub(a, b)) {
  365.                                                                 score++;
  366.                                                                 exp += 3;
  367.                                                         }
  368.                                                         
  369.                                                         break;
  370.                                                         
  371.                                                 case 3:
  372.                                                         if (mcl(randint(10000, 99999), randint(10000, 99999))) {
  373.                                                                 score++;
  374.                                                                 exp += 3;
  375.                                                         }
  376.                                                         
  377.                                                         break;
  378.                                                         
  379.                                                 case 4:
  380.                                                         uint e, f;
  381.                                                         
  382.                                                         while (true) {
  383.                                                                 e = randint(10000, 99999);
  384.                                                                 f = randint(10000, 99999);
  385.                                                                
  386.                                                                 if (e % f == 0) {
  387.                                                                         break;
  388.                                                                 }
  389.                                                         }
  390.                                                         
  391.                                                         if (div(e, f)) {
  392.                                                                 score++;
  393.                                                                 exp += 3;
  394.                                                         }
  395.                                         }
  396.                                 }
  397.                                 
  398.                                 (*account).score += score;
  399.                                 
  400.                                 if (score >= (*account).max_score) {
  401.                                         (*account).max_score = score;
  402.                                 }
  403.                                 
  404.                                 score = 0;
  405.                                 break;
  406.                                 
  407.                         case 7:
  408.                                 cout << "请输入文件路径:";
  409.                                 cin >> rfile;
  410.                                 
  411.                                 if (myreadInfo(rfile.c_str(), account)) {
  412.                                         cout << "读取信息成功!" << endl;
  413.                                 }
  414.                                 else {
  415.                                         cout << "读取文件失败,请重试。" << endl;
  416.                                 }
  417.                                 
  418.                                 break;
  419.                                 
  420.                         case 8:
  421.                                 cout << "请输入要保存的路径:";
  422.                                 cin >> wfile;
  423.                                 
  424.                                 if (mywriteInfo(wfile.c_str(), account)) {
  425.                                         cout << "保存文件成功!" << endl;
  426.                                         savable = true;
  427.                                 }
  428.                                 else {
  429.                                         cout << "保存文件失败,请重试。" << endl;
  430.                                 }
  431.                                 
  432.                                 break;
  433.                                 
  434.                         case 9:
  435.                                 cout << "Account Information:" << endl;
  436.                                 cout << "Name(姓名):" << (*account).name << endl;
  437.                                 cout << "Max-Score(最高分数):" << (*account).max_score << endl;
  438.                                 cout << "Score(累计分数):" << (*account).score << endl;
  439.                                 cout << "Level(等级):" << (*account).score << endl;
  440.                                 cout << "Exp(经验):" << (*account).score << endl;
  441.                                 cout << "ATID(账户ID):" << (*account).atid << endl;
  442.                                 
  443.                                 break;
  444.                                 
  445.                 }
  446.         }
  447.         
  448.         return 0;
  449. }

  450. bool add(uint a, uint b) {
  451.         uint answer = a + b;
  452.         uint userinput;
  453.         
  454.         cout << "%d + %d = " << a, b;
  455.         cin >> userinput;
  456.         
  457.         if (userinput == answer) {
  458.                 cout << "答对啦!" << endl;
  459.                 return true;
  460.         }
  461.         else {
  462.                 cout << "答错啦!" << endl;
  463.                 return false;
  464.         }
  465. }

  466. bool sub(uint a, uint b) {
  467.         uint answer = a - b;
  468.         uint userinput;
  469.         
  470.         cout << "%d - %d = " << a, b;
  471.         cin >> userinput;
  472.         
  473.         if (userinput == answer) {
  474.                 cout << "答对啦!" << endl;
  475.                 return true;
  476.         }
  477.         else {
  478.                 cout << "答错啦!" << endl;
  479.                 return false;
  480.         }
  481. }

  482. bool mcl(uint a, uint b) {
  483.         uint answer = a * b;
  484.         uint userinput;
  485.         
  486.         cout << "%d × %d = " << a, b;
  487.         cin >> userinput;
  488.         
  489.         if (userinput == answer) {
  490.                 cout << "答对啦!" << endl;
  491.                 return true;
  492.         }
  493.         else {
  494.                 cout << "答错啦!" << endl;
  495.                 return false;
  496.         }
  497. }

  498. bool div(uint a, uint b) {
  499.         uint answer = a / b;
  500.         uint userinput;
  501.         
  502.         cout << "%d ÷ %d = " << a, b;
  503.         cin >> userinput;
  504.         
  505.         if (userinput == answer) {
  506.                 cout << "答对啦!" << endl;
  507.                 return true;
  508.         }
  509.         else {
  510.                 cout << "答错啦!" << endl;
  511.                 return false;
  512.         }
  513. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 19:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表