【发布时间】:2016-08-26 13:15:14
【问题描述】:
我很抱歉在这件事上缺乏知识,但这对我来说非常重要,所以至少我会试一试。 我在 Windows 7 Ultimate 上使用 Visual Studio 2012 express, 我编写了一些非常基本的愚蠢程序来练习 C 语言中的结构。不幸的是,我写的其中一种方法有一个错误的停止条件,它进入了一个无限循环,更糟糕的是它超出了给定的数组边界。我将在下面添加代码,但代码不是我主要关心的问题,我已经设法修复它,我只想知道我是否会损坏我的系统或我的个人文件(视频、照片、办公文件等)给定方法,因为当我尝试运行这段代码时,发生了一件可怕的事情,我的电脑开始每 0.5 秒无休止地“哔”一次,我无法阻止它,程序处于无限循环,我管理仅使用 Ctrl + Alt + Del 来停止这种“乐趣”。我之前编写过多次崩溃的代码,但我不确定我的电脑第一次发出哔哔声。我希望专家可以帮助我解决这个问题。非常感谢。
导致所有问题的无限循环方法是“printArrHero”:
#include<stdio.h>
#include<string.h>
struct Date{
int day, month, year;
} typedef date_s;
struct Superhero{
char name[30];
double power, speed;
date_s birthday;
} typedef sp_s;
void printInfo(sp_s hero){
printf("Super hero info:\nName: [%s]\n" , hero.name);
printf("Power: [%.2lf]\n" , hero.power);
printf("Speed: [%.2lf]\n" , hero.speed);
printf("Birthday: [%d/%d/%d]\n\n" , hero.birthday.day, hero.birthday.month, hero.birthday.year );
}
void addHero (sp_s arr[], int *k, sp_s newHero){
arr[*k] = newHero;
(*k)++;
printf("\nSuccess! a new superhero is added.\n");
}
void printArrHero (sp_s arr[]){
int j=0;
while ( arr[j] != NULL ){
printf("[#%d]>>>>>>>>>>>>>>>>>>\n" , j+1);
printInfo( arr[j] );
j++;
}
}
void main(){
sp_s myHeros[100];
sp_s newHero;
int i = -1;
int k = 0;
while (i != 0){ // menu loop
printf("Welcome to my game!\n");
printf("To add a hero press 1\n");
printf("To find you're strongest hero press 2\n");
printf("To find you're fastest hero press 3\n");
printf("To see all of you're heroes list 4\n");
printf("To exit press 0\n");
scanf("%d" , &i);
switch (i){
case 0:
break;
case 1:
printf("Please enter the #%d hero characteristics:\nName: " , k+1);
scanf("%s" , &newHero.name);
printf("Power: ");
scanf("%lf" , &newHero.power);
printf("Speed: ");
scanf("%lf" , &newHero.speed);
printf("Birth date [xx/xx/xx](format): ");
scanf("%d/%d/%d" , &newHero.birthday.day, &newHero.birthday.month, &newHero.birthday.year);
addHero(myHeros, &k, newHero); // now when I collecten all the info needed to make a new hero lets send it to the func tht puts it in the #k place in the array
break;
case 2:
break;
case 3:
break;
case 4:
printArrHero(myHeros);
break;
} // END of switch-case
} // while menu loop
} // main
【问题讨论】:
-
好吧,
struct Something { /* something */ } typedef Something;不正确,不会编译。所以这是你的程序有问题 -
亲爱的“ForceBru”它编译得很好我的朋友,否则它不会运行。
标签: c visual-studio visual-studio-2012 structure infinite-loop