Trouble with basic C++ Loop

shyy

Well-known Member
Joined
Nov 6, 2008
Messages
1,484
Hi everyone,

I am having trouble with this function.

What the function does is tell the user to input a city name. If the city name is found to output the results. If the city is not found to output "City not found". I got the function to work but the problem is that it repeats "City Not Found" 19 times. How would I modify the following code so it only outputs x amount that is in the .txt file being pulled from and only output once if the city is not found?

Thanks

Rich (BB code):
void QueryCity(City Data[])
{
	system("CLS");
	cout << "Look up which city name: ? ";
	string cityName;
	cin >> cityName;




	for (int i = 0; i < 20; i++)
	{


		if(Data.Name == cityName)
		{
			cout << "City Name: \t" << "# of Outages: \t" << "# of Total Customers: \t" << endl;
			cout << Data.Name << "\t\t";
			cout << Data.NumOfOutages << "\t\t";
			cout << Data.TotalCust << "\t";
			cout << endl;
		}
		else if (Data.Name != cityName)
		{
			cout << "City not found in the database" << endl;
		}
	}


	system("PAUSE");
	system("CLS");
}
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
You don't know if the city is found or not until you've checked all the array elements for a match. One way (there are surely others) would be to use a "flag" variable. Set it to true if you find a match.

Ex. (be warned, my syntax may be bad as I don't write cpp code):
Code:
void QueryCity(City Data[])
{
	system("CLS");
	cout << "Look up which city name: ? ";
	string cityName;
	cin >> cityName;
	bool found;




	for (int i = 0; i < 20; i++)
	{


		if(Data[i].Name == cityName)
		{
			cout << "City Name: \t" << "# of Outages: \t" << "# of Total Customers: \t" << endl;
			cout << Data[i].Name << "\t\t";
			cout << Data[i].NumOfOutages << "\t\t";
			cout << Data[i].TotalCust << "\t";
			cout << endl;
			found = true;
		}
	
	
	}
		
	If (!found)
		cout << "City not found in the database" << endl;


	system("PAUSE");
	system("CLS");
}
 
Upvote 0

Forum statistics

Threads
1,225,657
Messages
6,186,257
Members
453,347
Latest member
mrizkiii28

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top