我不知道在这个c ++中该怎么做

时间:2015-09-01 01:37:44

标签: c++

这里是代码和编译错误

#include <iostream>
using namespace std;

int main()
{
double RTinSeries, RTinParallel, R1, R2, R3, R4, R5, R6, Option, Opt1, Opt2;
cout<<"Enter your option:(1 for parallel and 2 for series)";
cin>>Option;
if (Option =< 1 && Option >=3)
{
  cout<<"Error!";
  cout<<"You've enter a wrong Option";
}
{
  else if (Option = 1);
  for (Opt1=RTinParallel; RTinParallel = (1/((1/R1)+(1/R2)+(1/R3)+(1/R4)+                (1/R5)+(1/R6))););
  cout<<"Your Choice is Parallel";
  cout<<"Enter Resistance Value for R1:";
  cin>>R1;
  cout<<"Enter Resistance Value for R2:";
  cin>>R2;
  cout<<"Enter Resistance Value for R3:";
  cin>>R3;
  cout<<"Enter Resistance Value for R4:";
  cin>>R4;
  cout<<"Enter Resistance Value for R5:";
  cin>>R5;
  cout<<"Enter Resistance Value for R6:";
  cin>>R6;
  cout<<"Total Resistance in Parallel is:"<< Opt1<<"ohms"<<endl;
}
{
else (Option = 2);
for (Opt2=RTinSeries; RTinSeries = R1 + R2 + R3 + R4 + R5 + R6;)
  cout<<"Your Choice is Series";
  cout<<"Enter Resistance Value for R1:";
  cin>>R1;
  cout<<"Enter Resistance Value for R2:";
  cin>>R2;
  cout<<"Enter Resistance Value for R3:";
  cin>>R3;
  cout<<"Enter Resistance Value for R4:";
  cin>>R4;
  cout<<"Enter Resistance Value for R5:";
  cin>>R5;
  cout<<"Enter Resistance Value for R6:";
  cin>>R6;
  cout<<"Total Resistance in Series is:"<<Opt2<<"ohms"<<endl;
}

  system("pause");
  return 0;


}

编译结果

9:15: error: expected primary-expression before '<' token
15:7: error: 'else' without a previous 'if'
15:26: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
16:92: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
33:3: error: 'else' without a previous 'if'
34:63: warning: suggest parentheses around assignment used as truth value [-Wparentheses]

1 个答案:

答案 0 :(得分:4)

您有许多基本的语法错误。我将列出其中的一些,但实际上,您的任务是: 1)阅读第一条错误消息...... 2)查找关于你在该行使用的关键字和表达式的C ++书籍(或谷歌)... 3)仔细观察,看看你如何做到这一点有什么不同 4)用它来解决代码中的问题

例如: (Option =< 1 && Option >=3)

如果你查看&#34;小于或等于&#34; &#34;大于或等于&#34;操作,您会发现它们总是<=>=而不是=< 所以,你的表达应该是:

(Option <= 1 && Option >=3)

你看到那里的一个字符差异了吗?

对于这个:

if (Option =< 1 && Option >=3)
{
  cout<<"Error!";
  cout<<"You've enter a wrong Option";
}
{
  else if (Option = 1);

如果您阅读if / else,您将看到结构总是:

if (condition) 
{
   // stuff here
}
else if (condition2) 
{
   // more stuff here
}

看看你的花括号在代码中的样子。 他们看起来非常不同。 大括号应该始终包含在代码块中,并且应该以{{1​​}}开头,以{结尾 - 你的不是。你的}后面跟着}然后然后有了else部分......这就完全没了。

尝试制作代码的副本...并删除所有执行内容的语句... 只需保留if / elses和花括号,并查看它们的排列位置..排除花括号应该很容易做到一旦额外的代码不存在...然后你可以添加回代码语句