You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++症状录入程序报错:无法将void转换为std::string求助

Hey there! Let's break down your issues step by step and fix up that code for you.

1. Why You're Seeing That Compile Error

First, the error you're hitting:

error: could not convert ‘symptom.std::vector...push_back(...)’ from ‘void’ to ‘std::string’

This happens because std::vector::push_back() returns nothing (its return type is void), but your disease function declares that it returns a std::string. You're trying to return the result of push_back, which doesn't make sense—push_back just adds an element to the vector and calls it a day.

2. Other Hidden Issues in Your Code

Beyond the compile error, there are a few critical logic/runtime problems:

  • Vector Out-of-Bounds Access: When you call disease(dry[patientn], namex[patientn], patientn), the dry vector is empty at first. Accessing dry[0] when the vector has no elements is undefined behavior (your program might crash or behave unpredictably).
  • Pass-by-Value Means No Permanent Changes: Your disease function takes vector<string> symptom as a parameter—this creates a copy of the original vector. Any changes you make to symptom inside the function won't affect the original dry or sneeze vectors in main.
  • Unfinished Function Logic: If the user enters something other than Y/N, your function doesn't return a value, which is another undefined behavior.
  • Poor Data Structure Choice: Having separate vectors for each symptom (dry, sneeze) makes your code hard to maintain—adding a new symptom later would require creating a whole new vector and updating every part of your code that handles patients.
3. Fixed & Improved Code

Here's a revised version that fixes all these issues and makes your code cleaner:

#include <iostream>
#include <vector>
#include <string>

// Helper function to get valid Y/N input for a symptom
bool getValidSymptomResponse(const std::string& patientName, const std::string& symptom, std::string& result) {
    std::string input;
    std::cout << "Is " << patientName << " experiencing " << symptom << "? (Y/N): ";
    std::cin >> input;

    if (input == "Y" || input == "y") {
        result = "Y";
        return true;
    } else if (input == "N" || input == "n") {
        result = "N";
        return true;
    } else {
        std::cout << "Oops! Please enter only Y or N.\n";
        return false;
    }
}

// Struct to group all data for a single patient
struct Patient {
    std::string name;
    std::string dryCough;
    std::string sneezing;
};

int main() {
    std::vector<Patient> patients;
    const int totalPatients = 2;

    for (int i = 0; i < totalPatients; ) {
        Patient newPatient;
        std::cout << "Enter patient name: ";
        std::cin >> newPatient.name;

        // Keep asking until we get valid input for each symptom
        while (!getValidSymptomResponse(newPatient.name, "dry cough", newPatient.dryCough));
        while (!getValidSymptomResponse(newPatient.name, "sneezing", newPatient.sneezing));

        patients.push_back(newPatient);
        i++;
    }

    // Print out all patient data neatly
    std::cout << "\n--- Patient Symptom Summary ---\n";
    for (const auto& patient : patients) {
        std::cout << "Patient: " << patient.name << "\n";
        std::cout << "- Dry Cough: " << patient.dryCough << "\n";
        std::cout << "- Sneezing: " << patient.sneezing << "\n";
        std::cout << "------------------------------\n";
    }

    return 0;
}
4. Key Optimizations & Best Practices
  • Use a Struct for Patient Data: Grouping a patient's name and symptoms into a Patient struct makes your code far more readable and scalable—adding a new symptom (like "fever") just means adding a new field to the struct, not a whole new vector.
  • Pass Parameters by Reference: Using const std::string& for read-only data avoids unnecessary copies, and std::string& lets us modify the original value directly without copying vectors.
  • Input Validation Loops: Instead of exiting when the user enters bad input, we loop until they enter valid Y/N—this is much more user-friendly.
  • Avoid using namespace std: While it's common in small programs, it can cause naming conflicts in larger projects. Using std:: explicitly makes your code clearer.
  • Cleaner Output: Adding separators and clear labels makes the final summary easier to read.

内容的提问来源于stack exchange,提问作者Franklin Fredric

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 12:58:12