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(){
cout<< "Enter the total points you have for labs: ";
}

float quiz(){
cout<<"Enter the total points you have for quizzes: ";
}

float assign(){
cout<<"Enter the total points you have for assignments: ";
}

float exam(){
cout<<"Enter the total points you have for exams: ";
}

float final(){
cout<<"Enter the total points you have for the final: ";
}

int main(){

int labg, quizg, assigng, examg, finalg;

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

grade = labg + quizg + assigng + examg + finalg;

return 0;

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

parameters placed in main( * here * ) argv and argc
What should I do after that?
Help!
 1234567891011 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).

 1234567 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.

 1234567891011121314151617181920212223242526272829 35 int check_arguments(int argc, char *argv[]){ 34 int i=1, l, q, a, e, f; 35 cout<<"boop"<

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.