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

时间:2014-09-17 01:17:17

标签: c++

所以我为我的班级做了功课,现在一整天都坐在这个代码上。我不知道自己做错了什么。我写下了算法并再次检查了代码,但我没有看到它有什么问题。请帮忙!我编码的问题和代码在代码中。

代码低于

/*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:\users\prince\desktop\class work\tariqsalim 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)

3 个答案:

答案 0 :(得分:2)

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

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

应该是

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

此外,

#include < iostream >

应该是

#include <iostream>

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

答案 1 :(得分:1)

我可以在代码中看到多个问题,让我一步一步:

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>。有些编译器通过<string>标题间接地包含部分<iostream>标题,这允许您定义变量,但它仍然无法找到字符串的operator>>

答案 2 :(得分:0)

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