Why won't my code compile? [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
This is a code that takes a series of 3 numbers in a number pattern and figures out the difference between them. everything seems to be right but my compiler keeps telling me I need an initializer before int i? sorry, I'm new to C++ so I'm sure my code is horrible.
using namespace std;
void add(int a, int b, int c)
int i;
for (a+i!=b;b+i!=c)
{i=0; i<100; i++;}
else {cout i;}
};
int main()
{
int x, y, z;
cin>>x;
cin>>y;
cin>>z;
add(x, y, z);
}

Many things, first you're missing a curly braze after your add function.
Also, you have one extra ; in your for declaration.
Also, after your function add there shouldn't be a ;

Related

Trying to create a cos(x) calculating function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I am coding a cos(x) function in C++ but the result I am getting is infinity, except that it should be -1/3.
Here is my code:
#include <iostream>
#include <math.h>
using namespace std;
The factorial function:
int factorial(unsigned int n)
{
unsigned long factorial = 1;
for(int o=1;o<=n;o++)
{
factorial *= o;
}
}
int main()
{
double x;
double answre;
double input;
cin>>input;
for(int i=0;i<2;i++)
{
double y=2*(i)+2;
I declared y here instead of implementing it's value directly, since I thought it is dividing by factorial instantaneously and that is the reason for all the parentheses as well.
x=((pow(input,2*(i)+2))/(factorial(y)))*(pow(-1,(i)+1));
x=+x;
}
answere=1+x;
cout<<answere<<endl;
return 0;
}
You are not returning anything in factorial
x=((pow(input,2*(i)+2))/(factorial(y)))*(pow(-1,(i)+1));
x=+x; // ???
If you want to add to x, just write x += .... What you did was assign one (!) summand to x and then set x to itself. You are never adding anything.

C++ error expected unqualified [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Okay so im trying to do some old quiz from my university and i have a question and this is the answer like this , when i try it on Dev-C++ its say wrong , but the teacher say its correct
#include <iostream>
using namespace std;
void add (int,int);
int substract(int,int);
int multiply(int,int);
void divide(int,int);
int main()
{
int a,b;
cout<<"Please Enter The Value of a: ";
cin>>a;
cout<<"Please Enter The Value of b: ";
cin>>b;
add(a,b);
cout<<"The substract a-b is: "<<substract(a,b)<<endl;
cout<<"The multiply a*b is : "<<multiply(a,b)<<endl;
divide(a,b);
return 0;
}
void add (int a,int b);
{
cout<<"There sum is: "<<a+b<<endl;
}
int substract(int a,int b);
{
return (a-b);
}
int multiply (int a,int b);
{
return (a*b);
}
void divide (int a,int b);
{
cout<<"There divide is: "<<a/b<<endl;
}
You are placing semi colons at the end of your functions params list For example:
int substract(int a,int b); ---> (Should not have a semi-colon here)
{
return (a-b);
}
Whenever C++ compiler throws an unexpected unqualified at you, it is usually in part because your semi-colons are incorrect. This is good to remember for debugging in the future!

What did I do wrong? C++ Newbie here [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I thought the output would be 70? (20+20+20+10=70) Why is it so large?
#include <iostream>
using namespace std;
int main()
{
int a,b,c=20;
int d=10;
int sum = a+b+c+d;
cout << sum;
return 0;
}
The issue is that you are not initializing the variables a and b. That means when you attempt to run your program, the computer is looking in memory for a value to use for each, and that number could be very big or very small. Try this:
#include <iostream>
using namespace std;
int main()
{
int a = 20,b = 20,c=20; //here, a and b are defined
int d=10;
int sum = a+b+c+d;
cout << sum;
return 0;
}
C is the only one variable that you initialize to 20, the other 2 variables
(a and b) are holding garbage..
so your math calculation is undefined behaviour.

program to view addresses of elements in an array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
help plz its showing invalid indirection
i used it to find the location or memory addresses of elements in the array b
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int *ptr;
int b[]={1,0,2,3,4,5,6,7,8,9};
ptr=b;
for(int i=0;i<10;i++)
cout<<ptr[i]<<" "<<*b[i];
}
In order to print the address of the ith element in an array b, use
std::cout << b + i;
This will work in all cases except when b is an array of char, in which case you need to cast to void*
std::cout << static_cast<const void*>(b + i);
in place of iostream.h it should be iostream.
void main() ; it should be int main().
cout<<ptr[i]<<" "<<(b+i)<<endl;
Formatting by using endl in above line of code will make the result clear.
Your function should return an integer value.
return 0;

Confusion with structure variable in C++ [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
Is it possible to assign value of one structure variable to another structure variable
Is this code correct -
#include<iostream.h>
struct s1
{
int a;
float b;
char c;
} st1,st2,st3;
int main()
{
struct s2{
int x;
float y;
char z;
} ss1,ss2,ss3;
// Read & Initialize structures
ss2=ss1;
:
ss3.z=st1.c;
:
}
void func1()
{
ss2.x=st1.a;
ss3.y=st2.b;
ss1.z=st3.c;
:
ss1=ss3;
}
Kindly clear my doubt whether the above code is OK or not
ss2=ss1; this will depend on the compiler some compiler will allow to copy the structure variable and some not.
ss2.x=st1.a; ss2 structure will be local to the main function and it is not available in funct1(), so it must be throwing the compilation error.