warning: implicit declaration of function 'strcmp' [duplicate] - c

This question already has answers here:
I get implicit declaration of function strncmp
(2 answers)
Closed 3 years ago.
Creating a simple code that scans two numbers, asks the user if they would like to add or multiply them, and then performs the operation and prints the output.
#include <stdio.h>
int main(){
int num1;
int num2;
char oper[] = "";
printf("Enter a number: ");
scanf("%d", &num1);
printf("Enter another number: ");
scanf("%d", &num2);
printf("Would you like to add or multiply these numbers? ");
scanf("%s", &oper);
if(strcmp(oper, "multiply") == 0){
int prod = num1 * num2;
printf("The product is %d", prod);
}
else if(strcmp(oper, "add") == 0){
int sum = num1 + num2;
printf("The sum is %d", sum);
}
else{
printf("Why would you input something that you knew wouldn't work?");
}
return 0;
}

You are using strcmp function without declaring them. You need to include the header file which contains strcmp function declaration.
Use:
#include <string.h>

Related

Why can't I input the name and it kept coming up again before age [duplicate]

This question already has answers here:
scanf() leaves the newline character in the buffer
(7 answers)
Why is the gets function so dangerous that it should not be used?
(13 answers)
Closed 10 months ago.
#include <stdio.h>
void input(void);
void avgSalary(void);
struct Person{
char name[15];
int age;
float salary;
};
struct Person person[10];
int main(){
int choice, avg;
char cont;
do{
printf("1. Input person record \n2. Average salary\n");
printf("\nPlease enter selection: ");
scanf("%d", &choice);
if (choice == 1){
input();
}
else if (choice == 2){
avgSalary();
}
else{
printf("Error");
};
printf("\nContinue?: ");
scanf("%c", &cont);
printf("\n");
}while(toupper(cont)=='Y');
}
void input(){
int x;
getchar();
for (x=0;x<10;x++){
printf("Person %d\n", x+1);
printf("Name: ");
gets(person[x].name);
printf("Age: ");
scanf("%d", &person[x].age);
printf("Salary: ");
scanf("%f", &person[x].salary);
};
}
void avgSalary(){
int x, sum=0;
float avg=0;
for (x=0;x<10;x++){
sum += person[x].salary;
};
avg = sum/10;
printf("The average salary of %d person is %.2f\n", x, avg);
}
For the output, It asks for person's info and another is the average salary. We select 1 then after entering the first person's name, age and salary, I couldn't enter the next person's name all the way until the 10th person. Why is this happening?
Try replacing gets(person[x].name); with scanf .You could try scanf("%s",person[x].name); . Or you could probably (not recommended) add a getchar() at the end of your loop,after the last scanf call. In your first run through the loop,you are getting the behavior you want because you use the getchar() that is being called before the loop.
Edit:
Keep in mind that,using scanf you can't take an input containing a full name. If you want to do that,you can either use a seperate array for the last name in your Struct.Or else you can just probably get away with using getchar() and gets.

Calculator code wont work at all despite compiler showing 0 warnings [duplicate]

