#include<iostream>
#include<string>
using namespace std;
int main()
{
int list[25],vote[25];
bool found;
int count,first,mid,last;
char condition;
double high = 0,total=0;
string searchitem,name[25],winner;
cout <<"How many names?" <<endl;
cin>>count;
cout <<" Enter the "<<count<< " candidates last name and the number of votes received " <<endl;
for (int i=0;i<count;i++)
{
cin >>name[i];
cin >> vote[i];
total = total+vote[i];
}
cout <<"Candidate names ,number of votes and their percentage of votes are ..."<<endl;
for (int i=0;i<count;i++)
{
cout << name[i]<<endl;
cout << vote[i]<<endl;
cout << (vote[i]*100)/total<<endl;
if(vote[i]>high)
{
high = vote[i];
winner = name[i];
}
}
cout << " The winner of the election is " << winner << endl;
cout << "Do you have anything more to do?y/n?" << endl;
cin >> condition;
while (condition=='y' || condition=='Y')
{
found = false;
cout<<"Enter name you want to search \n";
cin>>searchitem;
first=0;
last=count-1;
while ((first<=count)&&!found)
{
mid=(first+last)/2;
if (name[mid]==searchitem)
{
found=true;
total = total-vote[mid];
cout << "Enter the new number of votes for " << name[mid] << endl;
cin >> vote[mid];
total = total + vote[mid];
}
else if (name[mid]>searchitem)
{
last=mid-1;
}
else first=mid+1;
}
if (!found)
{
cout<<" Sorry that name was not found \n";
}
cout <<"Candidate names ,number of votes and their percentage of votes are ..."<<endl;
high = 0;
for (int i=0;i<count;i++)
{
cout << name[i]<<endl;
cout << vote[i]<<endl;
cout << (vote[i]*100)/total<<endl;
if(vote[i]>high)
{
high = vote[i];
winner = name[i];
}
}
cout << " The winner is " << winner << endl;
cout << "Do you have anything more to do?y/n? "<<endl;
cin >> condition;
}
}
Хорошо. Теперь я пытался поэкспериментировать с этим, и когда в нем участвуют 5 имен, я могу изменить голоса третьего и четвертого имени и отобразить результаты.
Но когда я пытаюсь найти имя, фамилию и имя, моя программа просто останавливается!
Почему это происходит ??? И решение может помочь мне сэкономить время от попыток сломать стену голыми руками.