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!
Related
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 3 years ago.
Improve this question
I am not sure why I am getting this error in my code. It seems that my variable num is not being passed to my function printIt.
#include <stdio.h>
void printIt(int *num);
int main(){
int num;
printf("What is your annual income? ");
scanf("%d", &num);
printIt(&num);
return 0;
}
void printIt(int *num){
if (num > 90000){
printf("Congratulations! You are doing great.");
return;
}else{
printf("You will make $90,000, if you keep going.");
return;
}
}
You've defined printIt as receiving a pointer to an int, so inside of it, num is the address of that int, and *num is the value of the int.
IMO, it would be better to change it to void printIt(int num); and when you call it, just pass num directly: printIt(num);. There's no real point in passing the address in this case.
In your function printIt num is a pointer to an integer and *num is the income you have been given from the user.
There are two ways of solving this either change the signature of printIt or change the code.
Alternative one
void printIt(int num){
if (num > 90000){
printf("Congratulations! You are doing great.");
return;
}else{
printf("You will make $90,000, if you keep going.");
return;
}
}
and then call it with printIt(num); instead.
Alternative two: change the code and keep the function call as it is
void printIt(int *num){
if (*num > 90000){
printf("Congratulations! You are doing great.");
return;
}else{
printf("You will make $90,000, if you keep going.");
return;
}
}
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.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
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.
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.
Improve this question
#include<cmath>
#include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm>
//There were problems in this code. the include wasnt showing up. So i just put them as comments.
using namespace std;
int main()
{
int n, a[n],count[100],temp;
cin>>n;
if(n<100||n>pow(10,6))
return 0;
for(int i=0;i<100;i++)
{
count[i]=0;
}
for(int j=0;j<n;j++)
{
cin>>a[j];
if(a[j]<0||a[j]>=100)
return 0;
}
for(int m=0;m<n;m++)
{
temp=a[m];
count[temp]++;
}
for(int s=0;s<100;s++)
{
cout<<count[s]<<" ";
}
return 0;
}
Whoops!
Looks like you're trying to create a variable sized array (which doesn't exist... kinda) with an undefined variable (which can be anything the system wants it to be).
Use pointers instead, and let the user fill the variable before creating the array:
int n;
std::cin >> n;
int* a = new int[n];
Or use the magic C++ vectors, since you're including them. They offer many more features than plain pointers like dynamic allocation and some newer methods of type-safety, along with multiple in-out methods like LIFO and FIFO:
int n;
std::cin >> n;
std::vector<int> a(n);
Your below statement is wrong:-
int n, a[n],count[100],temp;
cin>>n;
a fix size array declaration must need the size during compilation. In the above declaration n does not have a size during compilation. Hence compiler can not allocate memory from stack.
a
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 8 years ago.
Improve this question
I made a simple C++ program. I expect a floating point output of 5/9, but is is zero. Can someone comment, why output is unexpected i.e. zero?
#include<iostream>
using namespace std;
void fun(double *ptr);
int main (int argc, char* argv[])
{
double a;
fun(&a);
cout<<a<<endl; // why not floating point 5/9??
}
void fun (double *ptr)
{
*ptr=(5/9);
}
5/9 will result in 0 because it is int division.
You would need to do
void fun (double *ptr)
{
*ptr = (5.0 / 9.0);
}
You need to use the correct type literals:
5 // int
5.0 // double
5.0f // float
5u // unsigned int
5l // long
5ul // unsigned long
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 ;