This question already has answers here:
How do I properly compare strings in C?
(10 answers)
Closed 1 year ago.
I was doing some amateur C practice. As you can see I'm trying to make a slightly more advanced calculator that allows the user to decide what they want to do. Even though I clearly defined what to write in if() function, it wont work at all. I would type in "add" or "mul" in the console but it always returns, "type a given mathematical function"
which is an outcome that I put in there to tell the user that they've given the wrong input. So far I've tried adding and removing the quotation mark in if() and replaced == with =. None of these work.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int num1;
int num2;
int mfunc;
printf("type in a mathematical function: add, subtract, divide or multiply ");
scanf_s("%d", &mfunc);
if (mfunc == "add") {
printf("enter first number: ");
scanf_s("%d", &num1);
printf("enter a second number: ");
scanf_s("%d", &num2);
printf("sum of two number is: %d", num1 + num2);
}
else if (mfunc == "sub") {
printf("enter first number: ");
scanf_s("%d", &num1);
printf("enter a second number: ");
scanf_s("%d", &num2);
printf("difference is: %d", num1 - num2);
}
else if (mfunc == "div") {
printf("enter first number: ");
scanf_s("%d", &num1);
printf("enter a second number: ");
scanf_s("%d", &num2);
printf("quotent is: %d\n", num1 / num2);
}
else if (mfunc == "mul") {
printf("enter first number: ");
scanf_s("%d", &num1);
printf("enter a second number: ");
scanf_s("%d", &num2);
printf("product is: %d", num1 * num2);
}
else {
printf("type a given mathematical function");
}
return 0;
}
you can't compare "add" with myfunc. add is a string (array of chars of size 4), myfunc is an int. In C you can't compare int to string. you can compare string to string so you can ask from the user to input his func by write "add", but it is a bit more complex for you right now (you need to scan it to a proper array of chars and compare the strings using function which might be case sensitive, etc.). also, you can use instruction for the user - some kind of map so the user could insert 0 for add, 1 for sub and so on..
also, I used "define" witch is macro (an pre-processor directive) to more readable code;
#include <stdio.h>
#include <stdlib.h>
#define ADD 0
#define SUB 1
#define DIV 3
#define MUL 4
int main()
{
int num1;
int num2;
int mfunc;
printf("type in a mathematical function: add, subtract, divide or multiply \n");
printf("to add insert 0,\nto subtract insert 1,\n");
printf("to divide insert 2,\nto multiply insert 3\n");
scanf("%d", &mfunc);
if (mfunc == ADD) {
printf("enter first number: ");
scanf("%d", &num1);
printf("enter a second number: ");
scanf("%d", &num2);
printf("sum of two number is: %d", num1 + num2);
}
else if (mfunc == SUB) {
printf("enter first number: ");
scanf("%d", &num1);
printf("enter a second number: ");
scanf("%d", &num2);
printf("difference is: %d", num1 - num2);
}
else if (mfunc == DIV) {
printf("enter first number: ");
scanf("%d", &num1);
printf("enter a second number: ");
scanf("%d", &num2);
printf("quotent is: %d\n", num1 / num2);
}
else if (mfunc == MUL) {
printf("enter first number: ");
scanf("%d", &num1);
printf("enter a second number: ");
scanf("%d", &num2);
printf("product is: %d", num1 * num2);
}
else {
printf("type a given mathematical function");
}
return 0;
}
if you really want the user to input string you should be do that:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
int num1;
int num2;
char mfunc[256];
printf("type in a mathematical function: add, subtract, divide or multiply \n");
gets(mfunc);
if (strcmp(mfunc, "ADD") == 0||strcmp(mfunc, "add") == 0) {
printf("enter first number: ");
scanf("%d", &num1);
printf("enter a second number: ");
scanf("%d", &num2);
printf("sum of two number is: %d", num1 + num2);
}
else if (strcmp(mfunc, "SUB") == 0||strcmp(mfunc, "sub") == 0) {
printf("enter first number: ");
scanf("%d", &num1);
printf("enter a second number: ");
scanf("%d", &num2);
printf("difference is: %d", num1 - num2);
}
else if ((strcmp(mfunc, "DIV") == 0||strcmp(mfunc, "div") == 0)) {
printf("enter first number: ");
scanf("%d", &num1);
printf("enter a second number: ");
scanf("%d", &num2);
printf("quotent is: %d\n", num1 / num2);
}
else if ((strcmp(mfunc, "MUL") == 0||strcmp(mfunc, "mul") == 0)) {
printf("enter first number: ");
scanf("%d", &num1);
printf("enter a second number: ");
scanf("%d", &num2);
printf("product is: %d", num1 * num2);
}
else {
printf("type a given mathematical function");
}
return 0;
}
I know I used gets(), see paul-kapustin in this Q.
How to read a line from the console in C?

Do while loop C coding error

What is going wrong here? I am getting the error Use of undeclared identifier 'answer'
Here's my code:
if (CalculatorChoice == 1) do {
int a;
int b;
int sum;
char answer;
printf ("You have choosen addition, please enter first number: ");
scanf("%d", &a);
printf ("Now please enter second number to addit: ");
scanf("%d", &b);
printf("The sum is: %d \n\n", sum = a+b);
printf("Do you want to go back to menupage? (y/n): ");
answer = getchar();
getchar();
} while(answer=='y');
if (CalculatorChoice == 1)
do {
/* ... */
char answer;
/* ... */
} while(answer=='y');
You have declared the variable answer inside the loop block, but it is accessed from the while condition, which is outside the loop block.
You can move the declaration outside the loop body to fix this:
if (CalculatorChoice == 1) {
char answer;
do {
/* ... */
} while(answer=='y');
}
Your version has variable declared inside block, is not accessible out of block
This code changes position
if (CalculatorChoice == 1)
{
char answer;
do {
int a;
int b;
int sum;
printf ("You have choosen addition, please enter first number: ");
scanf("%d", &a);
printf ("Now please enter second number to addit: ");
scanf("%d", &b);
printf("The sum is: %d \n\n", sum = a+b);
printf("Do you want to go back to menupage? (y/n): ");
answer = getchar();
getchar();
} while(answer=='y');
}

