C++代码不断带来错误。不知道我做错了什么?
所以我为我的班级准备了家庭作业,并且已经坐在这个代码上一整天了。我不知道我做错了什么。我写下了算法并再次检查了代码,但我没有发现任何错误。请帮忙!问题和我的代码都在代码里 代码如下所示C++代码不断带来错误。不知道我做错了什么?,c++,C++,所以我为我的班级准备了家庭作业,并且已经坐在这个代码上一整天了。我不知道我做错了什么。我写下了算法并再次检查了代码,但我没有发现任何错误。请帮忙!问题和我的代码都在代码里 代码如下所示 您需要删除我可以在您的代码中看到多个问题,让我逐步解决: int main() { string month1, month2, month3; double month1Inch, month2Inch, month3Inch; double averageInches; dou
您需要删除我可以在您的代码中看到多个问题,让我逐步解决:
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;
}
此外,您还应该习惯编译器输出,除了试图将字符串读入双变量外,其他所有输出都是语法错误,应该给出明确的错误消息
编辑:还注意到我们忽略了一个非常明显的问题。你需要包括。一些编译器通过头间接地包含了头的一部分,允许您定义变量,但它仍然找不到字符串的运算符>>。所以我找到了它。当程序启动时,我错误地使用了as source.cpp文件,然后用相同的代码创建了另一个文件。所以我才知道这是一个源程序对一个项目 我知道怎么了。代码中可能有一个bug。你必须告诉我们什么不起作用。我们该怎么办?您已经在这里转储了一堆代码,上面写着“它不工作”。你没有描述你想做什么或实际发生了什么。您没有列出任何错误消息,没有示例输入或输出,什么都没有。如果您查看注释,它会问我需要解决的问题。此外,错误代码太长,无法输入。我正在使用visualbasic 2013对此进行编程。它应该计算此后所有月份的平均英寸。我真的不知道我打错了什么!它快把我逼疯了!能够计算所有3英寸降雨的平均英寸数,一切看起来都很好,但我一直在得到构建错误。请发布您得到的具体错误。如果不解释问题所在,则很难帮助您找到解决方案。实际上,<>中的空白不应该是问题。也许我的IDE忽略了它?您使用的是什么环境?我只是快速地将代码粘贴到升华文本中,并在OS X下使用g++4.9进行编译。我使用的是Microsoft VisualStudio 2013I,我刚刚在visual studio 2013上对其进行了双重检查。空白在这个IDE上不是问题。好的,有可能,但是还有其他的错误需要注意:g++和clang都抱怨include'sHank you中的空白。我照你说的做了,但还是不跑。我将错误消息的第一行放在上面。@user2221218在末尾添加,您缺少include,这可能导致该问题。请用修复的pls再试一次。我这样做了,并不断得到生成错误:1>c:\users\prince\desktop\class work\tariqsalim第3章第4号。cpp37:error C2065:“totalMonths”:未声明的标识符1>c:\users\prince\desktop\class work\tariqsalim第3章第4号。cpp38:error C2065:“totalMonths”:未声明的标识符我完全更改了所有零件的totalMonths到totalInches的名称,并获取此错误消息:1>Tariqsalim第3章第4号。obj:error LNK2005:_main已在Source.objok中定义。由于您只获得一个源文件,因此现在获取链接错误非常奇怪。尝试删除所有已创建的.obj、.exe。。。编译和重新编译后得到的文件,可能是编译器弄错了什么。如果这对你没有帮助的话,你应该用你的新代码和所有的错误信息问一个新问题,因为我们离你遇到的第一个错误已经有一段距离了。
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)
cout << "Enter first month inches -" <<month1<<;
cout << "Enter first month inches -" <<month1;
#include < iostream >
#include <iostream>
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;
}