C++ HELP WITH POINTERS AND STRUCTURES Example from .csv file: This is what I hav
ID: 3763836 • Letter: C
Question
C++ HELP WITH POINTERS AND STRUCTURES
Example from .csv file:
This is what I have so far but it's not working:
Actual Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
//declare struct Country
struct Country {
string name; //object string name
double population; //object double population
};
//declare struct Node
struct Node {
Country ctry;
Node * next;
};
//create push
void push (Country item, Node * & list) {
Node * top = new Node;
top->ctry = item; //move pointer to ctry
top->next = list; //move pointer to next
list = top;
};
Node * world;
double printCountry (Node * world, string cName){
double countPOP = 0;
while(world!=NULL){ //iterate until end of list NULL
cout << "while l" << endl;
if (world->ctry.name == cName){
cout << "match" << endl;
countPOP = world->ctry.population;
cout << countPOP << endl;
}
world = world->next;
}
return countPOP;
}
int main(){
ifstream infile; //declare stream
infile.open("populations.csv"); //open file into stream
double fifties, seventies, nineties, tens, fifteens;
double population;
string name;
if (infile.fail()){ //evaluate if file opened
cout << "File open failed" << endl; //print error message
exit(0);
};
Node * countryList = NULL; //declare Node countryList
//loop through file lines
while (infile >> fifties >> seventies >> nineties >> tens >> fifteens){
getline(infile, name); //accept country name
population = fifteens;
Country c1 = {name, population};
push(c1, countryList);
};
infile.close();
/*
//print full country list
cout << "Country Data: " << endl;
Node * temp = countryList;
while(temp!=NULL) //iterate until end of list NULL
{
cout << temp->ctry.name //obj ctry
<< temp->ctry.population
<
temp = temp->next;
}
*/
string cName;
cin >> cName;
cout << "Search: " << cName << printCountry(countryList, cName) << endl;
return 0;
}
For all tasks, you should use the following data structure for storing a country and its current population: struct Country string name; double population; li struct Node Country ctry; Node next; )i Node * world;Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
//declare struct Country
struct Country {
string name; //object string name
double population; //object double population
};
//declare struct Node
struct Node {
Country ctry;
Node * next;
};
Node * world;
//create push
Node * NodeCreation(struct Country item)
{
struct Node * top;
top=new(struct Node);
if(top==NULL)
{
return 0;
}
else
{
top->ctry.name=item.name;
top->ctry.population=item.population;
top->next=NULL;
return top;
}
}
void push (struct Country item, Node * world)
{
Node * t;
Node * top = NodeCreation(item);
if(world==NULL)
{
world = top;
}
else
{
t=world;
while(t->next!=NULL)
{
t=t->next;
}
t->next=top;
world = t;
}
}
double printCountry (Node * world, string cName){
double countPOP = 0;
Node * tempWorld=world;
if(tempWorld==NULL)
cout<<"LIST IS EMPTY"<<endl;
else
{
while(tempWorld!=NULL)
{
if (tempWorld->ctry.name == cName)
{
cout << "match" << endl;
countPOP = tempWorld->ctry.population;
cout << "POPULATION IS:"<<countPOP << endl;
}
tempWorld = tempWorld->next;
}
}
return countPOP;
}
int main(){
ifstream infile; //declare stream
infile.open("populations.txt"); //open file into stream
double fifties, seventies, nineties, tens, fifteens;
double population1;
string name1;
if (infile.fail()){ //evaluate if file opened
cout << "File open failed" << endl; //print error message
exit(0);
};
world = NULL; //declare Node countryList
//loop through file lines
struct Country c1;
while (infile >> fifties >> seventies >> nineties >> tens >> fifteens>>name1){
population1 = fifteens;
c1.name=name1;
c1.population=population1;
push(c1, world);
}
infile.close();
string cName;
cout<<"Enter the country name to find the population:"<<endl;
cin >> cName;
cout << "Search country: " << cName << " Population (0 if not in the list): "<<printCountry(world, cName) << endl;
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.