Unexpected end-of-file found error in C programming [closed] - c

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 5 years ago.
Improve this question
Hi guys i have been check for my code many many times but i still confuse where is this error come from....
This error appear while build solution...
#include<stdio.h>
#include<conio.h>
int add(int a, int b);
int main()
{
int num1, num2, ans;
printf("Please Enter the two numbers :%d %d", num1, num2);
scanf_s("%d %d", &num1, &num2);
ans = add(num1, num2);
return 0;
}
int add(int a, int b)
{
int sum = a + b;
printf("\nSummition is = %d", sum);
}
Unexpected end-of-file found error

int main() {
int num1, num2, ans;
printf("Enter the two numbers : ");
scanf("%d %d", &num1, &num2);
ans= sum(num1, num2);
printf("\nAddition of two number is : ");
return (0);
}
int sum(int num1, int num2) {
int num3;
num3 = num1 + num2;
return (num3);
}

Related

Error C2371 redefinition; different basic types [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 3 years ago.
Improve this question
I am new to programing and I am trying to learn about functions, so I tried some questions with it to get a better understanding of it.
I am still unsure so right now I got stuck and i don't know how to continue.
I tried to make a first function in the main that has only the answers and that function calls a second function and the second one is calling to a third function
but I dont know how to fix this error i tried changing names but I am not sure what to do and even if i am doing is right
the error I get is related to "void Ex1()" so what can I change?
int main()
{
Ex1();
}
void Ex1()
{
int num1, num2, num3,min;
printf("enter three diff numbers \n");
scanf("%d %d %d", &num1, &num2, &num3);
printf("%d", function1(min));
}
int function1(int sum)
{
int num1, num2, num3, min, max;
if (num1 > num2)
{
max = num1;
min = num2;
}
else
{
max = num2; min = num1;
}
if (num3 > max)
{
min = max;
max = num3;
}
if (num3 > min&& num3 < max)
{
min = num3;
}
return min;
}
The problem you have is that you are trying to call your functions before you have defined (or at least, declared) them. In the C language, when the compiler encounters a call to such an "undeclared" function, it assumes that it is of a type that returns int (it can normally work out what any argument types will be from the actual values given).
So when, in your main function, the compiler comes across Ex1(), it assumes that the function returns an int (even though this isn't actually used). Then, later, when you have your definition for void Ex1(), the compiler has encountered a conflicting (re-)definition - with a different return type.
To fix this, the simplest option is to put a "forward declaration" of any functions used (both Ex1 and function1) before any code that calls them. In your case, this would be:
#include <stdio.h> // This "system header" provides the declarations for "printf" and "scanf"
void Ex1(); // No definition here - just telling the compiler what form it takes.
int function1(int sum); // And the same here
int main()
{
Ex1();
}
// .. . the rest of your code follows
Feel free to ask for further clarification and/or explanation.
You have problem with passing arguments between two functions EX1 and function 1. function1 should have num1, num2 and num3 as arguments.
int main()
{
Ex1();
}
void Ex1()
{
int num1, num2, num3,min;
printf("enter three diff numbers \n");
scanf("%d %d %d", &num1, &num2, &num3);
printf("%d", function1(num1, num2, num3));
}
int function1(int num1, int num2, int num3)
{
int min, max;
if (num1 > num2)
{
max = num1;
min = num2;
}
else
{
max = num2; min = num1;
}
if (num3 > max)
{
min = max;
max = num3;
}
if (num3 > min&& num3 < max)
{
min = num3;
}
return min;
}
This is just the order of definition, try to do it like this:
int num1, num2, num3, min, max;
int function1(int sum)
{
if (num1 > num2)
{
max = num1;
min = num2;
}
else
{
max = num2; min = num1;
}
if (num3 > max)
{
min = max;
max = num3;
}
if (num3 > min&& num3 < max)
{
min = num3;
}
return min;
}
void Ex1()
{
int num1, num2, num3;
printf("enter three diff numbers \n");
scanf("%d %d %d", &num1, &num2, &num3);
printf("%d", function1(min));
}
int main()
{
Ex1();
}

How to stop the int main(void) printf statements when there is an else statement in another function that gives another result? [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
So everything works in the program so far how the teacher wants it too. However, when I enter 0 for num2 it executes the printf like I want it too, but it also executes the printf at the end of the main. Is there anyway to stop it from also executing the one at end int main when I enter 0? I apologize if my code sucks. This is my first year(semester actually) coding and the teacher doesn't really teach us and I've been learning from the book
#include <stdio.h>
//Program with 3 functions 1) Divides 2) Multiplies 3) Calls the other functions
float multiply(float num1, float num2);
float divide(float num1, float num2);
void math_call(float num1,float num2);
int main(void){
float num1;
float num2;
//printf/scanf allows you to choose values for the functions
printf("Please enter 2 numbers:\n");
scanf("%f", &num1);
scanf("%f", &num2);
//printf below calls functions so it can print out your results
printf("Division = %f Multiplication = %f", divide(num1, num2), multiply(num1, num2));
return(0);
}
//Calls function 1 and 2
void math_call(float num1,float num2){
divide(num1,num2);
multiply(num1, num2);
}
//Divides the arguments and doesn't allow to divide by zero
float divide(float num1, float num2){
if(num2 != 0){
float divide;
divide = num1/num2;
return divide;
}
else{
printf("You can't divide by zero. So your answer for division is undefined.\n");
return(0);
}
}
//multiplies the arguments
float multiply(float num1, float num2){
if(num2 != 0){
float multiply;
multiply = num1*num2;
return multiply;
}
else{
printf("If you multiply by zero you will always get zero.\n");
return(0);}
}
This is the result
Please enter 2 numbers:
4
0
If you multiply by zero you will always get zero.
You can't divide by zero. So your answer for division is undefined.
Division = 0.000000 Multiplication = 0.000000
RUN SUCCESSFUL (total time: 1s)
Instead of this in your code
//printf below calls functions so it can print out your results
printf("Division = %f Multiplication = %f", divide(num1, num2), multiply(num1, num2));
Rewrite it as below
// call your functions here and fill variables with results
float divResult = divide(num1, num2);
float mulResult = multiply(num1, num2);
// check if num2 is not zero
if(num2 != 0)
{
// if it isn't it will print your message
printf("Division = %f Multiplication = %f", divResult, mulResult);
}

error printing value to screen. Max Min values [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 7 years ago.
Improve this question
I am writing an easy code that takes two numbers I enter and tells me which is the Max value and which is the Min value using 2 separate files for functions. here is my code so far. It scans the number and has the correct Max Min but when I go to print the values a crazy number prints.
#include<stdio.h>
int main(void)
{
double num2, max, min, num1;
printf("enter any space-separated pair of decimal numeric values on the same line: ");
scanf("%le%le", &num1, &num2);
max = ComputeMaximum(num1, num2);
min = ComputeMinimum(num1, num2);
printf("%le", max);
printf("%le", min);
return 0;
}
this is my main program, on seperate files in the same project I have:
double ComputeMinimum(double num1, double num2) {
double result;
result = (num1 < num2) ? num1 : num2;
return result;
}
And
double ComputeMaximum(double num1, double num2) {
double result;
result = (num1 > num2) ? num1 : num2;
return result;
}
I would like to have it print to screen:
ComputeMinimum(?, ?) returned ?
ComputeMaximum(?, ?) returned ? (question marks are the values.)
Code is missing prototypes.
That is all, format is OK, functions are OK.
#include<stdio.h>
// Add these to the same file as main()
// Or better yet, add to another file Compute.h and #include "Compute.h"
// here and in the the separate C file
double ComputeMinimum(double num1, double num2);
double ComputeMaximum(double num1, double num2);
int main(void)
...
// suggest adding check
if (2 != scanf("%le%le", &num1, &num2)) {
puts("Input error");
return -1;
}

uVa100- '3n+1' ~ time limit exceeded [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 7 years ago.
Improve this question
I have finished learning the basic C language recently. I started solving uVa online judge problems. I solved the 3n+1 problem, ran it in my compiler. It was doing perfectly fine. But when I submitted online it said "Time Limit Exceeded." I can't seem to place the solution.
Here's the code:
#include<stdio.h>
int execute(int x, int y)
{
int i, n, k, maxCycle=0;
for(i=x; i<=y; ++i)
{
n=1;
k=i;
while(k!=1)
{
if(k%2==1)
k=3*k+1;
else
k=k/2;
n++;
}
if(n>=maxCycle)
maxCycle=n;
}
return maxCycle;
}
int main()
{
while(1)
{
int a, b, max;
scanf("%d %d", &a, &b);
max=execute(a, b);
printf("%d %d %d\n", a, b, max);
}
return 0;
}
You are not breaking the input taking loop.
Use while ( scanf ("%d %d", &a, &b) != EOF ) when taking a,bin main().
You should break the input taking loop otherwise it waits for more input.
int main()
{
int a,b;
while(scanf ("%d %d", &a, &b) != EOF )
{
int max;
max=execute(a, b);
printf("%d %d %d\n", a, b, max);
}
return 0;
}

Why is scanf() taking wrong input? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am completely new to C, and was writing a small very simple program. The problem is that the scanf() is taking wrong input:-
#include <stdio.h>
int main(int args, char*argv[])
{
int num1 = scanf("%d",&num1) ;
int num2 =scanf("%d", &num2) ;
printf("Num1 = %d\n", num1) ;
printf("Num2 = %d\n", num2) ;
return 0 ;
}
When I give 34 and 23 as input it is outputting:-
Num1 = 1
Num2 = 1
Why is it so?
Don't assign the return value of scanf to num1 and num2. The return value of scanf indicates if the scan is successful, not what you thought it was.
int num1, num2;
scanf("%d", &num1);
scanf("%d", &num2);
This is because scanf returns the number of characters it matches. scanf("%d",&num1) and scanf("%d",&num2) will return 1. You are assigning that number, i.e, 1 to num1 and num2.
Now do it as follows:
#include <stdio.h>
int main(int args, char*argv[])
{
int num1;
int num2;
scanf("%d",&num1) ;
scanf("%d", &num2) ;
printf("Num1 = %d\n", num1) ;
printf("Num2 = %d\n", num2) ;
return 0 ;
}
Suggested reading: comp.lang.c FAQ list ยท Question 3.8.
You're assigning the return value of scanf into your values. scanf returns how many characters it matched; you're already passing a reference to the variable you want scanf to store the result in.
Why are you assigning the return value of scanf into your num1 and num2?
My suggestion:
#include <stdio.h>
int main(int args, char*argv[])
{
int num1, num2;
scanf("%d",&num1);
scanf("%d", &num2);
printf("Num1 = %d\n", num1);
printf("Num2 = %d\n", num2);
return 0;
}

Resources