1.11M Members

array output wrong

 
0
 

i insert the different id , but the program out with pointer value
anyone know why ???
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#include<iostream>
#include<string>
#define MAX 6
#define size 10

using namespace std;

struct student {
string name;
int id;
string prog;
double cgpa;

};


class stinfo {
    private:
        student data[size];
        int last;
    public:
        stinfo();
        void add(string name, int id, string prog, double cgpa);
        void printAll(int count);
        void selectionSort(int count);


};


int min(int a[], int pos){
    int min=99999;
    int i;
    int c;
    for(i =pos; i<MAX;i++){
        if(a[i]<min){
            min=a[i];
            c=i;
        }
    }
    return c;
}


stinfo :: stinfo()
{
    last = 0 ;
}



void stinfo::add(string name, int id,string prog, double cgpa)
{
    if (last == size) {
    return ;
}
data[last].name = name;
data[last].id = id;
data[last].prog = prog;
data[last].cgpa = cgpa;
++last;
}           // all infomation are in data 


void stinfo::selectionSort(int count) 
{
    int A[size];
    int i,j, temp;
    for (int p=0; p<last; p++)
    {
        A[p]= data[p].id;
    }



     for (i=0; i<MAX; i++)
     {
         j = min(A,i);
         if(A[j]<A[i])
         {
            temp = A[j];
            A[j] = A[i];
            A[i] = temp;
         }       
    }
        for (int q=0; q<count; q++)
        cout<<A[q]<<endl;
}


void stinfo::printAll(int count)
{
    if (last == size){
        return ;
    }else{

        for (int i =0; i<count; i++)
        {
            cout<<data[i].name<<endl
                <<data[i].id<<endl
                <<data[i].prog<<endl
                <<data[i].cgpa<<endl;
        }

    }
}



int main()
{

    char ans;
    string name1,prog1;
    int id1;
    double cgpa1;
    int count = 0;

    stinfo st;

    do{
    cout <<">>>>>  STUDENT INFOMATION RECORDING SYSTEM  <<<<< "<<endl;

    cout <<"A.) Insert NEW Student Record."<<endl
         <<"B.) print all Student Record."<<endl;


    cout <<"Please Choice ONE Option : ";
    cin >>ans;
    cout<<endl;

    switch ( ans )
    {
    case 'a':
    case 'A':
        cout <<endl;
        cout <<">>>>> INSERT NEW STUDENT INFORMATION <<<<<"<<endl;

        cout <<"NAME : ";
        cin >>name1;

        cout <<"ID: ";
        cin >>id1;

        cout <<"PROGRAMME : ";
        cin >>prog1;

        cout <<"CGPA: ";
        cin >>cgpa1;

        st.add ( name1,id1,prog1,cgpa1 );

        cout <<endl
             <<"INSERT COMPLETE... "<<endl
             <<endl;
        count ++;
        break;

    case 'b':
                                // sort at here
        st.printAll(count);
        break;

    case 'c':

    st.selectionSort(count);



    break;

        default :
        cout <<"Please Try Again..."<<endl
             <<endl;
    }

    }while ( toupper(ans) !='F' ) ;

        cout <<"EXIT The Program..."<<endl
             <<endl;

    system("pause");
    return 0;

}
 
1
 

You will get a much better response if you improve your questions. Here is how you can improve your questions:

1) Show us the code. You already do this. Good.
2) Tell us exactly what input you type in.
3) Tell us what you saw happen, and what you think should have happened.

 
0
 
  1. system("pause");

Avoid using system calls in your program.
system("PAUSE") is certainly less than ideal. Using a call to system creates a subprocess, which on windows is fairly expensive and in any case not terribly cheap on any operating system.

You may check out the reasons, in details here.

 
0
 

Avoid using system calls in your program.

Too vague, and Walt's rationale (from the link) is largely moot:

  1. "It's not portable." The system() function is inherently non-portable because its argument is dependent on available system console commands. Though it's wise to acknowledge this when choosing to use it, I wouldn't claim portability as a reason to avoid something unless there's an equally good and portable solution. Walt's stated solution is the portable way to simulate the behavior of system("PAUSE"), but it's not equally good because you have to consider extraneous characters in the input stream.

  2. "It's a very expensive and resource heavy function call." That's easily the dumbest reason for avoiding system("PAUSE") if you take half a second to think about what system("PAUSE") does. As an argument against system() in general it's appropriate, but not for system("PAUSE") specifically.

  3. "You must include a header you probably don't need: stdlib.h or cstdlib" Oh no, you must include a standard header that contains commonly used functions and types. The horror! ;) Who is Walt to say that the header isn't needed for other things? Further, if you choose to use the system() function then that header is needed anyway, so it's not really an argument against system(), it's just fluff to make the short list longer.

I'd say avoid using the system() function in favor of a system call that you know won't execute a malicious program of the same name. Walt doesn't address this at all, but it's by far the most persuasive argument against the system() funciton. The problem with stuff like system("PAUSE") is I could easily write a malicious program, call it PAUSE.exe, and put it in a place that ensures your code will run it. Your code will run it with whatever permissions the user running your program has, which could be devastating if you have root permissions.

The problem with system() is that it's inherently insecure. That alone is the only reason you need to avoid it.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article