2015-12-06 2 views
-1

Итак, я получил это задание, и у меня все работает отлично, за исключением вызова этой функции, которая должна отформатировать строку электронной почты. Если вы не вызываете эту функцию, я могу напечатать ее точно так, как говорит профессор, предположительно. Однако при вызове этой функции я не могу заставить ее возвращать информацию и печатать ее. Может ли кто-нибудь помочь мне с этим? Я потратил неудачное количество часов, пытаясь развить эту вещь.Как передать эту строку C++

//Function Prototypes 
std::string FormatEmailString(std::string from, std::string to, std::string subject, std::string msg); 
std::string GetInboxFile(std::string username); 
std::string GetLine(std::istream & sin); 


int main() 
{ 
    std::string email; 
    std::string from; 
    std::string to; 
    std::string subject; 
    std::string msg; 

    std::ifstream fin; 
    std::ofstream fout; 
    std::string user1; 

    fin.open(user1 + "-inbox.txt", std::ios::in); 

    while (fin.eof() == false) // I know fin.eof is not a good method to use. 
    {       // I tried while(fin) but it lets extra "From:" "To"... 

     email = GetLine(fin); //GetLine is a function call to getline. 
     from = GetLine(fin); 
     to = GetLine(fin); 
     subject = GetLine(fin); 
     msg = GetLine(fin); 

     std::string formatted = FormatEmailString(from, to, subject, msg); 
     fout << formatted; 

    } 
    std::cout << std::endl; 
    fin.close(); 
    std::cin.get(); 
    return 0; 
} 

std::string FormatEmailString(std::string from, std::string to, std::string subject, std::string msg) 
{ 

    std::ostringstream out; 
    std::ifstream fin; 

    std::string user1; 
    fin.open(user1 + "-inbox.txt", std::ios::in); 

    out << std::endl; 
    out << "From: " << from << std::endl; 
    out << "To: " << to << std::endl; 
    out << "Subject: " << subject << std::endl; 
    out << "Message: " << msg << std::endl; 

    fin.close(); 
    return out.str(); 

}//END FormatEmailString 

std::string GetLine(std::istream & sin) 
{ 

    std::string s; 
    std::getline(sin, s); 
    return s; 

}//END GetLine 

//FROM user1-inbox.txt TEXT FILE: 
#email    //This Line not printed 
abc    //FROM 
user1    //TO 
hello    //Subject 
How about lunch? //Message 
#email    //This line not printed 
abc    //From 
user1    //To 
Join the Dark Side //Subject 
We have cookies! //Message 
//END TEXT FILE 
+0

Каков ожидаемый результат? Какой результат вы получаете? –

+0

Ожидаемый результат: распечатать текстовый файл как: От: abc Кому: user1 Тема: Присоединиться к Темной стороне Сообщение: У нас есть файлы cookie! Я получаю пустое или зависание каждый раз, когда вызываю функцию FormatEmailString (от, до, subject, msg). –

+0

Почему вы создали свой собственный 'getline'? – erip

ответ

0

Вы не открываете файл, который хотите записать. Ваш fout используется только в двух местах ... в декларации, а затем u записывает в него.

+0

Чувак, его ответ на вопрос - причина ошибки. не так ли? – Dominik

+0

Мой плохой ... мой комментарий. – Phani

Смежные вопросы