dimanche 6 septembre 2020

My third and fourth command line arguments are not being detected in my C++ code

I've wrote some code to output certain students' grades if they're in between a lower bound and upper bound. I don't get any errors when I'm compiling the program but it's not writing to my csv file (the very last part of the code).

I suspect that there is a problem with my if-statement, where I wrote if(argv[3] && argv[4]) because it's just not executing this part of the code. Argv3 and 4 would be "D A" in the code. So I'd write: g++ -std=c++11 studentData.cpp and then: ./a.out output.csv D A

I went ahead and bolded the section of my code I'm referring to, in case you don't want to read everything.

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;

struct studentData {
   string studentName;
   int homework;
   int recitation;
   int quiz;
   int exam;
   double average;
};

void addStudentData(studentData students[], string studentName, int homework, int recitation, int quiz, int exam, int length){ 
        students[length].studentName = studentName; 
        students[length].homework = homework; 
        students[length].recitation = recitation;
        students[length].quiz = quiz;
        students[length].exam = exam;
        students[length].average = (homework + recitation + quiz + exam)/4.0; //calculating the student average
        
}
char calcLetter(double avg)
{
    char str[]={'A', 'B', 'C', 'D', 'F', '\0'};
    if(avg >= 90){
        return str[0];
        }
        else if(avg >= 80 && avg <90){
            return str[1];
            }
            else if(avg >= 70 && avg <80){
                return str[2];
                } 
                else if(avg >= 60 && avg <70){
                    return str[3];
                    }
                    else
                    {
                        return str[4];
                        }
}

float calcNum(string avg)
{
    if(avg == "A"){
        return 90;
        }
        else if(avg == "B"){
            return 80;
            }
            else if(avg == "C"){
                return 70;
                } 
                else if(avg == "D"){
                    return 60;
                    }
                    else
                    {
                        return 50;
                        }
}


void printList(const studentData students[], int length) 
{
    
    for(int i=0; i < length; i++){
      cout << students[i].studentName << " earned " << students[i].average << " which is an " << calcLetter(students[i].average) << endl;
      

    }
}

int main(int argc, char* argv[]) 
{
    
   studentData students[10];// File of 10 students
   string arg = argv[1]; // coerce the character array into a string type
   int length = 0;
   if (arg=="students.csv"){
       ifstream in_file;
       string filename = argv[1]; 
       string name; 
       int homework;
       int recitation;
       int quiz;
       int exam;
       
       string line;
       in_file.open(filename);
       while (in_file.good()){ 
           getline(in_file, line, ','); 
           name = line;
           
           getline(in_file, line, ','); 
           homework = stoi(line);
          
           
           getline(in_file, line, ',');
           recitation = stoi(line);
          
           
           getline(in_file, line, ',');
           quiz = stoi(line);
           
           getline(in_file, line, '\n');
           exam = stoi(line);
           
        addStudentData(students, name, homework, recitation, quiz, exam, length);
          length++;   
      }
      in_file.close();
   } 
   
   **ofstream out_file;
   out_file.open(argv[2]);
   if(argv[3] && argv[4]){
      
      string lower = argv[3];
      string upper = argv[4];
      int x = calcNum(lower);
      int y = calcNum(upper);
      cout << "lower is: " << lower << endl;
      cout << "upper is: " << upper << endl;
   
      for(int i= 0; i < length; i++){
       if(students[i].average > x && students[i].average < y){
        
        
        out_file << students[i].studentName << " earned " << students[i].average << " which is an " << calcLetter(students[i].average) << endl;
       }
       
      }
   
  }
   else {
        cout << "Argv3 and Argv4 have not been detected";
          }
          
   out_file.close();**
   return 0;
}

Aucun commentaire:

Enregistrer un commentaire