error : too few arguments to function? [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 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
I did the exact same thing in another part of code and got no error or any problem with it's way of working but in this part and so it gives me an error.
So the function is;
void triangle(int height,int base)
{
printf("Enter the base of the triangle:");
scanf("%d",&base);
printf("\nEnter the height of the triangle:");
scanf("%d",&height);
int area;
area = (height*base)/2;
printf("\nTriangles area is %d.",area);
}
I'm using a switch-case function for my code and this is how I call it in main function;
case 3:
{
triangle(area);
}
Feels weird but this works perfectly;
void square(int length)
{
printf("Enter the length of square:");
scanf("%d",&length);
int area;
area = length*length;
printf("\nRectangles area is %d.",area);
}
Like this;
case 1:
{
square(area);
}

I did the exact same thing in another part of code and got no error or
any problem with it's way of working but in this part and so it gives
me an error.
C does not guarantee to diagnose all incorrect code, neither at compile time nor at run time. Nevertheless, you should turn up your compiler's warning level. If afterward it anywhere accepts a call to your triangle() function with other than exactly two arguments and without emitting at least a warning of some kind, then throw it out and choose a better one.
So the function is;
void triangle(int height,int base)
{
printf("Enter the base of the triangle:");
scanf("%d",&base);
printf("\nEnter the height of the triangle:");
scanf("%d",&height);
int area;
area = (height*base)/2;
printf("\nTriangles area is %d.",area);
}
Note that your function does not actually use the value provided by the caller for either argument, nor does it in any way return any information to its caller, even if you call it with the correct number of arguments. If that's what you want then it would be better to declare it to accept no arguments, and to call it that way:
void triangle(void) {
int length;
int height;
// ...
Note that length and height are now strictly local variables. You call that variation like so:
triangle();

Related

Switch with goto statement in function keeps running ad infinitum? [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 1 year ago.
Improve this question
This is the relevant code, I'm trying to write a function to pick type of account and when I run this, it keeps running in a weird loop. Any solutions?
void AccType(){
wrongInput:;
int typCho;
printf("What type of account do you want to open?\nPress 1 for Current\n2 for savings\n3 for retirement:\n");
scanf("%d",typCho);
switch (typCho) {
case 1:
strcpy(PracRec.AccTyp,"Current");
break;
case 2:
strcpy(PracRec.AccTyp,"Savings");
break;
case 3:
strcpy(PracRec.AccTyp,"Retirement");
break;
default:
printf("Please enter a valid choice!!\n");
goto wrongInput;
}
}
The second argument of scanf needs to be a pointer (memory adress) to an integer...
So, replace the line:
scanf("%d",typCho);
with:
scanf("%d", &typCho);
& is an operator that returns the memory address of the operand at right.
This is needed because scanf is going to change the value inside the memory address which relates to the variable.
When you put only typCho as the argument, it probably made the program to write the input at a random memory location, as the variable was also uninitialized.
Also, consider avoiding the use of goto, as pointed in the previous comments.
You could easily do the same thing with a construct like:
do { ...code... } while (condition);

Trying to understand the error in my code [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 2 years ago.
Improve this question
I'm creating a code where someone enters the amount of people eating a cut from and from there I can figure out how many pieces I can have in one pizza.I'm having trouble and don't know how to fix my error.
#include <stdio.h>
int Cuts(int n)
{
int max = n*2;
return m;
}
int main()
{
int m;
m = Cuts();
printf("P1:%d\n" , m);
}
Your CutYourPizza function is written to require one integer argument (called n), but when you invoked that function on the line max = CutYourPizza(); you did not supply any argument.
For example, if you wanted to supply the number 10 as an argument, then you could have written max = CutYourPizza(10); with the argument 10 inside the parentheses.

why i cannot compile simple project by dev c [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 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
I don't know why cannot compile simple project below by dev.
error: Project1.exe has stopped working.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int n,d;
while(1){
printf("enter number");
scanf("%d",n);
d=n%10;
while(d!=0){
n=n/10;
printf("%d",d);
d=n%10;
}
}
return 0;
}
For one, your project does compile, since you get a run time error.
The run time error occurs because you are not using scanf correctly. The arguments to scanf after the format string should be pointers to the variables.
I don't know which compiler you are using, but any fairly modern compiler would have given you compiler warnings about this, e.g. here's Clang's output:
apa.c:9:20: warning: format specifies type 'int *' but the argument has type 'int' [-Wformat]
scanf("%d",n);
~~ ^
Changing this into scanf("%d",&n); makes your program work.
https://linux.die.net/man/3/scanf
First, the error that you got is not a compile, it's a run time error.
Second, it's happen when the run arrive to the line of scanf. This function required the variavbles address to put the value in.
Replace this:
scanf("%d", n);
With this:
scanf("%d", &n);

How to dynamically allocate memory in 2D array and store values? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 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
I am trying to allocate memory in a 2D array dynamically, but I don't know what is wrong.
Error
let input is
2 2
1 2 3 4 5 6 7 8 9 ........
then program crashes
#include<stdio.h>
#include<stdlib.h>
int main()
{
int N,M;
int i,j;
scanf("%d %d",&N,&N);
int **A = (int **)malloc(N*sizeof(int *));
for(i=0;i<N;i++)
{
A[i] = (int *)malloc(M*sizeof(int));
}
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
{
scanf("%d",&A[i][j]);
}
}
for(i=0;i<N;i++)
{
for(j=0;j<M;j++)
{
printf("%d",A[i][j]);
}
}
return 0;
}
Here's your problem:
scanf("%d %d",&N,&N);
When you read in the array dimensions, you read into N twice and never read into M. As a result, the contents of M are indeterminate, and attempting to read that value invokes undefined behavior.
Fix this to input a value for M:
scanf("%d %d",&N,&M);
As i said problem is in scanning N and M variables.
Change
scanf("%d %d",&N,&N);
to
scanf("%d",&N);
scanf("%d",&M);
and you are fine.
Problem was you were reading N twice while didnt read an M which was used uninitialized.
Non-static variables (local variables) are indeterminate. Reading them prior to assigning a value results in undefined behavior.
You should free all your allocated memory
for(i=0;i<N;i++)
{
free(A[i]);
}
free(A);
and dont cast malloc()'s return value becouse
Its reduntand
Adding the cast may mask failure to include the header stdlib.h, in which the prototype for malloc is found
If the type of the pointer is changed, one must fix all code lines where malloc() was called and cast
Next time if you cannot find a bug, try to use debugger and look what exactly is happening, you can see variable values, where would you clearly see that M is uninitialized and didnt change after scanf().

Where did i make a mistake in my program i think its a logical error but i cant quite find it [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 6 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
I have this program that I wrote, it complies but I don't think it outputs correctly. Did I make a mistake here?
Here's my program:
#include <stdio.h>
void main(void)
{
int loop_counter = -8;
int user_input = 9;
char c1 = '9';
char c2 = 43;
while(loop_counter != 21);
{
printf("%d\n", loop_counter);
printf("%d\n", loop_counter+1);
printf("%d\n", loop_counter+2);
loop_counter = loop_counter + 1;
loop_counter++;
printf("%d\n", loop_counter);
printf("%d\n", loop_counter+1);
printf("%d\n", loop_counter+2);
getchar();
}
printf("loop exit\n\n");
getchar();
}
The biggest problem is probably that you have not articulated what you are hoping the code will do or what you intended the code to do or what you think it does. I am assuming you just need some help so my attempt to do so is below.
Running this through the compiler and interpreting the error messages helps a little.
Right off the bat, the compiler doesn't like your call to main. The compiler offers a suggestion that will successfully fix this error, so just follow the advice (and don't forget to add a
return 0;
statement before you exit main.
Second warning the compiler generates is that you have a semicolon at the end of your while statement. It also tells you how to fix it. Follow the instructions and you should be able to generate an executable.
You will still, however, have problems at runtime. This gets back to what is your intent.
With errors above corrected, your loop_counter variable enters the while loop initialized to -8, increases by 1 on line 19 and again increases by 1 on line 20. A call is made to getchar() but no input is given. Also lines 7, 8, and 9 are not used by your program.
Hope this gives you some direction. :)
The biggest error is
while(loop_counter != 21);
where the trailing ; will make the loop infinite.

Resources