編譯器課程設計
Ⅰ c++課程設計一般都會出現哪些錯誤,錯誤提示都是什麼
編譯器的部分工作是尋找程序代碼中的錯誤。編譯器不能查出程序的意義是否正確, 但它可以查出程序形式上的錯誤。下面是編譯器能查出的最普遍的一些錯誤。語法錯誤。程序員犯了 C++ 語言中的語法錯誤。下面代碼段說明常見的語法錯誤;每個注釋描述下一行的錯誤。// error: missing ')' in parameter list for main int main ( { // error: used colon, not a semicolon after endl std::cout << "Read each file." << std::endl: // error: missing quotes around string literal std::cout << Update master. << std::endl; // ok: no errors on this line std::cout << "Write new master." <<std::endl; // error: missing ';' on return statement return 0 }
類型錯誤。C++ 中每個數據項都有其相關聯的類型。例如,值 10 是一個整數。用雙引號標注起來的單詞「hello」是字元串字面值。類型錯誤的一個實例是傳遞了字元串字面值給應該得到整型參數的函數。聲明錯誤。C++ 程序中使用的每個名字必須在使用之前聲明。沒有聲明名字通常會導致錯誤信息。最常見的兩種聲明錯誤,是從標准庫中訪問名字時忘記使用「std::」,以及由於疏忽而拼錯標識符名:#include <iostream> int main() { int v1, v2; std::cin >> v >> v2; // error: uses " v "not" v1" // cout not defined, should be std::cout cout << v1 + v2 << std::endl; return 0; } 錯誤信息包含行號和編譯器對我們所犯錯誤的簡要描述。按錯誤報告的順序改正錯誤是個好習慣,通常一個錯誤可能會產生一連串的影響,並導致編譯器報告比實際多得多的錯誤。最好在每次修改後或最多改正了一些顯而易見的錯誤後,就重新編譯代碼。這個循環就是眾所周知的編輯—編譯—調試。
Ⅱ c++課程設計源代碼
c++課程設計 學生管理系統源代碼
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
typedef struct stu
{
char name[20];
long int number;
int snum;
char sex[20];
char add[30] ;
char time[20];
char tel[20];
struct stu *next;
}stu,*student;
int init(student &s);
void insert(student &s);
void print(student &s);
int delete(student &s);
void find (student &s);
void findname(student &s);
void findnum(student &s);
void modifay(student &s);
void putfile(student &s);
void getfile(student &s);
void main()
{
student s;
init(s);
cout<<"歡迎使用學生管理系統."<<endl;
cout<<" "<<endl;
cout<<endl;
while(1)
{
int i;
mainint:
cout<<"請選擇相關操作:"<<endl<<"1.建立學生資料文件."<<endl<<"2.瀏覽."<<endl<<"3.刪除."<<endl;
cout<<"4.查詢."<<endl<<"5.修改."<<endl<<"6.打開學生文件."<<endl<<"7.退出管理系統." <<endl<<"請選擇:";
cin>>i;
if(i<0||i>7)
{
cout<<"輸入了錯誤的數字,Again!"<<endl<<endl;
goto mainint;
}
switch(i)
{
case 1: insert(s);break;
case 2: print(s);
putfile(s);
break;
case 3: ldelete(s);break;
case 4: find(s);break;
case 5: modifay(s);
break;
case 6: getfile(s);break;
case 7: exit(0);
}
}
}
int init(student &s)
{
s=(student)malloc(sizeof(stu));
if (s)
{
s->next=NULL;
return 0;
}
else return -1;
}
void insert(student &s) //insert
{
cout<<endl;
student p,q;
p=(student)malloc(sizeof(stu));
cout<<"請輸入學生信息:"<<endl;
cout<<"姓名:";
cin>>p->name;
cout<<endl;
cout<<"學號:";
cin>>p->number;
cout<<endl;
cout<<"性別:";
cin>>p->sex;
cout<<endl;
cout<<"家庭住址:" ;
cin>>p->add;
cout<<endl;
cout<<"出生年月:" ;
cin>>p->time;
cout<<endl;
cout<<"宿舍號:";
cin>>p->snum;
cout<<endl;
cout<<"宿舍號碼:";
cin>>p->tel;
q=s;
while(!(q->next==NULL)&&(q->next->number<p->number))
q=q->next;
p->next=q->next;
q->next=p;
//if()p->next=NULL;
//free(p);
}
int ldelete(student &s) //delete
{
cout<<endl;
student p,a;
p=s ;
cout<<"請輸入刪除的學號:";
long int number;
cin>>number;
while(p)
{ if (p->number==number)
{
a->next=p->next;
free(p); return(0);}
else
{ a=p;
p=p->next;} }
cout<<"沒有找到你要刪除的選項!"<<endl<<endl;return(-1);
}
void print(student &s)
{
int a;
a=0;
student p;
p=s->next;
cout<<"姓名 "<<"學號 " <<"性別 "<<"家庭住址 "<<"出生年月 "<<"宿舍號"<<"宿舍號碼"<<endl;
while(p)
{
a++;
cout<<p->name<<" "<<p->number<<" "<<p->sex<<" "<<p->add<<" "<<p->time<<" "<<p->snum<<" "<<p->tel<<" "<<endl;
p=p->next;
}
cout<<endl;
if (a==0)
cout<<"還沒有學生信息!"<<endl<<endl ;
}
void find (student &s) //find
{
findl:
cout<<"請選擇查找方法:"<<endl<<"1.按姓名查找."<<endl<<"2.按學號查找."<<endl;
cout<<"請選擇:";
int k;
cin>>k;
if(k<0||k>3)
{
cout<<"輸了入錯誤數字,Again!"<<endl<<endl;
goto findl;
}
switch(k)
{
case 1: findname(s);break;
case 2: findnum(s);break;
}
}
void findname(student &s) //find by name
{
student p;
p=s->next;
cout<<"請輸入姓名:";
char name[20];
int j;
j=0;
cin>>name;
cout<<"你要查找的資料是:"<<endl;
cout<<"姓名 "<<"學號 " <<"性別 "<<"家庭住址 "<<"出生日期 "<<"宿舍號 "<<"宿舍號碼"<<endl;
while(p)
{
if (strcmp(p->name,name)==0)
{
cout<<p->name<<" "<<p->number<<" "<<p->sex<<" "<<p->add<<" "<<p->time<<" "<<p->snum<<" "<<p->tel<<" "<<endl;
j++;
}
p=p->next;
}
cout<<endl;
if(j==0)
cout<<"對不起,沒找到你要的信息!"<<endl<<endl ;
}
void findnum(student &s) //find by number
{
student p;
p=s->next;
cout<<"請輸入學號:";
long int number;
int j;
j=0;
cin>>number;
cout<<"你要查找的資料是:"<<endl;
cout<<"姓名 "<<"學號 "<<"性別 "<<"家庭住址 "<<"出生日期 "<<"宿舍號 "<<"宿舍電話"<<endl;
while(p)
{
if (p->number==number)
{
cout<<p->name<<" "<<p->number<<" "<<p->sex<<" "<<p->add<<" "<<p->time<<" "<<p->snum<<" "<<p->tel<<" "<<endl;
j++;
}
p=p->next;
}
cout<<endl;
if(j==0)
cout<<"對不起,沒找到你要的信息"<<endl<<endl ;
}
void modifay(student &s) //modifay
{
student q,p,l,m;
int j;
j=0;
q=s->next;
l=s;
m=s;
cout<<"請輸入要修改的學號:" ;
long int num;
cin>>num;
cout<<"姓名 "<<"學號 "<<"性別 "<<"家庭住址 "<<"入學時間 "<<"宿舍號 "<<"電話號碼"<<endl;
while(q)
{
if (q->number==num)
{
cout<<"你要修改的信息是:"<<endl;
cout<<q->name<<" "<<q->number<<" "<<q->sex<<" "<<q->add<<" "<<q->time<<" "<<q->snum<<" "<<q->tel<<" "<<endl;
j++;
p=(student)malloc(sizeof(stu));
cout<<"請輸入新的學生信息:"<<endl;
cout<<"姓名:";
cin>>p->name;
cout<<endl;
cout<<"學號:";
cin>>p->number;
cout<<endl;
cout<<"年齡:";
cin>>p->age;
cout<<endl;
cout<<"性別:";
cin>>p->sex;
cout<<endl;
cout<<"家庭住址:" ;
cin>>p->add;
cout<<endl;
cout<<"入學時間:" ;
cin>>p->time;
cout<<endl;
cout<<"電話號碼:";
cin>>p->tel;
l->next=q->next;
free(q);
goto tt;
}
else
{
l=q;
q=q->next;
}
}
cout<<endl;
if (j==0) cout<<"沒找到你要的數據!"<<endl<<endl ;
tt:
while(!(m->next==NULL)&&(m->next->number<p->number))
m=m->next;
p->next=m->next;
m->next=p;
}
void putfile(student &s)
{
student p;
p=s->next;
FILE *fp;
if((fp=fopen("information.txt","w"))==NULL)
{
cout<<"打不開文件"<<endl<<endl;
exit(0);
}
while(p)
{
if(fwrite(p,sizeof(struct stu),1,fp)!=1 )
{
cout<<"文件寫入錯誤"<<endl<<endl;
return;
}
p=p->next;
}
fclose(fp) ;
}
void getfile(student &s)
{
student p,q;
q=s;
FILE *fp1 ;
if((fp1=fopen("information.txt","r"))==NULL)
{
cout<<"打不開文件"<<endl;
exit(0);
}
cout<<"姓名 "<<"學號 "<<"性別 "<<"家庭住址 "<<"入學時間 "<<"宿舍號 "<<"電話號碼"<<endl;
p=(student)malloc(sizeof(stu));
while(fread(p,sizeof(struct stu),1,fp1)!=0)
{
cout<<p->name<<" "<<p->number<<" "<<p->sex<<" "<<p->add<<" "<<p->time<<" "<<p->age<<" "<<p->tel<<endl;
while(!(q->next==NULL)&&(q->next->number<p->number))
q=q->next;
p->next=q->next;
q->next=p;
p=(student)malloc(sizeof(stu));
}
fclose(fp1);
cout<<endl;
}