相关疑难解决方法(0)

C++重启main而不会丢失变量

我程序的主要功能显示一个开关菜单.当输入选项1时,调用一个"洗牌"一系列"卡片"的函数.在混洗完成后,该函数通过调用main()将程序返回到开头,以便再次显示菜单.

我遇到的问题是菜单的选项4将混洗数组写入文件.但是当洗牌然后重新启动程序时,阵列数据会丢失,因此输出的文件都是垃圾.有没有办法重新启动main()而不丢失数据?

我在一个班级,我可以使用的工具受到限制,因此只有最基本的代码才可以接受.基本上,我正在寻找像goto但更安全的东西(顺便说一句,goto,在这个课程中也被禁止).

c++ program-entry-point

1
推荐指数
1
解决办法
2314
查看次数

类型 'std::string*& {aka std::basic_string<char>*&}' 的非const 引用从'std::string* {aka

我试图解决这个问题,我必须拼写数字。
当我尝试a通过引用调用我的字符串数组时,出现此错误。但是如果我按值调用它,我不会出错。
我不知道 rvalue 来自哪里,因为我的字符串元素应该被视为左值。

#include <iostream>
#include <string>
using namespace std;

void spell(int n,string* &a){
    if(n==0)
    return;
    spell(n/10,a);
    cout<<a[n%10];
}
int main(){
    int n;
    cin>>n;
    string a[10]{"zero ","one ","two ","three ","four ","five ","six ","seven ","eight ","nine "};
    spell(n,a);
    if(n<0)
    return 0;
    return main();
}
Run Code Online (Sandbox Code Playgroud)

c++ pass-by-reference

1
推荐指数
1
解决办法
98
查看次数

使用main()函数递归

我正在编写一个程序来使用main()函数的递归来计算阶乘.

/* Print factorial */
#include <stdio.h>
#include <stdlib.h>

static char **p;

int main(int argc, char **argv)
{
        int n, rv;
        if (argc < 2) {
                printf("Usage: a.out <value>\n");
                exit(-1);
        }
        n = atoi(argv[1]);
        if (!n) {
                rv = 0;
        } else {
                if (n == 1) {
                        rv = 1;
                } else {
                        n = n - 1;
                        **p = n;
                        main(2, p);
                }
        }
        printf("%d\n", rv);
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

该程序编译使用gcc但在执行时,我得到一个分段错误**p = n.有人可以帮我修改上面的程序以获得正确的结果.另外, …

c linux recursion segmentation-fault

0
推荐指数
1
解决办法
9101
查看次数

当我编译我的 C++ 代码时,avast 认为它是病毒

我第一次在开发控制台中编译并运行我的程序时,当我打开它时,我的电脑开始滞后,它在我的桌面上创建了一堆 .tmp 文件,名称类似于trzFE47.tmp我的电脑开始滞后我不得不关闭我的电脑我什至检查过任务管理器的后台进程处理可疑的事情,但我什么也没发现,所以当我重新启动我的电脑时,我再次编译我的 .cpp 程序,avast 给了我一个警告,说在我的代码中检测到可疑项目

#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
using namespace std;

int main() {
    int l;
    int a;
    int b;
    int c;
    Sleep(3000);

    srand(time(0));
    l = 1+rand()%6;
    a = 1+rand()%6;
    b = 1+rand()%6;
    c = 1+rand()%6;
    cout << a  << endl << b << endl << c << endl;
    if (a==b&& b==c&&c==a){
        int v;
        cout << "you win";
        cin >> v;
    }else{
        cout << "try again?"; …
Run Code Online (Sandbox Code Playgroud)

c++

0
推荐指数
1
解决办法
2456
查看次数