What is wrong with my program?

Basically, I have to find the ranks of baby names in the top 1000 list.
When I run it, it is weird and shows negative numbers.

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
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdlib>

using namespace std;

int main()
{
    char boys[1000][20], girls[1000][20], name[20], found;
    int count = 0;
    int rankofboys, rankofgirls, num;

    cout << "Enter the first name of the baby that you want to find\n";
    cin >> name;

    ifstream in;
    in.open("babynames2004.txt");
    if(in.fail())
    {
        cout << "Input file opening failed.\n";
        exit(1);
    }

    while(count < 1000 && in >>boys[count] >> girls[count])
    {
        count++;
    }

    for(int i = 0; i < 1000; i++)
    {
        if(strcmp(name, boys[i])==0)
        {
            found = true;
            rankofboys= i+1;
        }

        if(strcmp(name, girls[i]) ==0)
        {
            found = true;
            rankofgirls= i+1;
        }

        if(rankofboys !=0 && rankofgirls !=0)
        {
            cout << name << " is " << rankofboys << "on the rankings for boys.\n";
            cout << name << " is " << rankofgirls << "on the rankings for girls.\n";
        }
        else if (rankofboys !=0 && rankofgirls == 0)
        {
            cout << name << " is " << rankofboys << "on the rankings for boys.\n";
            cout << name << " is not ranked in the top 1000 girl names.\n";
        }
        else if (rankofboys == 0 && rankofgirls !=0)
        {
            cout << name << " is " << rankofgirls << "on the ranking for girls.\n";
            cout << name << " is not ranked in the top 1000 boy names.\n";
        }
    }
}
You forgot to initialize your counters "rankofboys" and "rankofgirls". You can print them before starting count and see they are not zeroes.
I tried and it is still returning the same thing. The program returns a bunch of the same answers and the answers are still negative.
You'd better post _what_ exactly you tried. You see, I'm not good in telepathy.

And by the way, what exactly is your task, and format of input file? You looks like trying to do reading statistics and printing results in the same loop - I think it is not what are you wanted to do.
Topic archived. No new replies allowed.