最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - Trying to display character array elements greater than X results in some values being replaced by visibly nothing - Stack

programmeradmin1浏览0评论

Basically, I'm making a student records management system, and everything else works fine. But, the module for displaying students with grades greater/lesser than X results in some values being invisible/ replaced by nothing.

This code isolates two examples of the main issue, individual snippets of display and sort work fine, but combined together under these or similar conditions, they produce bugged out grade values.

#include <iostream>
using namespace std;

int main(){
    
    const int default_size = 100;
    int stud_roll [default_size] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50};
    int class_sec [default_size] = {7, 2, 9, 5, 3, 8, 6, 1, 4, 10, 2, 8, 6, 1, 9, 4, 7, 3, 5, 10, 2, 6, 8, 3, 7, 1, 9, 4, 10, 5, 2, 6, 7, 8, 1, 9, 3, 4, 5, 10, 6, 2, 8, 7, 1, 9, 3, 4, 10, 5};
    float mid_term_marks [default_size] = {2, 8, 19.5, 3, 27, 10.5, 35, 7, 14, 4.5, 22, 30, 18.5, 25, 1, 21, 2.5, 38, 15, 17.5, 43, 5, 29, 11, 9.5, 24, 16, 50, 20, 33, 40, 23, 6.5, 41, 28, 39, 13, 45.5, 34, 36, 26, 31, 44, 46, 32, 48, 37, 49, 47, 42.5};
    float fin_term_marks [default_size] = {12, 88, 19.5, 3, 72, 41.5, 57, 9, 63, 4.5, 29, 84, 56, 22, 71, 2.5, 49, 35, 67, 18.5, 95, 6, 54, 31, 44.5, 39, 83, 10, 77, 66, 53, 5.5, 80, 13, 23.5, 60, 50, 70, 15, 93, 7, 59, 48.5, 42, 11.5, 28, 74, 26.5, 92, 17};
    char final_grades [default_size] = {};

    
    char user_choice[2];
    char check_grade;
    int number_of_entries = 50; // will be used as range for loops.
    
    for (int i = 0; i < number_of_entries; i++) {
        
        if (fin_term_marks[i] > 85) {
            
            final_grades[i] = 'A';
        }
        else if (fin_term_marks[i] < 86 && fin_term_marks[i] > 72) {
            
            final_grades[i] = 'B';
        }
        else if (fin_term_marks[i] < 73 && fin_term_marks[i] > 59) {
            
            final_grades[i] = 'C';
        }
        else if (fin_term_marks[i] < 60 && fin_term_marks[i] > 49) {
            
            final_grades[i] = 'D';
        }
        else if (fin_term_marks[i] < 50 && fin_term_marks[i] >= 0){
            
            final_grades[i] = 'F';
        }
        
    }
    
    do { 
    
        cout << " 16. Display Students with Grade greater than X (Ascending)." << endl;
        cout << " 17. Display Students with Grade less than or equal to X (Descending)." << endl;
        cout << " E.  Exit" << endl;
        
        cout << "\n Please enter the operation you'd like to perform: ";
        cin >> user_choice;
        
        
        else if (user_choice[0] == '1' && user_choice[1] == '6') { 
            
            cout << " Please enter the grade you want to check above: ";
            cin >> check_grade;
            check_grade = toupper(check_grade);
            
            if (check_grade >= 'A' && check_grade <= 'F' && check_grade != 'E') {
                
                for (int i = 0; i < number_of_entries; i++) { 
                
                    for (int j = i + 1; j < number_of_entries; j++) {
                    
                        if (fin_term_marks[i] > fin_term_marks[j]) {
                        
                            swap (stud_roll[i], stud_roll[j]); 
                            swap (class_sec[i], class_sec[j]);
                            swap (mid_term_marks[i], mid_term_marks[j]);
                            swap (fin_term_marks[i], fin_term_marks[j]);
                            swap (final_grades[i], final_grades[j]);
                        }
                    } 
                }
                
                for (int i = 0; i < number_of_entries; i++) {
                    
                    if (final_grades[i] < check_grade) {
                        
                        cout << " Roll Number:  " << stud_roll[i] << endl;
                        cout << " Final Grade:  " << final_grades[i];
                        cout << "  Final Term Marks:   " << fin_term_marks[i] << endl;
                        cout << " Class Number: " << class_sec[i];
                        cout << "  Mid Term Marks:     " << mid_term_marks[i] << endl;
                        cout << endl;
                    }
                }
            }
            
            else {
                
                cout << " Invalid grade... " << endl;
            }
        }
        
        else if (user_choice[0] == '1' && user_choice[1] == '7') {
            
            cout << " Please enter the minimum grade you want to check: ";
            cin >> check_grade;
            check_grade = toupper(check_grade);
            
            if (check_grade >= 'A' && check_grade <= 'F' && check_grade != 'E') {
                
                for (int i = 0; i < number_of_entries; i++) { 
                
                    for (int j = i + 1; j < number_of_entries; j++) {
                    
                        if (fin_term_marks[i] < fin_term_marks[j]) {
                        
                            swap (stud_roll[i], stud_roll[j]); 
                            swap (class_sec[i], class_sec[j]);
                            swap (mid_term_marks[i], mid_term_marks[j]);
                            swap (fin_term_marks[i], fin_term_marks[j]);
                            swap (final_grades[i], final_grades[j]);
                        }
                    } 
                }
                
                for (int i = 0; i < number_of_entries; i++) {
                    
                    if (final_grades[i] >= check_grade) {
                        
                        cout << " Roll Number:  " << stud_roll[i] << endl;
                        cout << " Final Grade:  " << final_grades[i];
                        cout << "  Final Term Marks:   " << fin_term_marks[i] << endl;
                        cout << " Class Number: " << class_sec[i];
                        cout << "  Mid Term Marks:     " << mid_term_marks[i] << endl;
                        cout << endl;
                    }
                }
            }
            
    } while (user_choice[0] != 'e' && user_choice[0] != 'E');
  
    return 0;
}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论