C++代码不断带来错误。不知道我做错了什么?
C ++ code keeps bring errors. don't know what I did wrong?
所以我为我的课做了家庭作业,并且已经在这个代码上坐了一整天了。我不知道我做错了什么。我写下了算法,并再次检查了代码,但我没有发现任何问题。请帮忙!问题和我编码的内容都在代码中。
代码低于
/*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文件,然后用相同的代码创建了另一个文件。所以我发现这是一个源程序对一个项目。
相关文章:
- 努力将整数转换为链表。不知道我在这里做错了什么
- 我已经做了一个程序来使用队列链C++表显示学生姓名和身高,现在我想以我不知道如何显示的顺序显示它
- 在不使用内置库函数的情况下添加字符串,我做错了什么?
- 尝试通过多个函数移动数组,但遇到了我不知道如何修复的错误
- 为什么C++数组索引值是有符号的,而不是围绕size_t类型构建的(或者我错了)
- 由于某种原因,我的代码中出现了 [json.exception.type_error.302]。我知道错误意味着什么,但我不知道哪里有故障
- 我做错了什么?GetDiskFreeSpaceExA的功能根本不起作用
- 想知道我哪里错了
- 是否存在链接类问题?我不知道发生了什么事
- 错误 C2064 — 我不知道我做错了什么
- 查找除数数不知道出了什么问题
- 我想对数组进行排序,但不知道出了什么问题
- C++ LNK2019 不知道怎么了
- 尝试对结构向量进行排序时,我在算法文件中收到 20 个错误。我不知道怎么了?
- 递归Fibonacci.不知道发生了什么
- C++代码不断带来错误。不知道我做错了什么?
- 核心转储?我已经通过我的程序很多次不知道出了什么问题
- 学习C++,我不知道我在这里做错了什么
- 不知道出了什么问题 - 两次程序隔离错误
- 使用prim算法的最小生成树,不知道出了什么问题