c++ sub menu

elektro

Well-known member
  • Apr 18, 2011
    6,856
    9,352
    113
    Tropical Island Of Sri Lanka
    මචංලා මට පහල තියෙන ප්‍රෝගෑම් එක කරගන්න පොඩි සප් එකක් ඕන..ඕකේ තියෙන විදියට කොහොමද ස්විච් එකකින් සබ් මෙනු එකක් හදන්නේ :( අනික් දේ View a student’s data using the ID වගේ දෙයක් කරන්නේ කොහොමද? ප්ලිස් මචංලා පොඩි සප් එකක් දෙන්නෝ :):)

    Use C++ program language to develop a menu driven program which is capable of
    answering inquiries made related student data as described below.

    The program should display a menu that enables the users to choose whether they want to view all students’records or view only the records of a specific student by the student’s id or to view the highest marks and the lowest marks.
    See sample below.

    ………… MENU ………………

    1. View all studentdata
    2. View a student’s data using the ID
    3. Show the highest and the lowest final marks

    Sample output:-
    Please enter your choice: 1

    |StudentID | TMA1 | TMA2| BCAT | Final
    | ==================================================
    |1232 | 10 | 23 | 45 | 56 |
    |2343 | 45 | 43 | 24 | 78 |
    |2343 | 34 | 45 | 45 | 45 |
    |3423 | 67 | 6 | 65 | 56 |

    Note: These data should be stored in a two-dimensional array
     

    Tom Riddle

    Member
    Aug 31, 2007
    1,833
    196
    0
    මචංලා මට පහල තියෙන ප්‍රෝගෑම් එක කරගන්න පොඩි සප් එකක් ඕන..ඕකේ තියෙන විදියට කොහොමද ස්විච් එකකින් සබ් මෙනු එකක් හදන්නේ :( අනික් දේ View a student’s data using the ID වගේ දෙයක් කරන්නේ කොහොමද? ප්ලිස් මචංලා පොඩි සප් එකක් දෙන්නෝ :):)

    Use C++ program language to develop a menu driven program which is capable of
    answering inquiries made related student data as described below.

    The program should display a menu that enables the users to choose whether they want to view all students’records or view only the records of a specific student by the student’s id or to view the highest marks and the lowest marks.
    See sample below.

    ………… MENU ………………

    1. View all studentdata
    2. View a student’s data using the ID
    3. Show the highest and the lowest final marks

    Sample output:-
    Please enter your choice: 1

    |StudentID | TMA1 | TMA2| BCAT | Final
    | ==================================================
    |1232 | 10 | 23 | 45 | 56 |
    |2343 | 45 | 43 | 24 | 78 |
    |2343 | 34 | 45 | 45 | 45 |
    |3423 | 67 | 6 | 65 | 56 |

    Note: These data should be stored in a two-dimensional array


    Code:
    int n = 0, StudentID = 0;
    
    int AllStudentData[3][5] = { {1232, 10, 23, 45, 56}, {2343, 45, 43, 24, 78}, {2343, 34, 45, 45, 45}, {3423, 67, 6, 65, 56}};
    
    cin >> n;
    
    switch (n)
    {
    case 1:
        PrintAllStudentData();
        break;
    case 2:
        PrintStudentData(StudentID)
        break;
    case 3:
        PrintLowestAndHighestMarks()
        break;
    default:
       cout << "Error! " << endl;
       break;
    }
    
    PrintAllStudentData()
    {
        cout << "|StudentID | TMA1 | TMA2| BCAT | Final " << endl;
        cout << "| ================================================== ";
    
        for ( int i = 0; i < 3; ++i)
        {
            for (int j = 0; j < 5; ++j)
            {
                  cout << AllStudentData[i][j];
                  cout << "|";
             }
            cout << endl;
         }
    }



    oya code gedi pitin copy karala compile wennewath run wennewath nehe honde. nikan eka digata gahagena giye. idea ekak enna athine. ithiri tika karaganna.
     

    Arkham Knight

    Member
    Mar 14, 2014
    1,627
    89
    0
    :) ඔන්න ගැහුවා හරියටම තියෙනවා නමුත් මේකේ තියෙන්නේ ROWS 2 ක් විතරයි.
    උබ ඉතුරු row ටික ගහගන්න එතකොට මේක වැඩ කරන විදියත්, code එකේ basic principle එකත් බලාගන්න පුලුවන්.

    සාරාංශය වශයෙන් කිව්වොත්.
    මේකේ Table එක Align කරන්න setw() function එක ඕන වෙනවා ඒක නැතුව table එක align කරන්න බෑ හරියට, ඒකට iomanip කියන library එකට access වෙන්න වෙනවා (මේක header file එකක්)

    ඊට පස්සේ ID එක ගහලා Student ගේ marks බලන්න ඕනේ නම් IF conditional statmenet
    එක යොදාගන්න පුලුවන්.

    අවසානයේදි High,Low mark එක ගන්න නම් bubble sort එක යොදාගන්න ඕනේ. මේක කරන්න කලින් final mark ටික ගන්න වෙනම single dimension array එකකට ඊට පස්සේ ඒ array එක sort කරන්න bubble sort
    algorithm එක හරහා (මම ඒක ගහලා ඇති අවසානයේදි) මතක ඇතුව sorting එකේදි low value එක මුලටත්, high value එක අගටත් දාගන්න.

    ඊට පස්සේ array එකේ 0 space එකට access වෙලා low
    mark එකත්
    array එකේ length - 1 (අවසාන space එක) space එකට access වෙලා high mark එකත් ගන්න පුලුවන්, මේක හරි සරලයි.

    මෙතනට මම code එක upload කරා. copy කරද්දි toggle wrap එක off කරලා copy කරන්න නැත්නම් VS එකේ code එක messy විදියට paste වෙයි paste කරන කොට.

    http://pastebin.com/DfBBRAMi#

    ඒ වගේම Elakiri එකේත් ගහලා ඇති. code එක අනිවාර්යෙන්ම වැඩ AND tested Visual Studio එකේ.


    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    /*
    1. View all studentdata
    2. View a student’s data using the ID
    3. Show the highest and the lowest final marks 
    */
    
    void allStudentData();
    void idStudent();
    void showHighLowMarks();
    void menu();
    
    int main()
    {
        menu();
    }
    
    int sR[3][5];
    void menu()
    {
        //|StudentID 0 | TMA1 1| TMA2 2| BCAT 3| Final 4
        //student column names
        sR[0][0] = 0;
        sR[0][1] = 1;
        sR[0][2] = 2;
        sR[0][3] = 3;
        sR[0][4] = 4;
    
        //student One
        sR[1][0] = 1232;
        sR[1][1] = 10;
        sR[1][2] = 23;
        sR[1][3] = 45;
        sR[1][4] = 56;//final mark
    
        //student two
        sR[2][0] = 2343;
        sR[2][1] = 45;
        sR[2][2] = 43;
        sR[2][3] = 24;
        sR[2][4] = 78;//final mark
    
    
        cout << "----------  Menu -----------" << endl;
        cout << "" << endl;
        cout << "1. View all studentdata" << endl;
        cout << "2. View a student’s data using the ID" << endl;
        cout << "3. Show the highest and the lowest final marks " << endl;
        cout << "" << endl;
        cout << "Type a desired value from the given menu and press the ENTER key to proceed" << endl;
        int givenVal = 0;
        cin >> givenVal;
        cin.ignore();
    
        switch (givenVal)
        {
        case 1:
            allStudentData();
            break;
        case 2:
             idStudent();
            break;
        case 3:
             showHighLowMarks();
            break;
        default:
            cout << "an Error is occurred" << endl;
            cin.clear();//clear the input buffer
            menu();
            break;
        }
        cin.get();
    }
    
    void allStudentData()
    { 
        cout << " ";  for(int i=0;i<40;i++){ cout << "_";}  cout << endl;
        cout << "|" << setw(11) << "StudentID 0" << "|" << setw(6) << "TMA1 1" << "|" <<  setw(6) << "TMA2 2" << "|" << setw(6) << "BCAT 3" << "|" <<  setw(7) << "Final 4|" << endl;
        cout << "|" << setw(11) << sR[1][0] << "|" <<  setw(6) << sR[1][1] << "|" <<  setw(6) << sR[1][2] << "|" <<  setw(6) << sR[1][3] << "|" << setw(7) << sR[1][4] << "|" << endl;
        cout << "|" << setw(11) << sR[2][0] << "|" <<  setw(6) << sR[2][1] << "|" <<  setw(6) << sR[2][2] << "|" <<  setw(6) << sR[2][3] << "|" << setw(7) << sR[2][4] << "|" << endl;
        cout << " ";  for(int i=0;i<40;i++){ cout << "_";}  cout << endl;
    }
    
    void idStudent()
    {
        cout << "Id the of the student to see" << endl;
        int stdID;
        cin >> stdID;
        cin.ignore();
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<5;j++)
            {
                if(sR[i][j] == stdID)
                {
                    cout << "Student ID.is :" << sR[i][j] << endl;
                    cout << "TMA1.......is :" << sR[i][j+1] << endl;
                    cout << "TMA2.......is :" << sR[i][j+1] << endl;
                    cout << "BCAT.......is :" << sR[i][j+2] << endl;
                    cout << "Final......is :" << sR[i][j+4] << endl;
                    break;
                }
            }
        }
        
    }
    
    void showHighLowMarks()
    {
        int fM[2];//for receiving final marks
        int k=0;
    
        for(int i=1;i<3;i++)
        {
            for(int j=0;j<5;j++)
            {
                if(j==4)//final mark value
                {
                    fM[k] = sR[i][j];
                    k++;
                }
            }
        }
    
        //sorting
        for(int i=0;i<1;i++)
        {
            int temp = fM[i+1];
            if(temp < fM[i])
            {
                fM[i+1] = fM[i];
                fM[i] = temp;
            }
        }
    
        cout << "The Highest value is..." << fM[1]  << endl;
        cout << "The Lowest value is...." << fM[0]  << endl;
    }
     
    • Like
    Reactions: elektro

    elektro

    Well-known member
  • Apr 18, 2011
    6,856
    9,352
    113
    Tropical Island Of Sri Lanka
    Code:
    int n = 0, StudentID = 0;
    
    int AllStudentData[3][5] = { {1232, 10, 23, 45, 56}, {2343, 45, 43, 24, 78}, {2343, 34, 45, 45, 45}, {3423, 67, 6, 65, 56}};
    
    cin >> n;
    
    switch (n)
    {
    case 1:
        PrintAllStudentData();
        break;
    case 2:
        PrintStudentData(StudentID)
        break;
    case 3:
        PrintLowestAndHighestMarks()
        break;
    default:
       cout << "Error! " << endl;
       break;
    }
    
    PrintAllStudentData()
    {
        cout << "|StudentID | TMA1 | TMA2| BCAT | Final " << endl;
        cout << "| ================================================== ";
    
        for ( int i = 0; i < 3; ++i)
        {
            for (int j = 0; j < 5; ++j)
            {
                  cout << AllStudentData[i][j];
                  cout << "|";
             }
            cout << endl;
         }
    }



    oya code gedi pitin copy karala compile wennewath run wennewath nehe honde. nikan eka digata gahagena giye. idea ekak enna athine. ithiri tika karaganna.

    Thanx mcho :)
     

    elektro

    Well-known member
  • Apr 18, 2011
    6,856
    9,352
    113
    Tropical Island Of Sri Lanka
    :) ඔන්න ගැහුවා හරියටම තියෙනවා නමුත් මේකේ තියෙන්නේ ROWS 2 ක් විතරයි.
    උබ ඉතුරු row ටික ගහගන්න එතකොට මේක වැඩ කරන විදියත්, code එකේ basic principle එකත් බලාගන්න පුලුවන්.

    සාරාංශය වශයෙන් කිව්වොත්.
    මේකේ Table එක Align කරන්න setw() function එක ඕන වෙනවා ඒක නැතුව table එක align කරන්න බෑ හරියට, ඒකට iomanip කියන library එකට access වෙන්න වෙනවා (මේක header file එකක්)

    ඊට පස්සේ ID එක ගහලා Student ගේ marks බලන්න ඕනේ නම් IF conditional statmenet
    එක යොදාගන්න පුලුවන්.

    අවසානයේදි High,Low mark එක ගන්න නම් bubble sort එක යොදාගන්න ඕනේ. මේක කරන්න කලින් final mark ටික ගන්න වෙනම single dimension array එකකට ඊට පස්සේ ඒ array එක sort කරන්න bubble sort
    algorithm එක හරහා (මම ඒක ගහලා ඇති අවසානයේදි) මතක ඇතුව sorting එකේදි low value එක මුලටත්, high value එක අගටත් දාගන්න.

    ඊට පස්සේ array එකේ 0 space එකට access වෙලා low
    mark එකත්
    array එකේ length - 1 (අවසාන space එක) space එකට access වෙලා high mark එකත් ගන්න පුලුවන්, මේක හරි සරලයි.

    මෙතනට මම code එක upload කරා. copy කරද්දි toggle wrap එක off කරලා copy කරන්න නැත්නම් VS එකේ code එක messy විදියට paste වෙයි paste කරන කොට.

    http://pastebin.com/DfBBRAMi#

    ඒ වගේම Elakiri එකේත් ගහලා ඇති. code එක අනිවාර්යෙන්ම වැඩ AND tested Visual Studio එකේ.


    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    /*
    1. View all studentdata
    2. View a student’s data using the ID
    3. Show the highest and the lowest final marks 
    */
    
    void allStudentData();
    void idStudent();
    void showHighLowMarks();
    void menu();
    
    int main()
    {
        menu();
    }
    
    int sR[3][5];
    void menu()
    {
        //|StudentID 0 | TMA1 1| TMA2 2| BCAT 3| Final 4
        //student column names
        sR[0][0] = 0;
        sR[0][1] = 1;
        sR[0][2] = 2;
        sR[0][3] = 3;
        sR[0][4] = 4;
    
        //student One
        sR[1][0] = 1232;
        sR[1][1] = 10;
        sR[1][2] = 23;
        sR[1][3] = 45;
        sR[1][4] = 56;//final mark
    
        //student two
        sR[2][0] = 2343;
        sR[2][1] = 45;
        sR[2][2] = 43;
        sR[2][3] = 24;
        sR[2][4] = 78;//final mark
    
    
        cout << "----------  Menu -----------" << endl;
        cout << "" << endl;
        cout << "1. View all studentdata" << endl;
        cout << "2. View a student’s data using the ID" << endl;
        cout << "3. Show the highest and the lowest final marks " << endl;
        cout << "" << endl;
        cout << "Type a desired value from the given menu and press the ENTER key to proceed" << endl;
        int givenVal = 0;
        cin >> givenVal;
        cin.ignore();
    
        switch (givenVal)
        {
        case 1:
            allStudentData();
            break;
        case 2:
             idStudent();
            break;
        case 3:
             showHighLowMarks();
            break;
        default:
            cout << "an Error is occurred" << endl;
            cin.clear();//clear the input buffer
            menu();
            break;
        }
        cin.get();
    }
    
    void allStudentData()
    { 
        cout << " ";  for(int i=0;i<40;i++){ cout << "_";}  cout << endl;
        cout << "|" << setw(11) << "StudentID 0" << "|" << setw(6) << "TMA1 1" << "|" <<  setw(6) << "TMA2 2" << "|" << setw(6) << "BCAT 3" << "|" <<  setw(7) << "Final 4|" << endl;
        cout << "|" << setw(11) << sR[1][0] << "|" <<  setw(6) << sR[1][1] << "|" <<  setw(6) << sR[1][2] << "|" <<  setw(6) << sR[1][3] << "|" << setw(7) << sR[1][4] << "|" << endl;
        cout << "|" << setw(11) << sR[2][0] << "|" <<  setw(6) << sR[2][1] << "|" <<  setw(6) << sR[2][2] << "|" <<  setw(6) << sR[2][3] << "|" << setw(7) << sR[2][4] << "|" << endl;
        cout << " ";  for(int i=0;i<40;i++){ cout << "_";}  cout << endl;
    }
    
    void idStudent()
    {
        cout << "Id the of the student to see" << endl;
        int stdID;
        cin >> stdID;
        cin.ignore();
        for(int i=0;i<3;i++)
        {
            for(int j=0;j<5;j++)
            {
                if(sR[i][j] == stdID)
                {
                    cout << "Student ID.is :" << sR[i][j] << endl;
                    cout << "TMA1.......is :" << sR[i][j+1] << endl;
                    cout << "TMA2.......is :" << sR[i][j+1] << endl;
                    cout << "BCAT.......is :" << sR[i][j+2] << endl;
                    cout << "Final......is :" << sR[i][j+4] << endl;
                    break;
                }
            }
        }
        
    }
    
    void showHighLowMarks()
    {
        int fM[2];//for receiving final marks
        int k=0;
    
        for(int i=1;i<3;i++)
        {
            for(int j=0;j<5;j++)
            {
                if(j==4)//final mark value
                {
                    fM[k] = sR[i][j];
                    k++;
                }
            }
        }
    
        //sorting
        for(int i=0;i<1;i++)
        {
            int temp = fM[i+1];
            if(temp < fM[i])
            {
                fM[i+1] = fM[i];
                fM[i] = temp;
            }
        }
    
        cout << "The Highest value is..." << fM[1]  << endl;
        cout << "The Lowest value is...." << fM[0]  << endl;
    }

    ගොඩක්ම ගොඩක් පිං මචං..උබට තව තවත් කරන වැඩකටයුතු වලින් ඉස්සරහටම යන්න ලැබෙන්න ඕන..ගොඩක් උං දැනගතත්තත් නොදන්න වගේ ඉන්න කාලෙක උබේ වෙලාව වැය කරලා කියලා දුන්නට ගොඩක් පිං.. :) වරදින්නේ නෑ ජය වේවා !!!

    මොකක්හරි අවුලක් තිබ්බොත් මම කියන්නම් මචෝ..

     

    Arkham Knight

    Member
    Mar 14, 2014
    1,627
    89
    0
    ගොඩක්ම ගොඩක් පිං මචං..උබට තව තවත් කරන වැඩකටයුතු වලින් ඉස්සරහටම යන්න ලැබෙන්න ඕන..ගොඩක් උං දැනගතත්තත් නොදන්න වගේ ඉන්න කාලෙක උබේ වෙලාව වැය කරලා කියලා දුන්නට ගොඩක් පිං.. :) වරදින්නේ නෑ ජය වේවා !!!

    මොකක්හරි අවුලක් තිබ්බොත් මම කියන්නම් මචෝ..

    ela :)