C++代码不断带来错误。不知道我做错了什么?

C ++ code keeps bring errors. don't know what I did wrong?

本文关键字:不知道 错了 什么 错误 代码 C++      更新时间:2023-10-16

所以我为我的课做了家庭作业,并且已经在这个代码上坐了一整天了。我不知道我做错了什么。我写下了算法,并再次检查了代码,但我没有发现任何问题。请帮忙!问题和我编码的内容都在代码中。

代码低于

/*Write a program that calculates the average rainfall for three months. The program
should ask the user to enter the name of each month, such as June or July, and the
amount of rainfall (in inches) that fell each month. The program should display a mes-
sage similar to the following:
The average rainfall for June, July, and August is 6.72 inches.*/
/*
1. Declare variables for month 1, 2, and 3.
2. Declare variable for Total and Average Rainfall
3. Ask user to input name of months.
4. Then ask user to input inches of rain fall
5. Add all inches and then divide by number of inches asked. In this case, 3.
6. Display average inches of rain for all months to user.
*/
#include < iostream >
#include <iomanip>
using namespace std;
int main()
{
string month1, month2, month3;
double month1Inch, month2Inch, month3Inch;
double averageInches;
double totalMonths;
cout << "Enter first month's name -";
cin >> month1Inch;
cout << "Enter first month inches -" <<month1<<;
cin >> month1Inch;
cout << "Enter second month's name - ";
cin >> month2;
cout << "Enter second month's inches -" << month2<< ;
cin >> month2Inch;
cout << "Enter third month's name -";
cin >> month3;
cout << "Enter third month's inches-" << month3 << ;
cin >> month3Inch;
totalMonths = (month1Inch + month2Inch + month3Inch);
averageInches = (totalMonths) / 3;
cout << "The average rainfall for" << month1 << "," << month2 << "," << "and" << month3 << "is" << averageInches << endl;
system("pause");
return 0;

}

这是我收到的第一行错误信息。

1>c:usersprincedesktopclass worktariqsalim chapter 3 number 4 .cpp(22): error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

您需要从所有cout语句的末尾删除<<,如

cout << "Enter first month inches -" <<month1<<;

应该是

cout << "Enter first month inches -" <<month1;

此外,

#include < iostream >

应该是

#include <iostream>

还有其他一些错误,您应该尝试使用调试器来发现自己。请参阅@AliciaBytes的详细回复。

我可以在您的代码中看到多个问题,让我来了解一下:

int main()
{
    string month1, month2, month3;
    double month1Inch, month2Inch, month3Inch;
    double averageInches;
    double totalMonths;
    cout << "Enter first month's name -";
    cin >> month1Inch;  // you want to read the name into a double variable?
                        // you probably wanted month1 instead of month1inch here.
    cout << "Enter first month inches -" <<month1<<;    // month1 doesn't have a value yet, look above
            // also what is "<<;" supposed to do? you should probably get rid of the "<<" at the end.
    cin >> month1Inch;
    cout << "Enter second month's name - ";
    cin >> month2;
    cout << "Enter second month's inches -" << month2<< ;   // again get rid of the "<<" at the end.
    cin >> month2Inch;
    cout << "Enter third month's name -";
    cin >> month3;
    cout << "Enter third month's inches-" << month3 << ;    // see above...
    cin >> month3Inch;
    totalMonths = (month1Inch + month2Inch + month3Inch);
    averageInches = (totalMonths) / 3;
    cout << "The average rainfall for" << month1 << "," << month2 << "," << "and" << month3 << "is" << averageInches << endl;
    system("pause");
    return 0;
}

此外,您应该习惯编译器输出,除了试图将字符串读取到双变量之外,所有这些都是语法错误,应该会给出明确的错误消息。

编辑:还注意到我们忽略了一个非常明显的问题。你需要#include <string>。一些编译器通过<iostream>头间接地包含了<string>头的部分,这允许您定义变量,但它仍然找不到字符串的operator>>

所以我想明白了。当程序启动时,我错误地将其作为source.cpp文件,然后用相同的代码创建了另一个文件。所以我发现这是一个源程序对一个项目。