0

after the 2nd time the input function member does not work Y?

the problem is that I want to create a program that gets my house visitora and if they are in my file.txt welcome msg will be displayed, otherwise sorry u r not invited should be displayed

enter image description here

#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;
}
t.niese
  • 39,256
  • 9
  • 74
  • 101
  • 6
    Don't use `goto` in C++ unless you know what you're doing and you're in one of the few situations where it's actually a good idea. You'll end up with [spaghetti code](https://en.wikipedia.org/wiki/Spaghetti_code). – eesiraed Apr 14 '20 at 17:32
  • 1
    Please paste your data as text not images. Images do not scale well (it may not be readable on smaller screens, such as mobile phones). Also, text in the image cannot be copied and pasted. – Thomas Matthews Apr 14 '20 at 17:54
  • 2
    Prefer to use `std::string` for text, not character arrays. Arrays can overflow; `std::string` can dynamically expand. – Thomas Matthews Apr 14 '20 at 17:56
  • Does this answer your question? [Using getline() in C++](https://stackoverflow.com/questions/18786575/using-getline-in-c) – ChrisMM Apr 14 '20 at 17:59
  • Put counter variables in your `for`, as in `for (int counter = 1; ...)` Keep the declaration as close as possible to the first use. – tadman Apr 14 '20 at 18:01
  • To fix this you should start by breaking it down into functions, like one would be `bool guestAllowed(const std::string& guest)` which would return true or false if they're in the visitors list. – tadman Apr 14 '20 at 18:11

1 Answers1

-3

well I fixed it but by a very old-fashioned method which is goto directive to avoid using whileloop or other kinda loops i am curious about the method in which we avoid using these non-standard goto directives thanks

 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;
  • 2
    That `goto` needs to go away. This isn't done yet. `goto` isn't "old fashioned" it's dangerous and completely inappropriate here. You've taken difficult to follow code and made it even harder to figure out. This is a huge step backwards. – tadman Apr 14 '20 at 18:06
  • 1
    `goto` is neither "old-fashioned" nor "non-standard". Its a tool that can do great harm and its use-cases are very rare, this isnt one – 463035818_is_not_an_ai Apr 14 '20 at 18:14