0

我正在尝试使用参数在 C++ 中执行 .exe,但出现错误。.exe 执行的代码在底部附近。(result并且usernamechars)

实际执行代码:

//Write result to disk.
char args[100];
sprintf(args,"%s %d %d","write.exe",result,username);
system(args);
result = false;
goto start;
return 0;

完整的程序:

#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <sstream>
#include <time.h>
#include <windows.h>
using namespace std;

int main()
{
    //Declare SOME Variables
    int answer;
    int var_1;
    int var_2;
    int lowest;
    int highest;
    int user_answer;
    int random;
    char username_file,password_file,username,password;
    bool login;
    string result;

    //Get Name
    login:
    cout << "Enter your username!" << endl;
    cin >> username;
    cout << "Enter your password!" << endl;
    cin >> password;

    //Compare usernames and passwords
    ifstream infile3;
    infile3.open ("database.txt");
    std::string line;
    while (std::getline(infile3, line))
    {
        std::istringstream iss(line);
        int a, b;
        if (!(iss >> username_file >> password_file)) { break; } // error

        //Check if username exists.
        if (username_file == username)
        {
            if (password_file == password)
            {
                cout << "Logged in as " << username << endl;
                login = true;
            }
        }
        if (login == false)
        {
            cout << "Failed to login with username: " << username << endl;
            cout << "Try again!" << endl;
            goto login;
        }
    }
    infile3.close();
    //Start Tag
    start:
    //Get highest Variable.
    string string_1;
    ifstream infile;
    infile.open ("high.txt");
    getline(infile,string_1);
    infile.close();
    //Get lowest Variable.
    string string_2;
    ifstream infile2;
    infile2.open ("low.txt");
    getline(infile2,string_2);
    infile2.close();
    //Convert Variables
    stringstream ss(string_1);
    ss >> highest;
    stringstream sss(string_2);
    sss >> lowest;
    //Generate Random Numbers
    srand ( time(NULL) );
    var_1=lowest+rand()%(highest);
    var_2=lowest+rand()%(highest);
    //Calculate Answer
    answer = var_1*var_2;
    //Display Variables
    cout <<"What is the product of: "<< var_1 <<" x "<< var_2 << endl;
    cin >> user_answer;
    //Correct?
    if (user_answer == answer)
    {
        cout << "You are correct!" << endl;
        result = true;
    }
    else
    {
        cout << "Incorrect" << endl;
        cout << "The answer is:" << answer << endl;
    }
    //Write result to disk.
    char args[100];
    sprintf(args,"%s %d %d","write.exe",result,username);
    system(args);
    result = false;
    goto start;
    return 0;
}

错误:

C:\Users\HC\Desktop\Code\testmultiply.class.cpp||In function 'int main()':|
C:\Users\HC\Desktop\Code\testmultiply.class.cpp|97|error: cannot pass objects of non-trivially-copyable type 'std::string {aka class std::basic_string<char>}' through '...'|
C:\Users\HC\Desktop\Code\testmultiply.class.cpp|99|error: ambiguous overload for 'operator=' in 'result = false'|
C:\Users\HC\Desktop\Code\testmultiply.class.cpp|99|note: candidates are:|
D:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|543|note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>]|
D:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|551|note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>]|
D:\program files\codeblocks\mingw\bin\..\lib\gcc\mingw32\4.7.1\include\c++\bits\basic_string.h|562|note: std::basic_string<_CharT, _Traits, _Alloc>& std::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>; std::basic_string<_CharT, _Traits, _Alloc> = std::basic_string<char>]|
||=== Build finished: 6 errors, 0 warnings ===|
4

1 回答 1

1

你声明result为 astd::string然后你尝试存储false它里面的值。result = false;

您可能想要做的是创建result一个bool

于 2013-10-19T21:33:44.507 回答