Class Average Calculator(Help!)

My assignment wants me to input scores via line command, but I'm not sure how I'm supposed to do that.

#include<iostream>
#include<cmath>
#include<string>

using namespace std;


float labs(){
float lgrade, labg;
cout<< "Enter the total points you have for labs: ";
cin>> lgrade;
return(labg = lgrade * .10);
}

float quiz(){
float qgrade, quizg;
cout<<"Enter the total points you have for quizzes: ";
cin>> qgrade;
return(quizg = qgrade * .10);
}

float assign(){
float agrade, assigng;
cout<<"Enter the total points you have for assignments: ";
cin>> agrade;
return(assigng = agrade * .40);
}

float exam(){
float egrade, examg;
cout<<"Enter the total points you have for exams: ";
cin>> egrade;
return(examg = egrade * .25);
}

float final(){
float fgrade, finalg;
cout<<"Enter the total points you have for the final: ";
cin>> fgrade;
return(finalg = fgrade * .15);
}



int main(){

int labg, quizg, assigng, examg, finalg;

labg = labs();
quizg = quiz();
assigng = assign();
examg = exam();
finalg = final();

int grade;

grade = labg + quizg + assigng + examg + finalg;
cout<<grade<<endl;

return 0;

}
http://www.cplusplus.com/articles/DEN36Up4/

parameters placed in main( * here * ) argv and argc
What should I do after that?
Help!
1
2
3
4
5
6
7
8
9
10
11
int main (int argc, const char * argv[])
{  int labg, quizg, assigng, examg, finalg;
    if (argc != 6)
    { cout << "Wrong number of command line arguments" << endl;
       return 0;
    }
    // argv[0] is the program name, so we start from argv[1]
    labg = atoi(argv[1]);  // convert first parameter to an integer
    //  etc for the other arguments
    //  calculate grade ... 
}

since labg = atoi(argv[1]);
it only lets the user input lab grades as the first.. i.e.
./a.out -labg 98 -quizg 100 etc...
How do I make it so it doesn't matter which order the user inputs it?
Then you're going to have to parse the command line.

Keep in mind that spaces in the command line delimit the arguments.
-labg 98
is two arguments.

what does parse command line mean?
To parse something means to break it apart into individual tokens and then process the individual tokens.

The C runtime has already taken the command line and broken it apart into individual tokens for you. It's given you a list of those tokens starting at argv[1].

Your job is to look at each of those tokens and determine which keyword (e.g. "-labg") it matches. If it matches a keyword, move on to the next token and convert it to a number, then assign it to the appropriate integer (e.g. variable labg).

1
2
3
4
5
6
7
  int i = 1;    // index to first token
  while (i <= argc) 
  {  if (strcmp(argv[i],"-labg") == 0)
        labg = atoi(argv[i+1]);
      // repeat for other keywords
      i += 2;
   }

The above code is just a general outline. It doesn't check that argv[i+1] is a number or even if it exists. It also doesn't deal with the command line being empty or if a keyword doesn't match.
Why do we do i += 2 at the end?
Also, do I put the other keywords inside the while loop as well?
Why do we do i += 2 at the end?

Because the arguments are in pairs.

do I put the other keywords inside the while loop as well?

Yes. That's what the "repeat for other keywords" meant.
i have some problem just going into the while loop.

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
35  int check_arguments(int argc, char *argv[]){
 34    int i=1, l, q, a, e, f;
 35    cout<<"boop"<<endl;
 36    while(i<=argc){
 37       if( strcmp(argv[i], "-l") == 0){
 38          l = atoi(argv[i+1]);
 39          cout<<"beep"<<endl;
 40       }
 41       else if (strcmp(argv[i], "-q") == 0){
 42          q = atoi(argv[i+1]);
 43       }
 44       else if (strcmp(argv[i], "-a") == 0){
 45          a = atoi(argv[i+1]);
 46       }
 47       else if (strcmp(argv[i], "-e") == 0){
 48          e = atoi(argv[i+1]);
 49       }
 50       else if (strcmp(argv[i], "-f") == 0){
 51          f = atoi(argv[i+1]);
 52       }
 53    }
 54    i +=2;
 55
 56    set_scores(l);
 57 }
 58
63 }
 66.int main (int argc, char *argv[]){
 67    check_arguments(argc, argv);

cout << boop and beep are just there for me to check if it went into the loop.
i += 2; must be inside the while loop.

Last edited on
Topic archived. No new replies allowed.