argc changes

I have to make a code that uses inputs from the command like to print the most common names on a BabyNames file. I need to print the number of names given in the command line based on the gender. (I'm also not finished with it)

The problem that I am having is that no matter what is in the command line,argc is always equal to 4. What have I done, or what do I need to do to fix 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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(int argc,char *argv[]){
    string name;
    string gender;
    int freq,pc=0;
    ifstream fin;
    
    if (argc < 4){
        cout << "Wrong number of arguments" << endl << "Correct usage: filename #namesToPrint gender" << endl;
        return -1;
    }
    
    fin.open(argv[1]);
    if (fin.fail()){
        cout << "baby2 not opened";
        return 1;
    }
    
    cout << argc << " Most Popular Baby Names" << endl << endl;
    std::cout.width(10); std::cout << std::left << "Name";
    std::cout.width(10); std::cout << std::right << "Frequency" << endl;

        while (fin>>name>>gender>>freq){
            if (gender == argv[3])
                std::cout.width(10); std::cout << std::left << name;
                std::cout.width(10); std::cout << std::left << " " /*<< gender << " "*/ << freq << endl;
                pc++;
            if (pc == argc)
                break;
        }       
    
    fin.close();
    return 0;
}
nevermind, looks like as noted below 33 is why you just get 4 results.
your arg parsing looks like you are doing it ok.

you may need to make some adjustments in case the filename or path have spaces, but that can be an enhancement later.
Last edited on
At line 33, shouldn't you be comparing pc to the numeric value of argv[2]?
@dhayden I tried both comparing pc to argv[2], which gave a forbidden comparison between pointer and integer.
I also tried using this
 
int numName = atoi(argv[2])

which also only allowed 4 names to be printed
Last edited on
argc is however many arguments you input to the program, including the program name itself. It's certainly not always 4.

1
2
3
4
            if (gender == argv[3])
                std::cout.width(10); std::cout << std::left << name;
                std::cout.width(10); std::cout << std::left << " " /*<< gender << " "*/ << freq << endl;
                pc++;

Do you realize you don't have curly braces { } around your if statement block?
Nice catch Ganado. So combining this, it sounds like the code should be:
1
2
3
4
5
6
7
8
9
10
11
...
        int numName = atoi(argv[2]);
        while (fin>>name>>gender>>freq){
            if (gender == argv[3]) {
                std::cout.width(10); std::cout << std::left << name;
                std::cout.width(10); std::cout << std::left << " " /*<< gender << " "*/ << freq << endl;
                pc++;
            }
            if (pc == numName)
                break;
        }       

For efficiency, you could put lines 9 & 10 inside the if statement since there's no reason to check when it hasn't changed.
Topic archived. No new replies allowed.