How to fix "too few arguments to function"? - c++

my program is given below
#include <iostream>
using namespace std;
int sum(int x,int y)
{
return x+y;
}
int sub(int x,int y)
{
return x-y;
}
int pro(int x,int y)
{
return x*y;
}
int quo(int x,int y)
{
return x/y;
}
int main()
{
int a,b;
char op;
cout<<"Enter two numbers:"<<endl;
cin>>a>>b;
cout<<"Enter a operator:"<<endl;
cin>>op;
switch(op)
{
case '+':
sum(a,b);
cout<<sum()<<endl;
break;
case '-':
sub(a,b);
cout<< sub()<<endl;
break;
case '*':
pro(a,b);
cout<< pro()<<endl;
break;
case '/':
quo(a,b);
cout<< quo() <<endl;
break;
default:
cout<<"Invalid Operator"<<endl;
}
return 0;
}
here is the error i receive
||=== Build file: "no target" in "no project" (compiler: unknown) ===|
C:\Users\amohe\Desktop\cal.cc||In function 'int main()':|
C:\Users\amohe\Desktop\cal.cc|33|error: too few arguments to function 'int sum(int, int)'|
C:\Users\amohe\Desktop\cal.cc|4|note: declared here|
C:\Users\amohe\Desktop\cal.cc|37|error: too few arguments to function 'int sub(int, int)'|
C:\Users\amohe\Desktop\cal.cc|8|note: declared here|
C:\Users\amohe\Desktop\cal.cc|41|error: too few arguments to function 'int pro(int, int)'|
C:\Users\amohe\Desktop\cal.cc|12|note: declared here|
C:\Users\amohe\Desktop\cal.cc|45|error: too few arguments to function 'int quo(int, int)'|
C:\Users\amohe\Desktop\cal.cc|16|note: declared here|
||=== Build failed: 4 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
need solution.

If you read the quite clear error message, including the line number, then you will notice that it does NOT complain on
sum(a,b);
But it DOES complain on
cout<<sum()<<endl;
The difference is rather striking.
You need to give enough parameters to the second one.
Also the first one is useless, compilers tend to tell you that if you increase the warning level.
A solution is to not only calculate and ignore it, instead output it directly and corrctly by using a gentle mixture of your two code lines.
cout<<sum(a,b)<<endl;

Related

error: redefinition of 'int main()' even if "int main()" apperas only once

