Hello there,
so i have this current project for school and i`m kinda stuck the program i`m suppose to write is this :
__________________________________________________ ____
Each year many tourist visit our country please write a program that collect the following information:
Name(first,middle,last)
sex
country
age
profession
Also the program shoud be bale to print out the following information on request:
1.What is the average age of the tourists visited
2. What is the name the age and the profession of the youngest women visited
3.what countries we had visitors from
4. from what country we got most visits
__________________________________________________ ____

Ok so far i`m this far and i`m kinda lost so if anyone can help me i will be very happy Thank you for all of your help in advance!
PS:i know it is a mess i`m kinda new to this

#include <iostream>
#include <cctype>
#include <string>
using namespace std;
void average_age(char x[][20]);
void youngest_women();

const int count=10;

int main()
{

char Fname[count][20];
char Sname[count][20];
char Lname[count][20];
char sex[count][20];
char country[count][20];
int age[count];
char profesion[count][20];
int x;

for(int i=0;i<=count;i++){
cout<<"Enter the First name of the tourist"<< endl;
cin>>Fname[i];
cout<<"Enter the Surname of the tourist"<< endl;
cin>>Sname[i];
cout<<"Enter the Family name of the tourist"<< endl;
cin>>Lname[i];
cout<<"Enter the sex of the tourist(m/f):"<< endl;
sex: cin>>sex[i];
if (sex[i] != "m"){
if (sex[i] != "f")goto sex;
}
cout<<"Enter the coutry the tourist is from"<< endl;
cin>>country[i];
country[i] = toupper(country[i]);
cout<<"Enter the age of the tourist"<< endl;
cin>>age[i];
cout<<"Enter the proffession of the tourist"<< endl;
cin>>profesion[i];
}
cout<<"What information would you like to know about our tourists?"<< endl;
cout<<"1.What is the average age of the tourists visited Bulgaria?"<< endl;
cout<<"2.What is the name of the youngest women visited Bulgaria?"<< endl;
cout<<"3.From wich countries we got tourists this year?"<< endl;
cout<<"Please chose from 1-3"<< endl;
cin>>x;
switch (x)
{
case 1: average_age(age[10][20]);break;
case 2: youngest_women();break;
case 3: country_list();break;
default: cout<<"Error";
}
return 0;
}

void average_age(char x[][20])
{
int average=0;
for(int i=0;i<=count;i++)
average +=age[i];
cout<< "The average age of the tourists is: "<<average<< " years"<< endl;
}

void youngest_women()
{
int m=0,y=0;
for(int i=0;i<=count;i++){
if (y>age[i]){
if (sex=='f'){
y=age[i];
m=i;
}
}
}
cout<<"The youngest women visited Bulgaria is: " <<Fname[m]<<" "<<Sname[m]<<" "<<Lname[m]<<" at age of "<<age[m]<<" from "<<country[i]<<endl;

}

void country_list()
{
int a[150] = {0, 0};
char list[][20]{AFGHANISTAN, ALBANIA, ALGERIA ,ANDORRA, ANGOLA, ARGENTINA, ARMENIA, AUSTRALIA, AUSTRIA, AZERBAIJAN, BAHAMAS, BAHRAIN, BANGLADESH, BARBADOS, BELARUS,
BELGIUM, BELIZE, BENIN, BHUTAN, BOLIVIA, BOTSWANA, BRAZIL, BRUNEI, USA, BURKINA, BURUNDI, CAMBODIA, CAMEROON, CANADA, CHAD, CHILE, CHINA, COLOMBIA, COMOROS, CONGO,
CROATIA, CUBA, CYPRUS, DENMARK, DJIBOUTI, DOMINICA, ECUADOR, EGYPT, ERITREA, ESTONIA, ETHIOPIA, FIJI, FINLAND, FRANCE, GABON, GAMBIA, GEORGIA, GERMANY, GHANA, GREECE,
GRENADA, GUATEMALA, GUINEA, GUYANA, HAITI, HONDURAS, HUNGARY, ICELAND, INDIA, INDONESIA, IRAN, IRAQ, IRELAND, ISRAEL, ITALY, JAMAICA, JAPAN, JORDAN, KAZAKHSTAN, KENYA,
KIRIBATI, KOSOVO, KUWAIT, KYRGYZSTAN, LAOS, LATVIA, LEBANON, LESOTHO, LIBERIA, LIBYA, LIECHTENSTEIN, LITHUANIA, LUXEMBOURG, MACEDONIA, MADAGASCAR, MALAWI, MALAYSIA, MALDIVES,
MALI, MALTA, MAURITANIA, MAURITIUS, MEXICO, MICRONESIA, MOLDOVA, MONACO, MONGOLIA, MONTENEGRO, MOROCCO, MOZAMBIQUE, MYANMAR, NAMIBIA, NAURU, NEPAL, NETHERLANDS, NICARAGUA, NIGER,
NIGERIA, NORWAY, OMAN, PAKISTAN, PALAU, PANAMA, PARAGUAY, PERU, PHILIPPINES, POLAND, PORTUGAL, QATAR, ROMANIA, RWANDA, SAMOA, SENEGAL, SERBIA, SEYCHELLES, SINGAPORE, SLOVAKIA,
SLOVENIA, SOMALIA, SPAIN, SUDAN, SURINAME, SWAZILAND, SWEDEN, SWITZERLAND, SYRIA, TAIWAN, TAJIKISTAN, TANZANIA, THAILAND, TOGO, TONGA, TUNISIA, TURKEY, TURKMENISTAN, TUVALU,
UGANDA, UKRAINE, URUGUAY, UZBEKISTAN, VANUATU, VENEZUELA, VIETNAM, YEMEN, ZAMBIA, ZIMBABWE}
for(int i=0; i<=count; i++){
for(int n=0; n<=150; n++){
if(strcmp(country[i],list[n]) == 0){
a[n] += 1;
break;
}
}
}
for(int i=0; i<=150; i++){
if(a[i] != 0){
cout<<list[i]<<" - "<<a[i]<<" tourists"<<endl;
}
}
for(int i=0; i<=150; i++){
if(y<a[i]){
y=a[i];
m=i;
}
}
cout<<"The countrie with the most visitors is :"<<country[m]<< " with " <<y<< " visitors"<< endl;
}

Reply With Quote

Are you getting any compilation or run-time errors? You really haven't given us much to go on.

I will say, though, that this is not correct:

const int SIZE = 10;
int anArray[SIZE] = {0};

for (int i = 0; i <= SIZE; i++) {
 /* ... */
}

I see at least 7 places where you have done similar in your program. When you create an array of SIZE elements the indexes start at zero (0) and end at SIZE-1 (which is 9 in my example). Change all your less-than-or-equal-to operators (x <= y) to simply less-than (x < y) operators.

Also, I noticed that your country_list() function hasn't set up the "list" array properly. All of those country names should be string literals. To make them literals, you must place them in "double quotes". Without proper use of quotes, the compiler thinks that those are all variable names instead of string literals.

It may not be a complete solution, but at least it's a step in the right direction.

Edited 6 Years Ago by Fbody: n/a

Thanks for your answer yes i get like 30 errors and was hammering my head how to clean them up. Thanks one more time

Thanks for your answer yes i get like 30 errors and was hammering my head how to clean them up. Thanks one more time

Can you be more specific? I may not have covered all your errors...

Ok here is how far i have gone now everything is ok (no sytax errors) the program is working BUT! The function for showing the youngest women isnt ok it just show the youngest person no matter of the sex and i`m not sure how to correct it.
And my second problem is the last function when i try to use the the program overload and crashesh any ideas anyone please?

