请问一下我这段代码错在哪里?
提示为:Undefinedvariable:startstrUndefinedvariable:laststr...
提示为:Undefined variable: startstr Undefined variable: laststr
展开
2个回答
展开全部
#include "stdio.h"
#include "stdlib.h"
int main()
{
/*Start by declaring arrays and pointers*/
char guessNumber[4]={1,2,3,4}, userGuess(我用了缩写,不然太麻烦)[4]; int a; char *origin=guessNumber, (无需指针)*user=userGuess;
/*for (a=0;a<4;a++)
{
srand(a);
guessNumber[a]=rand()%9; (我已经帮你改成随机产生了)
printf("%d",guessNumber[a]);
}*/
/*Initialization end*/
/*introduction starts*/ (注释太多,影响阅读)
printf("Hello and welcome to \'GUESS MY NUMBER\'\n");
printf("If you are right on both positon and value, you receive B.\n");
printf("If you are right on value but not position, you receive S.\n");
printf("Good luck!\n"); (规则帮你改的更明确了)
/*introduction end*/
/*Core coding starts*/
int tryCount=0, maxTry=20, temp, i, j, count_B=0, count_S=0;
char *P=origin, *U=user;
for(tryCount=0;tryCount<20;tryCount++)
{
printf("\nWhat's the number:**** ");
/*Loop for user input*/
for(temp=0;temp<4;temp++)
scanf("userGuess[temp]"); (用户数据输入的代码有问题,已经帮你改了)
temp=0; //Set temp back to zero for next loop
for(i=0;i<4;i++) /*i for guessNumber*/
{
for(j=0;j<4;j++) /* j for userGuess*/
{
if((i==j)&&((*P)+i)==((*U)+j)) /*if position and number match*/
count_B++; /*B++*/
else if((i!=j)&&(*(P)+i)==(*(U)+j)) /*If number match*/ (这里有问题,只要*P==*U就可以了,你为什么加了i和j呢? )
count_S++; /*S++*/
}
}
if(count_B==4)
{ (猜对,输出祝贺语句)
//All code goes here....
tryCount=100; break;
}
printf("\n%dB%dS",count_B, count_S);
(输出提示,之后要清零,不然B和S会累加)
}
(我帮你加入了重复猜的代码,不过没有修补漏洞,输入时如果你输入负号(-),或者小数点(.)或者超过4位的数,比如1598546,程序会出问题的)
}
修改后的程序代码:
#include "stdio.h"
#include "stdlib.h"
#include "iostream.h"
int main()
{
int gn[4],ug[4];//gn is short for guessnumber,ug is short for userguess.
char p;
//produce a correct number
printf("Hello and welcome to 'GUESS THE NUMBER'\n\n");
printf("The correct number consists four digits between 0~9 !!!!!!!!!\n\n");
printf("If you are right on both positon and value, you receive a B !!!!!!!!!\n");
printf("If you are right on value but not position, you receive a S !!!!!!!!!\n\n");
printf("Good luck!\n");
START: gn[0]=rand()%10;// randomly produce number between 0~9
do gn[1]=rand()%10;
while (gn[1]==gn[0]);
do gn[2]=rand()%10;
while(gn[2]==gn[1]||gn[2]==gn[0]);
do gn[3]=rand()%10;
while(gn[3]==gn[2]||gn[3]==gn[1]||gn[3]==gn[0]);
/* if the correct number is randomly produced, we should use rand() under stdlib.h
so the program is more complicated*/
//printf("\n%-2d%-2d%-2d%-2d\n\n",gn[0],gn[1],gn[2],gn[3]);
printf("\nIMPORTANT: No digits appear twice in the correct number!!!!!!!!!\n");
int tryCount=0, maxTry=20, i, j,count_B=0, count_S=0;
int n1,n2,n3,n4; //n is short for number
for(tryCount=0;tryCount<20;tryCount++) {
printf("\nThen what's your number:");
SUN: scanf("%1d%1d%1d%1d",&n1,&n2,&n3,&n4);//user input
if (n1*n2*n3*n4<0||n1*n2*n3*n4>6561)
{printf("error!!!!!!!!!");
goto SUN;}
else {}
ug[0]=n1;
ug[1]=n2;
ug[2]=n3;
ug[3]=n4;
printf("\nThe number you input is %d %d %d %d",ug[0],ug[1],ug[2],ug[3]);
printf("\nYou have tried %d times",tryCount+1);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++) {
if((i==j)&&(gn[i]==ug[j]))
count_B++;
else if((i!=j)&&(gn[i]==ug[j]))
count_S++; }
}
if(count_B==4)
{ printf("\n\nYou have guessed the number!\n");
printf("\nCONGRATULATIONS!!!!!!!!!\n\n");
printf("Want to guess again?<y/n>\n");
cin>>p;
if (p=='y')
goto START;
else goto SET;
}
else printf("\n\nThe clue is %d B %d S\n",count_B, count_S);
count_B=0;
count_S=0;
}
printf("\nYou have tried 20 times, but failed to figure out the number!\n");
printf("\nThe correct number is %d %d %d %d\n",gn[0],gn[1],gn[2],gn[3]);
printf("\nWant to guess again?<y/n>\n");
cin>>p;
if (p=='y')
goto START;
else goto SET;
SET:{}
return 0;
}
#include "stdlib.h"
int main()
{
/*Start by declaring arrays and pointers*/
char guessNumber[4]={1,2,3,4}, userGuess(我用了缩写,不然太麻烦)[4]; int a; char *origin=guessNumber, (无需指针)*user=userGuess;
/*for (a=0;a<4;a++)
{
srand(a);
guessNumber[a]=rand()%9; (我已经帮你改成随机产生了)
printf("%d",guessNumber[a]);
}*/
/*Initialization end*/
/*introduction starts*/ (注释太多,影响阅读)
printf("Hello and welcome to \'GUESS MY NUMBER\'\n");
printf("If you are right on both positon and value, you receive B.\n");
printf("If you are right on value but not position, you receive S.\n");
printf("Good luck!\n"); (规则帮你改的更明确了)
/*introduction end*/
/*Core coding starts*/
int tryCount=0, maxTry=20, temp, i, j, count_B=0, count_S=0;
char *P=origin, *U=user;
for(tryCount=0;tryCount<20;tryCount++)
{
printf("\nWhat's the number:**** ");
/*Loop for user input*/
for(temp=0;temp<4;temp++)
scanf("userGuess[temp]"); (用户数据输入的代码有问题,已经帮你改了)
temp=0; //Set temp back to zero for next loop
for(i=0;i<4;i++) /*i for guessNumber*/
{
for(j=0;j<4;j++) /* j for userGuess*/
{
if((i==j)&&((*P)+i)==((*U)+j)) /*if position and number match*/
count_B++; /*B++*/
else if((i!=j)&&(*(P)+i)==(*(U)+j)) /*If number match*/ (这里有问题,只要*P==*U就可以了,你为什么加了i和j呢? )
count_S++; /*S++*/
}
}
if(count_B==4)
{ (猜对,输出祝贺语句)
//All code goes here....
tryCount=100; break;
}
printf("\n%dB%dS",count_B, count_S);
(输出提示,之后要清零,不然B和S会累加)
}
(我帮你加入了重复猜的代码,不过没有修补漏洞,输入时如果你输入负号(-),或者小数点(.)或者超过4位的数,比如1598546,程序会出问题的)
}
修改后的程序代码:
#include "stdio.h"
#include "stdlib.h"
#include "iostream.h"
int main()
{
int gn[4],ug[4];//gn is short for guessnumber,ug is short for userguess.
char p;
//produce a correct number
printf("Hello and welcome to 'GUESS THE NUMBER'\n\n");
printf("The correct number consists four digits between 0~9 !!!!!!!!!\n\n");
printf("If you are right on both positon and value, you receive a B !!!!!!!!!\n");
printf("If you are right on value but not position, you receive a S !!!!!!!!!\n\n");
printf("Good luck!\n");
START: gn[0]=rand()%10;// randomly produce number between 0~9
do gn[1]=rand()%10;
while (gn[1]==gn[0]);
do gn[2]=rand()%10;
while(gn[2]==gn[1]||gn[2]==gn[0]);
do gn[3]=rand()%10;
while(gn[3]==gn[2]||gn[3]==gn[1]||gn[3]==gn[0]);
/* if the correct number is randomly produced, we should use rand() under stdlib.h
so the program is more complicated*/
//printf("\n%-2d%-2d%-2d%-2d\n\n",gn[0],gn[1],gn[2],gn[3]);
printf("\nIMPORTANT: No digits appear twice in the correct number!!!!!!!!!\n");
int tryCount=0, maxTry=20, i, j,count_B=0, count_S=0;
int n1,n2,n3,n4; //n is short for number
for(tryCount=0;tryCount<20;tryCount++) {
printf("\nThen what's your number:");
SUN: scanf("%1d%1d%1d%1d",&n1,&n2,&n3,&n4);//user input
if (n1*n2*n3*n4<0||n1*n2*n3*n4>6561)
{printf("error!!!!!!!!!");
goto SUN;}
else {}
ug[0]=n1;
ug[1]=n2;
ug[2]=n3;
ug[3]=n4;
printf("\nThe number you input is %d %d %d %d",ug[0],ug[1],ug[2],ug[3]);
printf("\nYou have tried %d times",tryCount+1);
for(i=0;i<4;i++)
{
for(j=0;j<4;j++) {
if((i==j)&&(gn[i]==ug[j]))
count_B++;
else if((i!=j)&&(gn[i]==ug[j]))
count_S++; }
}
if(count_B==4)
{ printf("\n\nYou have guessed the number!\n");
printf("\nCONGRATULATIONS!!!!!!!!!\n\n");
printf("Want to guess again?<y/n>\n");
cin>>p;
if (p=='y')
goto START;
else goto SET;
}
else printf("\n\nThe clue is %d B %d S\n",count_B, count_S);
count_B=0;
count_S=0;
}
printf("\nYou have tried 20 times, but failed to figure out the number!\n");
printf("\nThe correct number is %d %d %d %d\n",gn[0],gn[1],gn[2],gn[3]);
printf("\nWant to guess again?<y/n>\n");
cin>>p;
if (p=='y')
goto START;
else goto SET;
SET:{}
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
AiPPT
2024-09-19 广告
2024-09-19 广告
随着AI技术的飞速发展,如今市面上涌现了许多实用易操作的AI生成工具1、简介:AiPPT: 这款AI工具智能理解用户输入的主题,提供“AI智能生成”和“导入本地大纲”的选项,生成的PPT内容丰富多样,可自由编辑和添加元素,图表类型包括柱状图...
点击进入详情页
本回答由AiPPT提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询