Can you please tell me what did i do wrong.

Hello, can you guys please help me with my code, i am new to function, vector, passing data to array from vector and stuff. So, can you look through my code and tell me what did i do wrong. Thank you very much.
This is what i need to do:
-Read in the following files inside of a function : colleges.txt and states.txt (string data type).
-Add the colleges/universities to a vector of strings.
-Add the states to parallel arrays of strings.
-Call the read function from your main function
Here is my code. Again, thank you very much.
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
  #include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
const int SIZE = 4565;
void campusfunction(string)
{
    ifstream campustxt;
    campustxt.open("colleges.txt");
    string campus;
    vector<string> campusvector(SIZE);
    while(getline(campustxt, campus))
    {
        campusvector.push_back(campus);
        cout << campus << endl;
    }
}
void statefunction(string)
{
    ifstream statetxt;
    statetxt.open("states.txt");
    string state[SIZE];
        for(int i = 0; i < SIZE; i++)
        {
            statetxt >> state[i];
        }
}
int main()
{
    char choice;
    string comparison;
    string userinput;
    cout << "WELCOME TO MY COLLEGE AND UNIVERSITY SUMMARY PROGRAM" << endl;
    do{
    cout << "Press 1 to enter possible colleges and universities and the program will tell you if they appear on the list." << endl;
    cout << "Press 2 to find out how many colleges and universities appear in the state of your choice." << endl;
    cout << "Press 3 to find out how many colleges and universities appear on our list in total." << endl;
    cout << "Press 4 to quit." << endl;
    cin >> choice;
    if(choice == 1)
    {
        do{

        cout << "What College or University do you want to find?" << endl;
        getline(cin, userinput);
        campusfunction(comparison);
        if(userinput == comparison)
            cout << "This college or university is in the list." << endl;
        else
            cout << "This college or university is not on the list" << endl;
        }
        while(userinput != "QUIT");
    }
    else if(choice == 2)
    {
        string userinput;
        string state2;
        cout << "Enter a state (abbreviation):" << endl;
        cin >> userinput;
        int numofstate;
        statefunction(state2);
        for(int i = 0; i < SIZE; i++ )
        {
            if(state2[i] == userinput)
            {
                numofstate++;
            }
        }
        cout << "This state " << userinput << "has" << numofstate << "colleges and universities." << endl;
    }
    else if(choice == 3)
    {
    cout << "There are " << (SIZE - 1) << "appear on our list in total" << endl;
    }
    else if (choice ==4)
        cout << "Good bye! Have a nice day!!!" << endl;
    return 0;
}
your do block : it should be
1
2
3
do{
    //...
} while(/*...*/);
instead of do{}if(){}else if(){}else{}
You can just create a function to clean up your code for getting the input
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int option()
{
	int opt{};
	bool correct_answer = false;
	while (!correct_answer) {
		cout << "Press 1 to enter possible colleges and universities and the program will tell you if they appear on the list." << endl;
		cout << "Press 2 to find out how many colleges and universities appear in the state of your choice." << endl;
		cout << "Press 3 to find out how many colleges and universities appear on our list in total." << endl;
		cout << "Press 4 to quit." << endl;

		cin >> opt;
		if (opt > 0 && opt < 5) correct_answer = true;
	}

	return opt;
}


Than you might create functions for different optons (functon1, function2 ...) and write
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//read those files here

bool done = false;
while (!done)
{
	int opt = option();

	switch (opt)
	{
	case 1: { function1(); break; }
	case 2: { function2(); break; }
	case 3: { function3(); break; }
	case 4: { done = true; break; }
	}
}
cout << "Good bye! Have a nice day!!!" << endl;


your read files functions should probably return vectors of those read strings so you dont have to read files every time different options is entered for example
Also you are creating vector of size 4565 and than adding new read elements using push_back.

This way for example if you have a vector with elements
0 0 0 0 and you are using push_back(5) resulting vector will be 0 0 0 0 5. In your case you are having 4565 emty strings followed by whatever you read from your file.
So if you are going to add elements with push back just create an empty vector without elements at all. It will expand itself as you continue to add an elements to the end of the vector using push_back.

Resulting function could look like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
vector<string> campusfunction(string)
{
    ifstream campustxt;
    campustxt.open("colleges.txt");
    //could test if file was correctly opened
    
    vector<string> campusvector;
    for(string campus; getline(campustxt, campus);)
    {
        campusvector.push_back(campus);
    }

    return campusvector;
}

You should do similar with second file and once you have those vectors you can pass them by const vector<string>& to functions that need those data.
Last edited on
@etrusks

Always put a default: case in the switch, it catches bad input.

With g++ and clang++ there is a compiler warning which isn't normally on with -Wall -Wextra -pedantic:

-Wswitch-default
Warn whenever a switch statement does not have a default case.

some other handy ones here

http://www.cplusplus.com/forum/general/183731/#msg899203
Tnx man, will keep this in mind! Appreciate good tips about my code!
Topic archived. No new replies allowed.