2010-07-10 4 views
0
#include<iostream> 
#include<stdio.h> 

using namespace std; 

void student_array(); 
void query(); 
void show_arr(); 
void student_entry(); 


struct student 
{ 
    char name[80]; 
    char f_name[80]; 
    char the_class[3]; 
    char grade[2]; 
}; 

student std_arr[10]; 
char action; 
int count; 
int main() 
{ 
    cout<<"add 1st student"<<endl; 
    student_entry(); 
} 
void student_entry() 
{ 
    if (count == 10) 
    { 
     cout<<"Memory Full!"; 
     //break; 
    } 
    cout<<"enter name of student"<<endl; 
    cin>>std_arr[count].name; 
    //cout<<std_arr[count].name; 
    cout<<"enter student's father's name"<<endl; 
    cin>>std_arr[count].f_name; 
    cout<<"enter the class of student"<<endl; 
    cin>>std_arr[count].the_class; 
    cout<<"enter the grade of student"<<endl; 
    cin>>std_arr[count].grade; 
    query(); 
    count++; 

} 

void query() 
{ 
    cout<<"what do you want to do?"<<endl; 
    cout<<"press a to add"<<endl; 
    cout<<"press s to show"<<endl; 
    cout<<"press q to quit"<<endl; 
    cin>>action; 
    //cout<<action; 
    switch (action) 
    { 
     case 'a': 
     { 
      student_entry(); 
      break; 
     } 
     case 's': 
     { 
      show_arr(); 
      break; 
     } 
     default: 
     { 
      cout<<"wrong entry"; 
      query(); 
      break; 
     } 
    } 
} 

void show_arr() 
{ 
    for (int i = 0; i < count; i++) 
    { 
     cout<<endl<<"Student No."<<count<<endl; 
     cout<<"Name: "<<std_arr[i].name<<endl; 
     cout<<"Father's Name: "<<std_arr[i].f_name<<endl; 
     cout<<"Class: "<<std_arr[i].the_class<<endl; 
     cout<<"Grade Achieved: "<<std_arr[i].grade<<endl; 
    } 
} 

스위치 구조가 s의 경우 show_arr() 함수를 호출하지 않습니다.C++의 스위치 구조가 작동하지 않습니다.

+0

여기서 q는 스위치를 처리하는 부분입니까? –

답변

1

query을 호출하기 전에 변수 count을 증가시켜야합니다. 그렇지 않으면 for 루프가 실행되지 않습니다. 한 학생이 이미 배열에 추가되었으므로 쿼리를하기 전에이 변수를 증가시키는 것이 좋습니다.

1

count은 항상 0입니다.

처음으로 main에서 student_entry으로 전화 할 때 count 값을 증가시키기 전에 query을 호출하려고합니다. 이제 a을 입력하면 다음 학생의 데이터는 str_arr[0]에 입력되고 querycount을 업데이트하지 않고 호출됩니다.

따라서 's'을 입력하고 show_arr 함수를 호출 할 때마다 count의 값은 0이됩니다.

student_entry 메소드에서 쿼리를 호출하지 말고 count를 증가시키고 그 결과를 리턴하십시오. 주 기능의 while(true) 루프에서 쿼리를 수행하고 입력 된 데이터를 기준으로 student_entry 또는 show_data 또는 break을 호출하십시오.

관련 문제