#include <iostream>
#include <cctype>
#include <string>
using namespace std;
void average_age();
void youngest_women();
void country_list();

	const int count=2;
	int age[count];
	int sex[count];
	char Fname[count][20];
	char Sname[count][20];
	char Lname[count][20];	
	char country[count][20];
	char profesion[count][20];
	
int main()
{



	int x;
	
	for(int i=0;i<count;i++){
		cout<<"Enter the First name of the tourist"<< endl;
		cin>>Fname[i];
		cout<<"Enter the Surname of the tourist"<< endl;
		cin>>Sname[i];
		cout<<"Enter the Family name of the tourist"<< endl;
		cin>>Lname[i];
		cout<<"Choose the sex of the tourist:"<< endl;
		cout<<"0.Male"<<endl;
		cout<<"1.Female"<<endl;
		sex: cin>>sex[i];
		if (sex[i] != 0){
			if (sex[i] != 1)goto sex;
		}
		cout<<"Enter the coutry the tourist is from(use only upper case letters)"<< endl;
		cin>>country[i];
		cout<<"Enter the age of the tourist"<< endl;
		cin>>age[i];
		cout<<"Enter the proffession of the tourist"<< endl;
		cin>>profesion[i];
	}
begining:	cout<<"What information would you like to know about our tourists?"<< endl;
	cout<<"1.What is the average age of the tourists visited Bulgaria?"<< endl;
	cout<<"2.What is the name of the youngest women visited Bulgaria?"<< endl;
	cout<<"3.From wich countries we got tourists this year?"<< endl;
	cout<<"Please chose from 1-3(4 to quit)"<< endl;
	cin>>x;
	switch (x)
	{
		case 1: average_age();goto begining;
		case 2: youngest_women();goto begining;
		case 3: country_list();goto begining;
		case 4: goto end;
		default: goto begining;
	}
end:	return 0;
}

