Program with command lines(Really close, please help)

I want to write a program that takes the number of tests, assignments, labs, and quizzes taken in a given class by command line arguments. The program will read the number of tests, assignments, labs and quizzes as command line input, and then read the specific number of grades for each by prompting the user for the correct number of items.

The user can supply these arguments in any order, and therefore you need to preface each argument with an option to know what the argument is. For example:
class_average –q 5 –a 8 –l 10 –t 2 -f

I want the function to take in these commands and then ask what the scores for how many tests and quizzes and so on is:

example:
Quiz 1:
Quiz 2:

Quiz 5:
Assignment 1:
Assignment 2:

Assignment 8:

Final Exam:

with the example:
class_average –q 5 –a 8 –l 10 –t 2 -f
The user would have to enter 5 quiz scores, 8 assignment scores, 10 lab scores and 2 test scores with the final exam score optional.

How can I write this small program?

here is what I have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iosteam>
#include <string>
#include <cmath>
#include <stdlib.h>

using namespace std;

#define LABS .1;
#define QUIZZES .1;
#define ASSIGNMENTS .4;
#define TESTS .25;
#define FINAL .15;

int main(int argc, char *argv[]){




}

Last edited on
man getopt
getopt?
think he means get out. People here are perfectly willing to help you with one or two sections but not do your homework for you.
With `man getopt' I mean `man getopt'
http://linux.die.net/man/3/getopt
@Stimson I am not asking for complete homework help. I just need to understand this command line arguments stuff. I am sorry if I offended you. Just was looking for examples like this question.

@ne555
I am looking that up right now and I see what you are saying. I am going to try that out, thank you!
instead of using getopt I started this:

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
 
#include <cmath>
#include <cstdlib>
#include <sys/time.h>
#include <iostream>
#include <stdlib.h>
#include <cctype>

using namespace std;

#define LABS .1;
#define QUIZZES .1;
 #define ASSIGNMENTS .4;
 #define TESTS .25;
 #define FINAL .15;

 int error_check(int argc, char *argv[]);
 int quizzes(char *argv[]);
 int labs(char *argv[]);
 int assignments(char *argv[]);
 int tests(char *argv[]);
 int final_exam(char *argv[]);

 int main(int argc, char *argv[]){

    //error_check(argc, argv);
    quizzes(argv);
    labs(argv);
    //assignments(argv);
    //tests(argv);
   //final_exam(argv);

    return 0;

 }

 int error_check(int argc, char *argv[]){

    while(argc < 9){
       cout << "Error! Please enter in all parameters." << endl;
       return 0;
    }
    while((argv[2] || argv[4] || argv[6] || argv[8]) == 0){
       cout << "Error! Please enter different integer then 0." << endl;
       return 0;
    }
    while(isdigit(atoi(argv[2])) || isdigit(atoi(argv[4])) || isdigit(atoi(argv[6])) || isdigit(atoi(argv[8]))==false){
      cout << "Error! You did not enter an integer in your arguments." << endl;
       return 0;
    }
 }

 int quizzes(char *argv[]){

    int q_score;
    int n;
    int i;

  if(argv[1][1] == 'q'){
        n = atoi(argv[2]);
     }
     else if(argv[1][3] == 'q'){
        n = atoi(argv[4]);
     }
     else if(argv[1][5] == 'q'){
        n = atoi(argv[6]);
     }
     else if(argv[1][7] == 'q'){
        n = atoi(argv[8]);
     }

        for(i=0; i < n; i++){
           cout << "Please enter your quiz score: ";
           cin >> q_score;
        }
 }

 int labs(char *argv[]){

    int l_score;
    int n;
    int i;

   if(argv[1][1] == 'l'){
       n = atoi(argv[2]);
   }
   else if(argv[1][3] == 'l'){
      n = atoi(argv[4]);
   }
   else if(argv[1][5] == 'l'){
       n = atoi(argv[6]);
   }
   else if(argv[1][7] == 'l'){
       n = atoi(argv[8]);
   }
      for(i=0; i < n; i++){
         cout << "Please enter your lab score: ";
         cin >> l_score;
      }
 }

 int assignments(char *argv[]){

    int a_score;
    int n;
    int i;

    if(argv[1][1] == 'a'){
       n = atoi(argv[2]);
    }
    else if(argv[1][3] == 'a'){
       n = atoi(argv[4]);
    }
    else if(argv[1][5] == 'a'){
       n = atoi(argv[6]);
    }
    else if(argv[1][7] == 'a'){
       n = atoi(argv[8]);
    }
       for(i=0; i < n; i++){
  cout << "Please enter your assignment score: ";
         cin >> a_score;
       }
 }

 int tests(char *argv[]){

    int t_score;
    int n;
    int i;

    if(argv[1][1] == 't'){
       n = atoi(argv[2]);
    }
    else if(argv[1][3] == 't'){
       n = atoi(argv[4]);
    }
    else if(argv[1][5] == 't'){
       n = atoi(argv[6]);
    }
    else if(argv[1][7] == 't'){
       n = atoi(argv[8]);
    }
       for(i=0; i < n; i++){
          cout<< "Please enter your test score: ";
          cin >> t_score;
       }
 }

 int final_exam(char *argv[]){

    int f_score;
    int n;
    int i;

    if(argv[1][1] == 'f'){
       n = atoi(argv[2]);
    }
    else if(argv[1][3] == 'f'){
       n = atoi(argv[4]);
    }
    else if(argv[1][5] == 'f'){
       n = atoi(argv[6]);
    }
    else if(argv[1][7] == 'f'){
       n = atoi(argv[8]);
    }
       for(i=0; i < n; i++){
          cout << "Please enter your final exam score: ";
          cin >> f_score;
       }
 }


This code is suppose to let class_average –q 5 –a 8 –l 10 –t 2 -f be inputed in any order and then ask what were the scores for the 5 quizzes and 8 assignments and what not.

When I try the example above it asks for the scores of all of these but only 5 times instead of the number it is suppose to be. So it asks for 5 assignments and so on when it should not. It should ask for 8.

I think my else if statements are not working.

any help?
and sorry, my int main looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
int main(int argc, char *argv[]){

    //error_check(argc, argv);
    quizzes(argv);
    labs(argv);
    assignments(argv);
    tests(argv);
    final_exam(argv);

    return 0;

 }
Topic archived. No new replies allowed.