C++ 什么';我的C+有什么问题+;源代码,它在第一次之后不运行?

C++ 什么';我的C+有什么问题+;源代码,它在第一次之后不运行?,c++,C++,第二次之后,输入功能成员不工作Y 问题是,我想创建一个程序来获取我的访客a,如果他们在我的文件中。txt欢迎消息将被显示,否则对不起,你没有被邀请应该被显示 #include <fstream> #include <iostream> #include <string.h> using namespace std; int main() { fstream file; file.open("guests.txt"); if (file.is_op

第二次之后,输入功能成员不工作Y

问题是,我想创建一个程序来获取我的访客a,如果他们在我的文件中。txt欢迎消息将被显示,否则对不起,你没有被邀请应该被显示

#include <fstream>
#include <iostream>
#include <string.h>
using namespace std;
int main() {
  fstream file;
  file.open("guests.txt");

  if (file.is_open())
    cout << "OK";
  else
    cout << "Error";

  char user = 'n';

  while (user == 'n') {
    int counter = 1;
    file.seekg(0);
    char visitor[15], guest[15];

    clog << " What is your name? ";
    cin.getline(visitor, 15);

    for (counter; counter <= 100; counter++) {
      file >> guest;

      if (strcmp(guest, visitor) == 0) {
        cout << " Welcome! ";
        goto label1;
      }
    } // end of for loop

    cerr << " Sorry dear " << visitor << " you are not invited. ";

  label1:
    cout << '\n' << " Do you want to exit ? ( y / n ) ";
    cin >> user;
  }

  return 0;
}

#包括
#包括
#包括
使用名称空间std;
int main(){
流文件;
打开(“guests.txt”);
if(file.is_open())

cout我很好地修复了它,但是使用了一种非常老式的方法,即goto指令,以避免使用whileloop或其他类似的循环 我对我们避免使用这些非标准goto指令的方法感到好奇 谢谢

fstream文件;
打开(“guests.txt”);

如果(C.I.SyOpOn())CUT在C++中不使用<代码> Goto < /Cord>,除非你知道你在做什么,你处于少数几个实际情况下是好主意的情况下。你最终会把你的数据粘贴为文本而不是图像。图像不好缩放(它可能在小屏幕上,如手机)上是不可读的。。此外,无法复制和粘贴图像中的文本。首选使用
std::string
作为文本,而不是字符数组。数组可能溢出;
std::string
可以动态扩展。这是否回答了您的问题?请将计数器变量放入
for
,如
for(int counter=1;…)
使声明尽可能靠近第一次使用。该
goto
需要取消。这还没有完成。
goto
不是“老式的”这是危险的,完全不合适的。你已经很难理解代码,而且更难理解。这是一个巨大的倒退。
goto
既不是“老式”也不是“非标准”。它是一个会造成巨大伤害的工具,它的用例非常罕见,这不是一个
 fstream file; 
    file.open("guests.txt" );
    if ( file.is_open ( ) ) cout << " OK"; else cout << " Error ";
label: file.seekg(0);
    clog <<'\n'<<  " Name : ";  cin.getline(visitor, 15);
    i = 1;
    label1 : file >> guest;
    if (strcmp(guest, visitor) == 0) {
        cout << '\n' << "Dear " << visitor << " welcome to the party. ";
        goto label;

    }
    else {
        i++;
        if (i <= 100) goto label1 ; else    cout << '\n' << " U R not invited ";
    }
    goto label;