void average_age()
{
	int average=0;
	for(int i=0;i<count;i++)
		average +=age[i];
	cout<< "The average age of the tourists is: "<<average/count<< " years"<< endl;
}

void youngest_women()
{
	int m=0,y=0;
	for(int i=0;i<count;i++){
		if(sex[i]){
			if(y>age[i]){
			y=age[i];
			m=i;
			}
		}
		cout<<"The youngest women visited Bulgaria is: " <<Fname[m]<<" "<<Sname[m]<<" "<<Lname[m]<<" at age of "<<age[m]<<" from "<<country[m]<<endl;
	}


}

void country_list()
{
	int y=0,m=0;
	int a[150] = {0, 0};
char list[][20]={"AFGHANISTAN", "ALBANIA", "ALGERIA" ,"ANDORRA", "ANGOLA", "ARGENTINA", "ARMENIA", "AUSTRALIA", "AUSTRIA", "AZERBAIJAN", "BAHAMAS", "BAHRAIN", "BANGLADESH", "BARBADOS", "BELARUS",
"BELGIUM", "BELIZE", "BENIN", "BHUTAN", "BOLIVIA", "BOTSWANA", "BRAZIL", "BRUNEI", "USA", "BURKINA", "BURUNDI", "CAMBODIA", "CAMEROON", "CANADA", "CHAD", "CHILE", "CHINA", "COLOMBIA", "COMOROS", "CONGO",
"CROATIA", "CUBA", "CYPRUS", "DENMARK", "DJIBOUTI", "DOMINICA", "ECUADOR", "EGYPT", "ERITREA", "ESTONIA", "ETHIOPIA", "FIJI", "FINLAND", "FRANCE", "GABON", "GAMBIA", "GEORGIA", "GERMANY", "GHANA", "GREECE",
"GRENADA", "GUATEMALA", "GUINEA", "GUYANA", "HAITI", "HONDURAS", "HUNGARY", "ICELAND", "INDIA", "INDONESIA", "IRAN", "IRAQ", "IRELAND", "ISRAEL", "ITALY", "JAMAICA", "JAPAN", "JORDAN", "KAZAKHSTAN", "KENYA",
"KIRIBATI", "KOSOVO", "KUWAIT", "KYRGYZSTAN", "LAOS", "LATVIA", "LEBANON", "LESOTHO", "LIBERIA", "LIBYA", "LIECHTENSTEIN", "LITHUANIA", "LUXEMBOURG", "MACEDONIA", "MADAGASCAR", "MALAWI", "MALAYSIA", "MALDIVES",
"MALI", "MALTA", "MAURITANIA", "MAURITIUS", "MEXICO", "MICRONESIA", "MOLDOVA", "MONACO", "MONGOLIA", "MONTENEGRO", "MOROCCO", "MOZAMBIQUE", "MYANMAR", "NAMIBIA", "NAURU", "NEPAL", "NETHERLANDS", "NICARAGUA", "NIGER",
"NIGERIA", "NORWAY", "OMAN", "PAKISTAN", "PALAU", "PANAMA", "PARAGUAY", "PERU", "PHILIPPINES", "POLAND", "PORTUGAL", "QATAR", "ROMANIA", "RWANDA", "SAMOA", "SENEGAL", "SERBIA", "SEYCHELLES", "SINGAPORE", "SLOVAKIA",
"SLOVENIA", "SOMALIA", "SPAIN", "SUDAN", "SURINAME", "SWAZILAND", "SWEDEN", "SWITZERLAND", "SYRIA", "TAIWAN", "TAJIKISTAN", "TANZANIA", "THAILAND", "TOGO", "TONGA", "TUNISIA", "TURKEY", "TURKMENISTAN", "TUVALU",
"UGANDA", "UKRAINE", "URUGUAY", "UZBEKISTAN", "VANUATU", "VENEZUELA", "VIETNAM", "YEMEN", "ZAMBIA", "ZIMBABWE"};
	for(int i=0; i<=count; i++){
		for(int n=0; n<=150; n++){
			if(strcmp(country[i],list[n]) == 0){
				a[n] += 1;
				break;
			}
		}
	}
	for(int k=0; i<150; k++){
		if(a[k] != 0){
			cout<<list[k]<<" - "<<a[k]<<" tourists"<<endl;
		}
	}
	for(int s=0; s<150; s++){
		if(y<a[s]){
		y=a[s];
		m=s;
		}
	}
	cout<<"The countrie with the most visitors is :"<<country[m]<< " with " <<y<< " visitors"<< endl;
}

