添加stat()检查后,cin.get()中断了

时间:2015-10-26 03:27:04

标签: c++ cin stat

所以我想在dir上添加一个stat()检查。编译后,它开始抛出一些非常奇怪的东西。这是我得到的:

#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <boost/algorithm/string.hpp>
#include <sys/stat.h>

using namespace std;
using namespace boost;
struct stat sb;

int main()
{
    cout<<" \n";
    cout<<"                 Account Progam\n"
    cout<<" \n";
    cout<<"             Created by: Illyduss\n";
    cout<<"                      2015\n";
    cout<<" \n";
    cout<<" \n";
    cout<<" \n";
    std::string account;
    cout<<"Do you already have an account? \n";
    cout<<"Type 'NEW' to create an account or enter your account name.\n";
    cout<<"Account Name: ";
    cin>> account;
    cin.ignore();
    string str1(account);
    to_upper(account);
    if (account == "NEW"){

        ...Some if checking here and junk...

        ...And then what was recently added the bugs my cin.get()...

    else {
        cout<< "Welcome back, '" << account << "'!\n";
        struct stat sb;
        if (stat("/home/user/Account Program/accounts",&sb) == 0 && S_ISDIR(sb.st_mode)){
            cout<<"Please enter your password: ";
        }
        else {
            cout<< "There is no account by that name, please type NEW to create a new account.\n";
        }
        }
    }
    cin.get();
}

我添加了cin.get()以便在终端运行后保持终端打开所以我可以看到它们是什么,但我相信它与我的dir检查相冲突,因为它运行得很好,直到我添加了在尝试打开帐户目录之前,请检查是否已经创建了帐户。

具体来说,我收到了这些错误:

intro.cpp:112:2: error: ‘cin’ does not name a type
  cin.get();
  ^
intro.cpp:113:1: error: expected declaration before ‘}’ token
 }
 ^
user@computer:~/Account Program$ g++ intro.cpp
intro.cpp:112:2: error: ‘cin’ does not name a type
  cin.get();
  ^
intro.cpp:113:1: error: expected declaration before ‘}’ token
 }
 ^

同样它正在折腾我用来关闭int main()的错误就像一旦它到达dir检查它把我扔出我的int main()?我没有正确关闭stat()吗?

1 个答案:

答案 0 :(得分:0)

答案是:

#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <boost/algorithm/string.hpp>
#include <sys/stat.h>

using namespace std;
using namespace boost;
struct stat sb;

int main()
{
    cout<<" \n";
    cout<<"                 Account Progam\n"
    cout<<" \n";
    cout<<"             Created by: Illyduss\n";
    cout<<"                      2015\n";
    cout<<" \n";
    cout<<" \n";
    cout<<" \n";
    std::string account;
    cout<<"Do you already have an account? \n";
    cout<<"Type 'NEW' to create an account or enter your account name.\n";
    cout<<"Account Name: ";
    cin>> account;
    cin.ignore();
    string str1(account);
    to_upper(account);
    if (account == "NEW"){

        ...Some if checking here and junk...

        ...And then what was recently added the bugs my cin.get()...

    else {
        cout<< "Welcome back, '" << account << "'!\n";
        struct stat sb;
        if (stat(("/home/user/Account Program/accounts/"+account).c_str(),&sb) == 0 && S_ISDIR(sb.st_mode)){
            cout<<"Please enter your password: ";
        }
        else {
            cout<< "There is no account by that name, please type NEW to create a new account.\n";
        }
    }
    cin.get();
}

我在最后一个之后有一个额外的}并且需要添加正在检查的子目录(&#34; / home / user / Account Program / accounts /&#34; + account).c_str ()。