c++如何改为一直猜数字直到猜对为止,这个只能猜五次

img


c++这个代码如何改为一直猜数字直到猜对为止,这个只能猜五次,不理解,new不是动态分配吗

你这是JAVA代码。你这代码里的new都是申请类型变量,没有动态分配的东西。还是你贴错代码了?
猜数游戏C++代码:

#include <iostream>
#include <stdlib.h>
#include <time.h>
using namespace std;
void caishu(int n)
{
    int c,count=0;
    int max=100,min=1;
    system("cls");
    while(1)
    {
        cout <<"请输入一个数:";
        cin >> c;
        count++;
        if(c>n)
        {
            max = c;
            cout << "太大了,数应该在"<< min <<"-"<<max<<"之间。";
        }
        else if(c<n)
        {
            min = c;
            cout << "太小了,数应该在"<< min<<"-"<< max<<"之间。";
        }
        else
        {
            cout <<"猜对了,共猜了"<< count<<"次"<<endl;
            system("pause");
            return;
        }
    }
}



int main()
{
    int n;
    int op;
    srand((unsigned int)time(0));
    //菜单
    while(1)
    {
        system("cls");
        cout <<"欢迎使用猜数游戏\n";
        cout <<"1.开始游戏\n";
        cout <<"2.退出游戏\n";
        cin >> op;
        switch(op)
        {
        case 1:
            n = rand()%100+1;
            caishu(n);
            break;
        case 2:
            return 0;
        }
    }



}



猜数游戏C代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void caishu(int n)
{
    int c,count=0;
    int max=100,min=1;
    system("cls");
    while(1)
    {
        printf("请输入一个数:");
        scanf("%d",&c);
        count++;
        if(c>n)
        {
            max = c;
            printf("太大了,数应该在%d-%d之间。",min,max);
        }
        else if(c<n)
        {
            min = c;
            printf("太小了,数应该在%d-%d之间。",min,max);
        }
        else
        {
            printf("猜对了,共猜了%d次\n",count);
            system("pause");
            return;
        }
    }
}



int main()
{
    int n;
    int op;
    srand((unsigned int)time(0));
    //菜单
    while(1)
    {
        system("cls");
        printf("欢迎使用猜数游戏\n");
        printf("1.开始游戏\n");
        printf("2.退出游戏\n");
        scanf("%d",&op);
        switch(op)
        {
        case 1:
            n = rand()%100+1;
            caishu(n);
            break;
        case 2:
            return 0;
        }
    }



}



您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632