Comparing user input string to input file string

I am having trouble with a school assignment where I prompt the user to input a country name, then compare it to a file that has country names with their capitals. If the country is not in the list the user is prompted to input the capital of the country.
The text file is formatted like so

Canada Ottawa
Italy Rome
Russia Moscow

This is what I have so far, I cannot figure how to make the program check the entire file before proceeding to step where country isnt in list.

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
  #include <iostream>
#include<fstream>
#include<iomanip>
#include <string>
using namespace std;

int main(){
    
    string userCountry,country, userCapital, capital;
    ifstream fin;
    ofstream fout;
    
    fin.open("A3Q1_V1.dat");
    
    if (!fin)
    { cout<<"Error opening file"<<endl;
        return 0; }
    cout<<"•••••••••••••••••••••••••••••••••\n"<<"\tCountry's Capital Checker\n"<<"•••••••••••••••••••••••••••••••••\n";
    cout<<"\nWhich country would you like to check? (no spaces in name):  ";
    cin>>userCountry;
   
    while (fin>>country){
    if (country == userCountry) {
        fin>>capital;
        cout<< "\nThe capital of "<<userCountry<<" is "<<capital<<"\n\nThank you for using Country's Capital Checker\n\n";
        }
    else{ cout<<"\nThat country does not appear on the file.\n\nWhat is the capital of "<<userCountry<<"? ";
        cin>>userCapital;
        
    }
    }
    
    
    
    fin.close();
    return 0;
}
Last edited on
Hello fish692,

First suggestion would be to read the country and the capital at one time. Otherwise the if/else will through the reading of the file off.

Next suggestion would be to create a struct and make a vector of the struct or at least an array. This way you will not have to read the file every time you need to check for a match.

The if/else statements will print out the else part until you find a match. Not what you want.

You are using an input file for this program. Post the file or a good sample, so everyone will be using the same information. Do not make people guess at what you might have.

Andy
the assignment is focused on using the file and being able to check within it instead of creating an array of all the country names apart. When I run it without the else part it works and can properly find the correct capital. Is there a way of writing an if statement at the end if it doesnt find a match? Thanks
I think you want this pattern:

bool found = false;

while..
if ==
found = true;
..
much later:
if(!found)
....

this also lets you stop when you find it: you can put found in the while and stop looking after you find the match, do you see this?
I figured a way out, I added a counter in the if statement to go up to 1 if there was a match and added an if statement after the while loop if the counter remained at 0. Thanks for the help
Topic archived. No new replies allowed.