0

I'm making a very basic email server and I need a function to show me how many emails the inbox of a user has. Each email sent has the word "#email" at the top so I'm using it as a keyword do count how many emails, but is not working and I don't know why. Can someone please tell me what is wrong or put me in the right direction?? thanks

int GetInboxCount(std::string username)
{
std::ifstream email(username + "-inbox.txt"); //opens the inbox file
std::string item;
int count = 0;
  while(!email.eof()) //read the file until the end
  {
    email >> item;
    if(item == "#email") //search for the word #email that is at the top of every email in the inbox
    {
        ++count;

    }
  }
email.close();
return count; //it should return the numbers of emails
Cristina
  • 55
  • 4
  • `while(!email.eof())` : [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) Most likely side effect is of-by-one on the count. – user4581301 Nov 27 '17 at 19:03
  • Question cannot be answered in its current state without guesswork. We need a sample of the email text file to make decent parsing suggestions. – user4581301 Nov 27 '17 at 19:11

1 Answers1

0

In order to accomplice your task you need to use std::find() to find the keyword #email on the item string. Also use while(email>>item) instead of while(!email.eof())

Jake Freeman
  • 1,706
  • 1
  • 6
  • 15
  • Easily fooled by placing "#email" somewhere inside the email message. – user4581301 Nov 27 '17 at 19:08
  • @user4581301 ok, but thats a design problem not a question problem, maybe the first five characters. – Jake Freeman Nov 27 '17 at 19:09
  • I agree with you. I don't think the question is currently in a state where it can be answered. – user4581301 Nov 27 '17 at 19:12
  • I'm sorry, I'm new to this... I know is easily fooled just by writing #email wherever, but that's the way the problem is. I need a function to count how many "#email" can find in the file I'm opening(username+ "-inbox.txt) and return me a number. @user4581301 – Cristina Nov 27 '17 at 19:23
  • 1
    @CristinaChung please explain how your current code fails to work. Put that in your question. Having to guess what is wrong does not make it an answerable question as far as StackOverflow is concerned. – drescherjm Nov 27 '17 at 19:28
  • 1
    @Cristina are you sure the file is being opened in the first place? That is currently not being tested. – user4581301 Nov 27 '17 at 19:35
  • Good point. Maybe the file is in a different folder than the current working directory so it is not read at all. Properly using a debugger (single step through this function looking at the variables) should help determine the cause of the problem. – drescherjm Nov 27 '17 at 19:38
  • @drescherjm so this functions returns the count and then I print it, but is not printing...let me check if it is opening the file. Thanks – Cristina Nov 29 '17 at 17:04
  • A debugger should help with that. – drescherjm Nov 29 '17 at 17:16