+ 2
What is wrong with this code?
I’m trying to make a code in cpp that uses “cin” To let me insert a random number and multuply it by two, what is wrong with my code here? #include <iostream> using namespace std; void timestwo(int x;) { return x*2;} int main() { return 0; cout >> cin >> x; } It keeps saying compliant error.
6 Respostas
+ 7
First of all, you cannot take input and output it to console in a single line its because C++ is Statically Typed Language. Either take input first or print first.
Secondly, you have defined 'void' as the return type of your function which means it returns nothing. But in this case, you are returning x*2 which is an integer. So you need to change return type to int of a function.
Thirdly, you haven't called your function in the code so the statements inside the function won't execute.
Lastly, I see a semi-colon in the parameter list of a function which I am not sure is a syntax error.
+ 5
cout is followed by << extraction operator and void function can't return any value.
+ 5
#include <iostream>
using namespace std;
int timestwo(int x) {
return x*2;
}
int main() {
int z;
cin >> z;
int res = timestwo(z);
cout <<res;
return 0;
}
+ 4
next ,
you haven't called the function, so even if you solve error it will output nothing.
+ 2
cout arrow direction is to left
cin arrow direction is to rigth
and return x*2 to is useless because if a method explicitly declared with void keywords its means this method can't recieve any value send by return type
use int if you want recieve an integer to return type.
+ 1
remove the semicolon in the function parameter, do int timestwo() instead of void, and do:
cin >> x;
cout << x;