What is wrong with this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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.

10th Sep 2018, 5:24 PM
Vlogger Matt
Vlogger Matt - avatar
6 Answers
+ 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.
10th Sep 2018, 5:59 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 5
cout is followed by << extraction operator and void function can't return any value.
10th Sep 2018, 5:37 PM
#DARK_PROGRAMMER_✔
#DARK_PROGRAMMER_✔ - avatar
+ 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; }
10th Sep 2018, 5:47 PM
MDJ_
MDJ_ - avatar
+ 4
next , you haven't called the function, so even if you solve error it will output nothing.
10th Sep 2018, 5:37 PM
#DARK_PROGRAMMER_✔
#DARK_PROGRAMMER_✔ - avatar
+ 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.
11th Sep 2018, 3:56 AM
Real Gutch
Real Gutch - avatar
+ 1
remove the semicolon in the function parameter, do int timestwo() instead of void, and do: cin >> x; cout << x;
10th Sep 2018, 5:37 PM
hinanawi
hinanawi - avatar