Please help me spot my error [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
Hey I am writing a simple program that adds fractions.
but whenever i run the program it will not perform the operations.
It only scans the input, but no output.
please spot my error! >>>>>>CANNOT USE FLOATS<<<<<<<<
#include <stdio.h>
int main ( void )
{
int lcd, d1, d2, num1, num2, sum;
printf("Enter the first number:");
printf("Enter the numerator:");
scanf("%d", num1);
printf("Enter the denominator:");
scanf("%d", &d1);
printf("Enter the second number:");
printf("Enter the numerator:");
scanf("%d", &num2);
printf("Enter the denominator:");
scanf("%d", &d2);
for (lcd = d1; lcd % d2 != 0; lcd+= d1);
num1 *= lcd / d1;
num2 *= lcd / d2;
sum = num1 + num2;
printf("%d", sum);
return 0;
}
scanf must receive pointers
scanf("%d", num1) -> scanf("%d", &num1)
Because scanf use call by reference. What is that? please see the following code.
#include <stdio.h>
void foo(int i)
{
i = 1;
}
void bar(int *pi)
{
*pi = 1;
}
int main()
{
int a = 2;
foo(a);
printf("%d", a); /* output is 2 */
bar(&a);
printf("%d", a); /* output is 1 */
}
In foo(a), we're using call by value. That means a is copied when we call foo. the code, i = 1, of foo changes only the copy of a, and does NOT change real value of a.
In bar(&a), we're using call by reference. Can you find the difference? Yes, it's bar(&a), not bar(a). "&" operator gets the pointer of a, and we call bar with the pointer. So pi refers to a, and *pi = 1 changes real value of a successfully.
A real-life example is here: printf and scanf.
int i = 0;
printf("output number : %d\n", i);
scanf("%d", &i); /* input number */
printf doesn't need to change its arguments, so it use call-by-value.
But, scanf receives user's input and change its arguments into user's input. so it use call-by-reference. thus, we should use &i, not i.
Does it need to be that complex? Why not just divide the numerator by the denominator?
Also, your working with integers. I think you want floats.
#include <stdio.h>
int main ( void )
{
int d1, d2, num1, num2;
printf("Enter the first number:");
printf("Enter the numerator:");
scanf("%d", &num1);
printf("Enter the denominator:");
scanf("%d", &d1);
printf("Enter the second number:");
printf("Enter the numerator:");
scanf("%d", &num2);
printf("Enter the denominator:");
scanf("%d", &d2);
printf("%f",
((float) num1 / (float) d1) + ((float) num2 / (float) d2)
);
return 0;
}
If you want to compute the non simplified sum of two fractions, just using the following formula
a/b + c/d = (a*d + c*b)/(b*d)
Therefore, the following code could be used to achieve your goal
#include <stdio.h>
int main ( void )
{
int den1, den2, num1, num2, sum_num, sum_den;
printf("Enter the first number:\n");
printf("\tEnter the numerator:");
scanf("%d", &num1);
printf("\tEnter the denominator:");
scanf("%d", &den1);
printf("Enter the second number:\n");
printf("\tEnter the numerator:");
scanf("%d", &num2);
printf("\tEnter the denominator:");
scanf("%d", &den2);
sum_num = num1 * den2 + num2 * den1;
sum_den = den1 * den2;
printf("sum = %d/%d\n", sum_num, sum_den);
return 0;
}

Program skips scanf when using scanf for integer along with char values [duplicate]

This question already has answers here:
Why scanf is behaving weird for char input?
(5 answers)
Does scanf() take '\n' as input leftover from previous scanf()?
(1 answer)
Closed 8 years ago.
#include <stdio.h>
int multiple(int num1,int num2){
return (num1*num2);
}
int add(int num1, int num2){
return (num1+num2);
}
/*&x points to its value space *x points to its memory space*/
int main(){
int num1,num2,ans;
char func;
printf("First number => ");
scanf("%d",&num1);
printf("Second number => ");
scanf("%d",&num2);
printf("Please Enter + for addition, or * for multiplication => ");
scanf("%c",&func);
if (func == '*'){
ans = multiple(num1,num2);
}else if(func == '+') {
ans = add(num1,num2);
}else {
printf("Sorry, invalid operation");
}
printf("Ans : %d",ans);
return 0;
}
When i run my programme it will prompt me for firest and the second number however it does not promts me for the char input scanf("%c",&func); is not being executed.
My ouput -----------------------------------------------------------------:
$ ./p8t3
First number => 23
Second number => 32
Please Enter + for addition, or * for multiplication => Sorry, invalid operationAns : 2665616
while scanning for the + or * operator, change as below:
printf("Please Enter + for addition, or * for multiplication => ");
scanf(" %c",&func); //use a space before '%c'
printf("Please Enter + for addition, or * for multiplication => ");
scanf(" %c",&func);
The reason is that when you input a number and press ENTER, the scanf will process that number, but the new line is still in the input buffer.

Resources