-1

我刚开始使用 c++,只是在玩一些前几天学到的东西,而我编写的这个程序除了试图弄清楚如何在用户输入中使用可返回参数之外没有真正的目的。当我尝试编译这个时,整个事情就变得疯狂了。它出什么问题了?

这是我写的代码:

    #include <iostream>


// defining the Parameters of Add and Multply

 int add(int a, int b)
 {
 return a + b;
 }
 int multiply(int c, int d)
 {
 return c * d;
 }

 int main ()
{
using namespace std;

// user defines vairables

int x;
int y;
int w;
int z;

cout << "enter x value now:" << endl;
cin >> x >> endl;
cout << "enter y value now" << endl;
cin >> y >> endl;
cout << "enter w value now" << endl;
cin >> w >> endl;
cout << "enter z value now" << endl;`
cin >> z >> endl;

//computation of variables

cout << "computing x and y factors" << add(x, y) << endl;
cout << "computing w and z cordinates" << multiply(w, z) << endl;
cout << " " << endl;
cout << "final answer is:  " << add(x, y) + multiply(w, z) << endl;

cin.clear();
cin.ignore(255, '\n');
cin.get();


return 0;
}
4

2 回答 2

1

首先:由于您要询问编译错误,因此您应该发布您遇到的确切编译错误。

你的问题在于这样的陈述:

cin >> z >> endl;

应该:

cin >> z;

您只需将输入的输入变量读入一个变量,使用cin您没有向流中插入任何内容。endl在流中插入换行符。

除了第 32 行有一个杂散的`,您需要将其删除。

于 2012-08-19T05:29:39.237 回答
0

是的,显然该程序很好......正如Als提到的那样,只是一个愚蠢的小错误。让我再解释一下。 endl是用于在屏幕上输出新行的字符。当您通过cin进行输入时,您不能同时打印某些内容。cin 是标准输入流,而不是输出流。如果你想打印endl,你可以这样做。

cout << x << endl ;

于 2012-08-19T07:10:50.663 回答