views:

69

answers:

2
#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;
    }
}

My switch structure is not calling the show_arr() function on the case of s.

+1  A: 

You need to increment the variable count before calling query otherwise the for loop is not executed. Since one student is already added to the array it makes sense to increment this variable before doing query.

Naveen
+1  A: 

count is always zero.

First time when you call student_entry from main, you are calling query before incrementing the value of count. Now if you enter a, the next student's data will be entered at str_arr[0] itself and query is called without updating count.

Hence whenever you enter 's' and the function show_arr is called, value of count would be zero.

Don't call query from the student_entry method, just increment count and return from it. Do querying in a while(true) loop in the main function and call student_entry or show_data or just break out based on entered data.

Amarghosh