help in C++ please

please I need your help in my project in C++

I dont know how to do it this is the project::::


write a program that lists European, Middle-Eastern, or Asian Countries and thier capitals. the program retrieves this information from an input file that the user specifies during program execution. The program retrieves this information from an input file that the user specifies during program execution. The data can be stored in a one-dimensional array or a two-dimensional array. the data is displayed in a tabular format at the end if the program execution.

in your project you should show the utilization of the following:

1. using user defined functions
2. using appropriate selection
3. using loops
Something like this? I'm not sure what "appropriate selection" is, but it certainly uses user-defined function and a loop.
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
#include <string>
#include <vector>
#include <utility>
#include <fstream>
#include <iostream>

class CountryList
{
    std::vector< std::pair< std::string, std::string> > m_list;
public:
    CountryList(std::string filename);
    friend std::ostream& operator<<(std::ostream& lhs, CountryList& rhs);
};

std::ostream& operator<<(std::ostream& lhs, CountryList& rhs)
{
    for (auto& it : rhs.m_list)
        lhs << it.first << '\t' << it.second << '\n';

    return lhs;
}

CountryList::CountryList(std::string filename)
{
    std::ifstream fin(filename);
    if (!fin.is_open())
    {
        std::cerr << "could not open " << filename << std::endl;
        return;
    }

    do
    {
        std::pair<std::string, std::string> temp;
        fin >> temp.first >> temp.second;
        m_list.push_back(temp);
    } while (fin.good());
}

int main()
{
    CountryList cl("input.txt");
    std::cout << cl;
}
Last edited on
thanx but this program doesnt work

and the project need to be with array
I dont know how to do it this is the project
What exactly are your problems?

doesnt work
Without any more specific hint, its really difficult to give some help. Try your own solution. Then anyone may help fixing your bugs.
This should get you going! It reads all data from a text file and adds it to 2 arrays. The data is then displayed in a formatted table. This is as far as I will go. I cannot do all the work for you ;)

Output looks like:
1
2
3
4
France        Paris
Cuba          Havana
Fiji          Suva
Colombia      Bogota


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
 

#include <iostream>
#include <iomanip>
#include <fstream>


using namespace std;


//function prototypes
int buildArrays();

//declare variables
string CountryName[100]; //Array of country names
string CountryCapital[100]; //Array of country capitals
int totalCountries; //Total number of countries
int i=0; //Counter

int main()
{

	//the buildArrays function reads all the data from the input file and determines the total number of countries in the file. 
	totalCountries=buildArrays();
	
	//display all the output
	for(i=0;i<=totalCountries;i++)
	{
		cout<<"\n";
		cout<<setw(15) << left << CountryName[i] << setw(5) << right <<CountryCapital[i];  	    
	}
	
	return 0;
}

//custom function to read all the data from the file
int buildArrays()
{	 

	
	//open the text file
	ifstream inFile;
	inFile.open( "input.txt" );
	
	//if the text file doesn't open display an error
	if( inFile.fail() )
  	{
  		cout << "The input.txt file failed to open";
  		exit(-1);
  	}
	
	//while there is data in the file add it to the arrays
	while(inFile)
	{
		inFile>> CountryName[i];
		inFile>> CountryCapital[i];
		i++;				
	}
	
	//return the total number fo countries
	return i-2;
}
Last edited on
can u do me even a small file cuz I dont know how to use file list
Topic archived. No new replies allowed.