gpt4 book ai didi

c - 程序在分成 2 个程序时可以工作,但在同一个程序中使用时则不行

转载 作者:行者123 更新时间:2023-11-30 20:55:30 26 4
gpt4 key购买 nike

我有一个项目,它由使用 switch-case 的菜单和子程序组成。

我的问题是菜单号 3,它在我的程序中无法正常工作,但是当我将其分离到另一个程序中时,它可以正常工作。

这是我的CPP的链接http://www.mediafire.com/download/4tj6slw1edrdu2h/My+program.rar

#include <conio.h>
#include <stdio.h>
#include <windows.h>
#include <math.h>
#include <string.h>
void gotoxy(short x, short y) {COORD pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
int main ()
{
//BORDER and Effects Design :)
int bx=0, by=0, choice,intctr=1; double doublegrade=0, doublesum=0;
int reader1,letter;int counterf,reader=0;
int seconds,second1,hour,minute;//(Seconds)
float x=-5;float y=-45.23;char name[25];char iname[30];
//intctr,doublegrademdoublesum=(average)
//int reader1,letter,iname(Falling Asterisk)
//x,y,char,MATH
bx=20;by=7;
while(bx<=50 && by<=31)
{
gotoxy(bx,by);
system("COLOR 2");
printf("%c", 3);
bx++;
if(bx==50)
{bx=20; by++;}
}
//Loob Ng Menu (lalagyan ng Spaces!!) 21-49
bx=21; by=8;
while(bx<=48 && by<=30)
{
gotoxy(bx,by);
printf(" ");
system("COLOR 4");
bx++;
if(bx==49)
{bx=21;by++;}
}
//Print ng MENU
while(choice!=5)
{
bx=33; by=10;
gotoxy(bx, by);
printf("MENU");bx=26;by=12;
gotoxy(bx,by);
printf("(1) AVERAGE");by++;
gotoxy(bx,by);
printf("(2) MATH");by++;
gotoxy(bx,by);
printf("(3) FALLING ASTERISK");by++;
gotoxy(bx,by);
printf("(4) SECONDS");by++;
gotoxy(bx,by);
printf("(5) EXIT");bx=32; by++;
gotoxy(bx,by);
printf("CHOICE: ");bx=39;
gotoxy(bx,by);
scanf("%d", &choice);
system ("CLS");
switch (choice)
{
//case1XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
case 1:bx=20; by=7;
while(bx<=50 && by<=31)
{
gotoxy(bx,by);
system("COLOR 5");
printf("%c", 5);
bx++;
if(bx==50)
{bx=20; by++;}
}
bx=21; by=8;
while(bx<=48 && by<=30)
{
gotoxy(bx,by);
printf(" ");
bx++;
if(bx==49)
{bx=21;by++;}
}
bx=26; by=14;
while(intctr<6)
{
gotoxy(bx,by);
printf("Enter grade %d: ", intctr);
gotoxy(41,by);
scanf("%lf", &doublegrade);
doublesum= doublesum + doublegrade;
intctr++;by++;
}
gotoxy(bx,by);
printf("Average:%.2lf", doublesum/5);
getch();
bx=21; by=8;
while(bx<=48 && by<=30)
{
gotoxy(bx,by);
printf(" ");
system("COLOR E");
bx++;
if(bx==49)
{bx=21;by++;}
}
getch(); break;
//case2XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
case 2: bx=20; by=7;
while(bx<=50 && by<=31)
{
gotoxy(bx,by);
system("COLOR 6");
printf("%c", 2);
bx++;
if(bx==50)
{bx=20; by++;}
}
bx=21; by=8;
while(bx<=48 && by<=30)
{
gotoxy(bx,by);
printf(" ");
bx++;
if(bx==49)
{bx=21;by++;}
}
bx=25;by=16;
gotoxy(bx,by);
printf("Mathematical Funtions\n");by++;
gotoxy(bx,by);
printf("abs= %d\n", abs(x));by++;
gotoxy(bx,by);
printf("fabs= %.2f\n", fabs(y)); by++;
gotoxy(bx,by);
printf("ceil= %.2f\n", ceil(y)); by++;
gotoxy(bx,by);
printf("floor= %.2f\n", floor (y));by++;
gotoxy(bx,by);
printf("ceil= %.2f\n", ceil(45.23));by++;
gotoxy(bx,by);
printf("floor= %.2f\n", floor(45.23));by++;
y=pow(2,3);
gotoxy(bx,by);
printf("pow= %.2f\n",y);by++;
gotoxy(bx,by);
printf("sqrt= %.2f\n", sqrt(4));
bx=21; by=8;
while(bx<=48 && by<=30)
{
gotoxy(bx,by);
printf(" ");
system("COLOR B");
bx++;
if(bx==49)
{bx=21;by++;}
}
getch ();break;
//Case3XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
case 3: system ("CLS");
bx=20; by=7;
while(bx<=50 && by<=31)
{
gotoxy(bx,by);
system("COLOR 8");
printf("%c", 1);
bx++;
if(bx==50)
{bx=20; by++;}
}bx=21; by=8;
while(bx<=48 && by<=30)
{
gotoxy(bx,by);
printf(" ");
bx++;
if(bx==49)
{bx=21;by++;}
} bx=27;by=12;
gotoxy(bx,by);
printf("FALLING ASTERISK");
by=14;bx=23;gotoxy(bx,by);
printf("Input name: ");bx=34;by=14;
fgets(iname,30,stdin);bx=32;by=16;
gotoxy(bx,by);
printf("%s", iname);
counterf=strlen(iname);
bx=32;by=16;
while(reader<counterf)
{
gotoxy(bx,by);
printf("*");
Sleep (500);
gotoxy(bx,by);
Sleep (500);
printf(" ");
by++;reader1++;
if(reader1==counterf)
{reader++;gotoxy(bx,by);printf("%c",iname[letter]);
letter++;by=16;bx++;reader1=0;}
}
bx=21; by=8;
while(bx<=48 && by<=30)
{
gotoxy(bx,by);
printf(" ");
system("COLOR D");
bx++;
if(bx==49)
{bx=21;by++;}
}
getch();break;
//CASE4XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
case 4: system ("CLS");
bx=20;by=7;
while(bx<=50 && by<=31)
{
gotoxy(bx,by);
system("COLOR A");
printf("%c", 219);
bx++;
if(bx==50)
{bx=20; by++;}
}
bx=21; by=8;
while(bx<=48 && by<=30)
{
gotoxy(bx,by);
printf(" ");
system("COLOR 4");
bx++;
if(bx==49)
{bx=21;by++;}
}
bx=27;by=16;
gotoxy(bx,by);
printf("ENTER SECONDS: ");
scanf("%d",&seconds);
hour=seconds/3600;
minute=(seconds%3600)/60;
second1=seconds%60;bx=23,by=18;
gotoxy(bx,by);
printf("Hour:%d Minute:%d Seconds:%d", hour,minute,second1);
getch();
bx=21; by=8;
while(bx<=48 && by<=30)
{
gotoxy(bx,by);
printf(" ");
system("COLOR B");
bx++;
if(bx==49)
{bx=21;by++;}
}
getch();break;
case 5:printf("BYEBYE");
getch();
return 0;
default:
gotoxy(27,19);printf("Invalid Number!");
}//switch
}//MAIN WHILE
getch();
return 0;
}

当我分离CASE 3时。(我没有改变任何东西)

#include <conio.h>
#include <stdio.h>
#include <windows.h>
void gotoxy(short x, short y) {
COORD pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
int main ()
{ int bx=20, by=7, choice,intctr=1;
int reader1,letter;
char iname[200];
system ("CLS");
while(bx<=50 && by<=31)
{
gotoxy(bx,by);
system("COLOR 8");
printf("%c", 1);
bx++;
if(bx==50)
{bx=20; by++;}
}bx=21; by=8;
while(bx<=48 && by<=30)
{
gotoxy(bx,by);
printf(" ");
bx++;
if(bx==49)
{bx=21;by++;}
} bx=27;by=12;
gotoxy(bx,by);
printf("FALLING ASTERISK");
int counterf,reader=1;
by=14;bx=23;gotoxy(bx,by);
printf("Input name: ");bx=34;by=14;
fgets(iname,200,stdin);bx=32;by=16;
gotoxy(bx,by);
printf("%s", iname);
counterf=strlen(iname);
bx=32;by=16;
while(reader<counterf)
{
gotoxy(bx,by);
printf("*");
Sleep (500);
gotoxy(bx,by);
Sleep (500);
printf(" ");
by++;reader1++;
if(reader1==counterf)
{reader++;gotoxy(bx,by);printf("%c",iname[letter]);
letter++;by=16;bx++;reader1=0;}
}

getch ();
return 0;
}

最佳答案

您的 fgets 不会等待完整程序中的输入,因为它在 stdin 中看到换行符 ('\n') (之前的 scanf 留下的)并愉快地消费它。

解决方案是使用以下方法清除stdin(标准输入流):

int c; /* At the start of main */

while((c = getchar()) != '\n' && c != EOF); /* Consume all characters until \n or EOF */

就在有问题的fgets之前。

关于c - 程序在分成 2 个程序时可以工作,但在同一个程序中使用时则不行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32666457/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com