我的C++程序出现错误

I am getting errors in my C++ program

本文关键字:错误 程序 C++ 我的      更新时间:2023-10-16

我目前正在创建一个天气应用程序,该应用程序给我一个错误。它说!=有问题,但我不确定出了什么问题,所以任何人都可以提供帮助。它给了我错误操作数类型不兼容("std::string *"和"const char *")(有 4 个)和"!=":没有从"const char *"到"std::string *"
的转换 谢谢C++代码:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
void moveTemperaturesToRight(double temperatures[], double windSpeed[], string windDirection[])
{
    for (int i = 3; i > 0; i--)
    {
        temperatures[i] = temperatures[i - 1];
        windSpeed[i] = windSpeed[i - 1];
        windDirection[i] = windDirection[i - 1];
    }
}
int main()
{
    string name;
    int choice;
    int numOfReadings = 0;
    double temperatures[4], windSpeeds[4];
    string windDirections[4];
    bool initialized = false;
    string str;
    //Have the user provide a name for the weather station upon entry.
    cout << "Enter the name of weather station: ";
    getline(cin, name);
    //Control loop to perform various actions.
    while (true)
    {
        cout << "1. Input a complete weather reading." << endl;
        cout << "2. Print the current weather." << endl;
        cout << "3. Print the weather history (from most recent to oldest)." << endl;
        cout << "4. Exit the program." << endl;
        cout << "Enter your choice: ";
        cin >> str;
        if (str.length() != 1 || str < "1" || str > "4")
        choice = 0;
        else
            choice = atoi(str.c_str());
        //Switch based on choice.
        switch (choice)
        {
        case 1: moveTemperaturesToRight(temperatures, windSpeeds, windDirections);
            do {
                cout << "Enter the temperature (a value >=0):";
                cin >> temperatures[0];
            } while (temperatures < 0);
        //get correct wind speed
        do
        {
            cout << "Enter the wind speed (a value >=0):";
            cin >> windSpeeds[0];
        } while (windSpeeds < 0);
        //get correct wind direction
        do
        {
            cout << "Enter the wind direction (North,South,East or West):";
            cin >> windDirections[0];
        } while (windDirections != "North" && windDirections != "South" && windDirections != "East" && windDirections != "West");
        initialized = true;
        if(initialized)
            numOfReadings++;
            if (numOfReadings > 4)
                numOfReadings = 4;
            break;
        case 3:   //Print the current weather, if valid weather is entered.
            for (int i = 0; i < numOfReadings; i++)
            {
                cout << "*****" << name << "*****" << endl;
                cout << "Temperature: " << temperatures[i] << endl;
                cout << "Wind speed: " << windSpeeds[i] << endl;
                cout << "Wind direction: " << windDirections[i] << endl << endl;
            }
            if (numOfReadings == 0)
                cout << "Please enter the details before asking to print." << endl;
            break;
        case 2:   if (numOfReadings == 0)
        {
            cout << "Please enter the details before asking to print." << endl;
            break;
        }
                  cout << "*****" << name << "*****" << endl;
                  cout << "Temperature: " << temperatures[0] << endl;
                  cout << "Wind speed: " << windSpeeds[0] << endl;
                  cout << "Wind direction: " << windDirections[0] << endl << endl;
                  break;
        case 4:   return 0;   //Stops execution.
        default: cout << "Invalid choice. Please follow the menu." << endl;
        }
    }
}

您需要将windDirections元素与文本进行比较。

你是说windDirections[0] != "North"&c.吗?

当前,您正在尝试比较 std::string 的数组,因此编译器会发出诊断程序。它尽最大努力将数组衰减到指向std::string的指针(因此存在特定错误),但随后放弃了。