阅读独特的数据

时间:2012-03-19 18:28:32

标签: c++ c

如果用户输入他已输入的USN号码,则以下代码正常工作。 但是,如果他进入相同的USN号码,那么它将被接受。我试过检查一下 USN号使用无限循环(;;),但我不知道如何退出无限循环。

是否有任何解决方案。或者请建议我如何终止无限循环。

void student :: GetData()
 {

  cout<<"Enter the number of students record wolud you like to enter(10 records as MAX): ";

  cin>>MAX;

  cout<<"\n Now enter the "<<MAX<<"students records\n";


  student so[10]; // SO is Student Object

 int i;

    for(i=0;i<MAX;i++)
      {
    cout<<"Enter the Name: ";
    cin>>so[i].name;
    cout<<"\nEnter the USN num.: ";
    cin>>so[i].USN;


     for(int k=1;k<i;k++)
    {
     if (so[i].USN == so[k].USN); // here it finds the if the entered USN number exist or not
        {
            cout<<"The USN number exist try new one: ";
            cin>>so[i].USN;    
        } // But after this statement if he enter the same exist USN number is accepting

          //I need you guys to help me out at this point
    }

    cout<<"\n Enter the Three Marks: ";

      for(int j=0;j<3;j++)
    {
      cin>>so[i].marks[j];
    }
 }
}

2 个答案:

答案 0 :(得分:0)

这样做:

if (so[i].USN == so[k].USN) {
   ...
}
else break;

你有语法问题

if (so[i].USN == so[k].USN); <-- the ; is wrong here because it would end the if right away

答案 1 :(得分:0)

要打破循环使用..'break'。这将立即退出循环并继续从循环的底部执行。或者,如果循环应该退出并使其成为条件的一部分,则使用变量来存储,例如:

bool shouldExit = false;
while (shouldExit == false)
{
   ...
   if (someCondition) {
      shouldExit = true;
   }
   ...
}