IMHO, I think you'll have to change your youngest_women function to something like:

void youngest_women(){
	int m=0,y=age[0];
	for(int i=1;i<count;i++){
		if(sex[i]==1){
			if(y>age[i]){
				y=age[i];
				m=i;
			}
		}
	}   //end of for
	cout<<"The youngest women visited Bulgaria is: " <<Fname[m]<<" "<<Sname[m]<<" "<<Lname[m]<<" at age of "<<age[m]<<" from "<<country[m]<<endl;
}

First of all your cout should come after the for loop.
Secondly, I changed the if condition to

if (sex[i] == 1)

This is optional (since you are taking females as 1) but I think it becomes more compiler independent(others please clarify).
Thirdly assign y to the first array element.

y = age[0];

(i think you can guess why)

hope it helps...
cheers....

Edited 6 Years Ago by NP-complete: n/a

as far as your 2nd prob is concerned look at line no. 100 and 101. You forgot to change the <= to < as already advised by Fbody. Also at line no. 108 you have to change the i to k.

Other than that think about the logic of the function. I think your logic is not correct.
You again have the same problem. Assign y to the first element of the array.

Edited 6 Years Ago by NP-complete: n/a

I`m just not sure how to make the algorithm for the last function this is the best idea i came up with if anyone can suggest me other approche i will try it :D

First do the changes i mentioned. It will work just fine. Assign y=a[0] and change the loop conditions. Also in line no 119 change it to something like :

cout<<"The countrie with the most visitors is :"<<list[m]<< " with " <<y<< " visitors"<< endl;

you were printing out country[m] it should be list[m] as your loop goes from 0-149 and your country array has got only 2 elements.


Your function works... By logical error i meant that y=0 thing.
Enjoy.

Edited 6 Years Ago by NP-complete: n/a

Let's start by looking at your youngest_woman function...

void youngest_women()
{
	int m=0,y=0;
	for(int i=0;i<count;i++){
		if(sex[i]){
			if(y>age[i]){
			y=age[i];
			m=i;
			}
		}
		cout<<"The youngest women visited Bulgaria is: " <<Fname[m]<<" "<<Sname[m]<<" "<<Lname[m]<<" at age of "<<age[m]<<" from "<<country[m]<<endl;
	}
 
 
}

The core logic of what you have created is correct, it really just needs a couple tweaks. Generally, when finding Max/Min values, it's better to start by assuming the first element of the range is the Max/Min value. By making this assumption, you give yourself a good concrete spot (and a potentially valid result value) to start from to compare the other elements to. If you start with some random value, you run the risk that the random starting value is larger/smaller than the actual Max/Min which generates invalid results.
This is one way to find the Min value:

const int SIZE = 10;
int myElements[SIZE];

/* ... populate the myElements array ... */

//declare misc. variables...
int minValue, minValueIndex;

// begin minimum detection...
for (int i = 0; i < SIZE; i++) {
  if (i == 0) {
    minValue = myElements[i];
    minValueIndex = i;
  } else if (myElements[i] < minValue) {
      minValue = myElements[i];
      minValueIndex = i;
    }  //end if
  }  //end if
} //end for

/* ... output to report your results ... */

Here is another way:

const int SIZE = 10;
int myElements[SIZE];

/* ... populate the myElements array ... */

//declare misc. variables...
int minValue, minValueIndex=0;

// begin minimum detection...
minValue = myElements[minValueIndex];
for (int i = 1; i < SIZE; i++) {
  if (myElements[i] < minValue) {
    minValue = myElements[i];
    minValueIndex = i;
  }  //end if
} //end for

/* ... output to report your results ... */

Once you have the detection working, add the additional if to determine if the person is Male or Female. I think your original 'm'/'f' method was a better way, but the new 0/1 method works as well.

Your second issue sounds like an array boundary issue, but I'm having a hard time following your code due to lack of commenting.

Edited 6 Years Ago by Fbody: n/a

Thanks alot for the help of both of you! Thanks to you guys everything is now working corret and i even got an A :D Thanks one more time! And if you ever visiting Bulgaria you got a beer from me! :D

This question has already been answered. Start a new discussion instead.