I was working on a problem on Pbinfo: https://www.pbinfo.ro/probleme/898/sumfactcif but each time I tried to run my code it said:
sumfactcif.cpp: In function 'int main()':
sumfactcif.cpp:35:5: error: redefinition of 'int main()'
int main(){
^
sumfactcif.cpp:25:5: error: 'int main()' previously defined here
int main()
^
I don't know what to do because in my IDE(Codebloks) the code has no errors.
Here's the code if you can help me:
#include <iostream>
using namespace std;
int sumfactcif(int x)
{
int p,p1=0;
while(x>0)
{
int u=x%10;
p=1;
for(int i=1;i<=u;i++)
{
p=p*i;
}
p1=p1+p;
x=x/10;
}
return p1;
}
int main()
{
int x,fct;
cin>>x;
fct=sumfactcif(x);
cout<<fct;
}
Thanks!
Answer: Looks like the site already added an "int main" to my code, so that the result had two "int mains". Thanks to #churill for pointing that out

C++ Class Header and Implementation Error

I just recently started messing around with separate class files in c++ and this was my first attempt:
First I made a class header called "ThisClass.h":
//ThisClass.h
#ifndef THISCLASS_H
#define THISCLASS_H
class ThisClass
{
private:
int x;
float y;
public:
ThisClass(int x, float y);
void setValues(int x, float y);
int printX();
float printY();
};
#endif // THISCLASS_H
Then, I implemented my class in a file called "ThisClass.cpp":
//ThisClass.cpp
#include "ThisClass.h"
ThisClass::ThisClass(int x, float y)
{
this->x = x;
this->y = y;
}
void ThisClass::setValues(int x, float y)
{
this->x = x;
this->y = y;
}
int ThisClass::printX()
{
return this->x;
}
float ThisClass::printY()
{
return this->y;
}
Finally, I made a file called "main.cpp" where I used the class:
//main.cpp
#include <iostream>
using namespace std;
int main()
{
ThisClass thing(3, 5.5);
cout << thing.printX() << " " << thing.printY()<< endl;
thing.setValues(5,3.3);
cout << thing.printX() << " " << thing.printY()<< endl;
return 0;
}
I then compiled and ran this program through Code Blocks which uses the MinGW compiler and received the following errors:
In function 'int main()':|
main.cpp|7|error: 'ThisClass' was not declared in this scope|
main.cpp|7|error: expected ';' before 'thing'|
main.cpp|8|error: 'thing' was not declared in this scope|
||=== Build failed: 3 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Am I somehow doing this wrong? Any help would be appreciated.
You forgot to #include "ThisClass.h" in main.cpp.
As already answered that you forgot to put #include "ThisClass.h"in main.cpp.
Just do that and your code will be compiled.
I just want to answer your question -
However, now my console isn't outputting anything even though I have 2 cout calls
Please put a getchar() before return in main function, it will allow you to see your output.

Write a subprogram that calculates n!. While using this subprogram make a program that calculates (a+b)!

My task is
Write a subprogram that calculates n!. While using this subprogram make a program that calculates (a+b)!.
I wrote the following code:
using namespace std;
int f(int n) {
for(int i=1;i<=n;i++){
f=f*i;
}
return f;
}
int main() {
int a,b;
cout<<"a=";
cin>>a;
cout<<"b=";
cin>>b;
cout<<"factorial of a+b="<<(a+b)*f;
return 0;
}
And I get this error when I compile:
In function 'int f(int)':
7:27: error: invalid operands of types 'int(int)' and 'int' to binary 'operator*'
8:8: error: invalid conversion from 'int (*)(int)' to 'int' [-fpermissive]
In function 'int main()':
16:34: error: invalid operands of types 'int' and 'int(int)' to binary 'operator*'
17:9: error: expected '}' at end of input
Here's your error
return f;
returns a function pointer of your function f(). Also you're using the function pointer for your calculations.
In C++ you need to declare a variable that can be used to calculate and return:
int f(int n) {
int result = 1;
for(int i=1;i<=n;i++) {
result=result*i;
}
return result;
}

Error: cannot convert parameters from 'int [10]' to 'int' [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
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.
Closed 8 years ago.
Improve this question
This is a fairly simple program for the implementation of Stack. But there is a problem with the declaration of the push(), pop() and display() functions. The error statements are as follows. Please help me figure out the problem.
Error:
error C2664: 'push' : cannot convert parameter 1 from 'int [10]' to 'int'
error C2664: 'pop' : cannot convert parameter 1 from 'int [10]' to 'int'
error C2664: 'display' : cannot convert parameter 1 from 'int [10]' to 'int'
Program:
#include<iostream>
#define STACKSIZE 10
using namespace std;
void push(int,int,int);
void pop(int,int);
void display(int,int);
int main()
{
int stack[STACKSIZE],n,data,stackptr=-1;
while(1)
{
cout<<"1. Push\n2. Pop\n3. Display\n4. Exit\n";
cin>>n;
switch(n)
{
case 1:
cout<<"\nEnter a data to push: ";
cin>>data;
push(stack,stackptr,data);
break;
case 2:
pop(stack, stackptr);
break;
case 3:
display(stack, stackptr);
break;
case 4:
exit(1);
break;
default:
cout<<"\nEnter correct choice...\n\n";
}
}
system("pause");
return 0;
}
void display(int stack[STACKSIZE], int &stackptr)
{
cout<<"\n\n";
for(int i=0;i<stackptr;i++)
cout<<stack[i]<<"\n";
cout<<"\n\n";
}
void push(int stack[STACKSIZE],int &stackptr, int data)
{
if(stackptr == STACKSIZE - 1)
{
cout<<"\n\nStack full\n\n";
return ;
}
stackptr++;
stack[stackptr] = data;
}
void pop(int stack[STACKSIZE], int &stackptr)
{
if(stackptr == 0)
{
cout<<"\n\nStack Empty\n\n";
return ;
}
stackptr = stackptr-1;
}
The answer given by Gangadhar and Jansel is incomplete. You are passing values by reference so this is not enough:
void push(int stack[size],int,int);
This is what you need:
void push(int[],int&,int);
void pop(int[],int&);
void display(int[],int&);
Your function's declarations are wrong. for example:
void push(int, int, int);
// ^^^ int
Just correct it
void push(int stack[], int&, int);
// ^^^^^^^^^^^ should be int[]
You were making mistake in function declaration. This is the correct way.
void push(int[],int&,int);
void pop(int[],int&);
void display(int[],int&);
These error are self explanatory push,pop and display functions prototypes receives 3 int parameters, and you are trying to pass int[] by first first parameter, so, change your functions prototype to receive int[] of it first parameter.
You have a function prototype for push/pop at the start that declares an int not an array.
void push(int, int, int);
This is the version the compiler uses to check your call against. Rewrite these prototypes to have the function signature as the functions themselves.

C++ member function not declared error, when it appears to be

I receive the errors:
cs163hw1.cpp:41:24: error: no ‘int menutype::run_prog()’ member function declared in class ‘menutype’
and
main.cpp:18:7: error: ‘struct menutype’ has no member named ‘run_prog’
When attempting to compile my program with the associated code (spanning the appriprait .cpp and .h files):
int main(int argc, char ** argv){
...
menu.run_prog();
...
class menutype{
public:
menutype(int);
int display();
int run_prog();
private:
extras list;
person menup;
};
int menutype::run_prog(){
bool exit = false;
int input;
while(!exit){
input = 0;
while(input < 1 || input > 4)
input = display();
switch(input){
case 1 :
break;
case 2 :
break;
case 3 :
break;
case 4 : exit = true;
break;
default :
break;
}
}
}
I have no idea why this is happening, any guesses?
You need to have the class menutype declared above main(). Better still is to move the class to its own dedicated file called menutype.cpp and include the header in main's source file. As you have described it, the complier has no knowledge of menutype yet as its parsing the source file from the top of the file.