Using Visual Studios 2019 and having errors in my code

Hi, I am having issues figuring out what I did wrong with my code. I am being told that there are expected ";" and I am not sure how to fix the expected declarations. I would like assistance so I can learn how to fix it.
https://imgur.com/a/50IVZsy for a more in-depth view

[#include <iostream>
#include <fstream>
//defining max file size
#define MAX_FISHERS 25
using namespace std;
//main
int main()
{
//initializing variables
string line;
string fisher[MAX_FISHERS]; //storing fishers' names
int fish[MAX_FISHERS]; //storing number of fishes caught

//opening text file
ifstream infile("pgm5.txt");
int count = 0
if (infile.is_open())
{
int index1 = 0, index2 = 0;
while (getline(infile, line))
{
//token1 stores the fisher name and token 2 will store fish count as string
std::string token1 = line.substr(0, line.find(" "));
std::string token2 = line.substr(line.find("") + 1, line.size() - 1);

//storing token1 to the fisher array
fisher[index1++] = token1;
//converting string to integer using the stringstream class
stringstream temp(token2);
int num = 0;
temp >> num;

//storing integer to fish array
//using ab() to change the negative sign
fish[index2++] abs(num);

//increasing count by 1
count++
}
infile.close(); //closing input file
}

else
{
cout << "Unable to open file";
}
//declare variables for additional operations
int maxFish = 0, minFish = 99999, totalFish = 0, minPos = 0, maxPos = 0;

//running loop to find the winner, least scorer and the total
for (int = i = 0;i < count;i++)
{
//display name and count for each fisher
cout << "Fisher " << (i + 1) << " who named " << fisher[i] << " caught " << fish[i] << " fish\n";

//find max scorer
if (fish[i] > maxFish)
{
maxFish = fish[i];
maxPos = i + 1;
}

//find min scorer
if(fish[i] < minFish)
{
minFish = fish[1];
minPos = i + 1;
}

//find total
totalFish += fish[1];
}

//display output for format
cout << "The winner is Fisher " << maxPos << " who named " << fisher[maxPos - 1] << " by catching " << maxFish << " fish." << endl;
cout << "Fisher" << minPos << " who named " << fisher[minPos - 1] << " caught the least number of fish: " << minFish << "." << endl;
cout << "And in total all the fishers caught " << totalFish << " fish." << endl;
cout << "Press any key to continue..." << endl;

return 0;

}
}
Last edited on
Solve one error at a time (usually the first one). Each error will have an associated line number with it. Look around the line it reports as an error.

Read your code out loud to yourself to pick up on errors you wouldn't notice otherwise.
int count = 0has no semi-colon.

int = i = 0 is not correct syntax.

fish[index2++] abs(num); is not correct syntax.
Last edited on
How do I go about fixing it because when I do try to adjust it, I just get more errors.
How do I go about fixing it because when I do try to adjust it, I just get more errors.

Yes, this shouldn't scare you -- the number or size of the error messages is not a good indicator of how many issues your code contains.

Fix the program like this:
1. Compile.
2. Fix the first error.
3. Recompile.
4. If there are errors, go to step 2, else go to step 5.
5. Test the program.

To avoid this issue in the future, you'll have to change your workflow a little bit. The key is to write just a little code at a time. This way, if you make major conceptual mistakes, you can catch them quickly, before spending several hours or days on code that is fundamentally wrong and must be re-written. You'll also get more frequent practice correcting errors, and thereby learn to understand the compiler more quickly. This is a good practice for anyone learning to code.

An experienced programmer might wait a long time (i.e., they might write an entire feature) before compiling and testing their code. This potentially saves a lot of time, but it only makes sense for programmers who are confident they can write that much code correctly (maybe excluding typos) the first time around.
Something of a start, at least it runs now:
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
#include <iostream>
#include <fstream>
#include <sstream> // <--
//defining max file size
#define MAX_FISHERS 25
using namespace std;
//main
int main()
{
    //initializing variables
    string line;
    string fisher[MAX_FISHERS]; //storing fishers' names
    int fish[MAX_FISHERS]; //storing number of fishes caught
    
    //opening text file
    ifstream infile("pgm5.txt");
    int count = 0; // <--
    if (infile.is_open())
    {
        int index1 = 0, index2 = 0;
        while (getline(infile, line))
        {
            //token1 stores the fisher name and token 2 will store fish count as string
            std::string token1 = line.substr(0, line.find(" "));
            std::string token2 = line.substr(line.find("") + 1, line.size() - 1);
            
            //storing token1 to the fisher array
            fisher[index1++] = token1;
            //converting string to integer using the stringstream class
            stringstream temp;
            temp << token2; // <--
            int num = 0;
            temp >> num;
            
            //storing integer to fish array
            //using ab() to change the negative sign
            fish[index2++] = abs(num); // <--
            
            //increasing count by 1
            count++;
        }
        infile.close(); //closing input file
    }
    
    else
    {
        cout << "Unable to open file";
    }
    //declare variables for additional operations
    int maxFish = 0, minFish = 99999, totalFish = 0, minPos = 0, maxPos = 0;
    
    //running loop to find the winner, least scorer and the total
    for (int i = 0;i < count;i++) // <--
    {
        //display name and count for each fisher
        cout << "Fisher " << (i + 1) << " who named " << fisher[i] << " caught " << fish[i] << " fish\n";
        
        //find max scorer
        if (fish[i] > maxFish)
        {
            maxFish = fish[i];
            maxPos = i + 1;
        }
        
        //find min scorer
        if(fish[i] < minFish)
        {
            minFish = fish[1];
            minPos = i + 1;
        }
        
        //find total
        totalFish += fish[1];
    }
    
    //display output for format
    cout << "The winner is Fisher " << maxPos << " who named " << fisher[maxPos - 1] << " by catching " << maxFish << " fish." << endl;
    cout << "Fisher" << minPos << " who named " << fisher[minPos - 1] << " caught the least number of fish: " << minFish << "." << endl;
    cout << "And in total all the fishers caught " << totalFish << " fish." << endl;
    cout << "Press any key to continue..." << endl;
    
    return 0;
    
} // <-- 



Unable to open fileThe winner is Fisher 0 who named x\320\200 by catching 0 fish.
Fisher0 who named x\320\200 caught the least number of fish: 99999.
And in total all the fishers caught 0 fish.
Press any key to continue...
Program ended with exit code: 0
To use C++ strings and stringstreams you need to include two other headers: <string> and <sstream>.

The other errors are missing semicolons, syntax typos and an extra closing brace ( } ).

PLEASE learn to use code tags, they make reading and commenting on your source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/

HINT, you can edit your post and add code tags.

It looks like you tried to use code tags, the first #include has a stray opening bracket at the front.

Several of the reported errors are "phantom errors," treated as errors because of problems earlier in your code.
Well that was a statement of the obvious and already done.

Perhaps explaining the obscure codes in the output would have been a constructive input instead of the repetition and citation from the obnoxious and serially boring code tag police.
Topic archived. No new replies allowed.