I am using Code::blocks to compile my first multiple source file, learned from "C Programming in easy steps" by Mike McGrath. Unfortunately my math functions seem to be having issues. Here's the header that contains the functions:
/* this header file contains utility functions */
int square(int x); /* function prototypes */
int multiply(int x, int y);
int square(int x)
{
return (x*x);
}
int multiply(int x, int y)
{
return (x*y);
}
The only function having the problem is "square()". It reads the input of "2" as "2293356" and outputs the square as "553755367"... What the heck?!?
Here's the menu.c file... There's menu.c, ops.c, calc.c, and utils.h. Abaov is the .h.
MENU.c
include
void menu();
void menu()
{
int num;
printf("\n\tEnter the number of an operation:\n");
printf("\t1. Square a number\n");
printf("\t2. Multiply two numbers\n");
printf("\t3. Exit\n");
scanf("%d", &num);
switch(num)
{
case 1 : getnum(); break;
case 2 : getnums(); break;
case 3 : return;
}
}
Here's ops.c...
#include <stdio.h>
#include "utils.h"
void getnum();
void getnums();
void getnum()
{
int num;
printf("Enter an integer to be squared: ");
scanf("%d", &num);
printf("%d squared is %d\n, num, square(num)");
menu();
}
void getnums()
{
int num1, num2;
printf("Enter two numbers to be multiplied, ");
printf("seperated by a space: ");
scanf("%d", &num1);
scanf("%d", &num2);
printf("%dx%d = %d\n", num1, num2, multiply(num1, num2));
menu();
}
This is the last part of the program, calc.c,
#include <stdio.h>
int main()
{
menu();
printf("end\n");
return 0;
}
The square of 2293356 doesn't fit into int and therefore overflows which leads to undefined behavior! As to why it reads 2 as 2293356 cannot be answered without more code.
Update:
And here's your real error:
printf("%d squared is %d\n, num, square(num)");
should be
printf("%d squared is %d\n", num, square(num));
:)
Related
Why can't my code run when I am using a function name "div". But when I change it to any name like " divi",it runs.Can someone explained it? Im 1st yr.
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
double div(int, int);
int main(void)
{
int num1, num2;
printf("Enter the numbers: \n");
scanf("%d", &num1);
printf("Enter the numbers: \n");
scanf("%d", &num2);
printf("The answer is %.2f", div(num1,num2));
}
double div(int x, int y)
{
double quot;
quot=((double) x/y);
return quot;
}
The standard library already defines a function called div, so you can't use that name for your own function.
I'm creating a calculator that adds, subtracts, and multiplies complex numbers, but I keep getting errors on my displayComplexNumber. Each time I try to compile, it says, "error: expected expression before ‘double’" or "error: too few arguments to function ‘displayComplexNumber’"
#include <stdio.h>
#include <stdlib.h>
int getMenuChoice();
void getComplexNumber(double* num, double* imagine);
void addComplexNumber(double num1, double imagine1, double num2, double imagine2, double* num, double* imagine);
void displayComplexNumber(double* num, double* imagine);
int main()
{
double num1,imagine1, num2, imagine2, num, imagine;
int choice;
do
{
choice = getMenuChoice();
switch (choice)
{
case 1: // addition
getComplexNumber(&num1, &imagine1);
getComplexNumber(&num2, &imagine2);
addComplexNumber(num1, imagine1, num2, imagine2, &num, &imagine);
displayComplexNumber(double* num, double* imagine)
break;
case 0: // display
break;
default:
break;
}
}while (choice != 0);
return 0;
}
int getMenuChoice()
{
int choice;
printf("1 - addition\n");
printf("0 - EXIT\n");
scanf("%d",&choice);
return choice;
}
void getComplexNumber(double* num, double* imagine)
{
printf("Enter the real component\n");
scanf("%lf", num);
printf("Enter the imaginary component\n");
scanf("%lf", imagine);
}
void addComplexNumber(double num1, double imagine1, double num2, double imagine2, double* num, double* imagine)
{
*num = num1 + num2;
*imagine = imagine1 + imagine2;
}
void displayComplexNumber(double* num, double* imagine)
{
printf("*RESULT*\n");
printf("%.2lf + %.2lfi\n", num, imagine);
}
displayComplexNumber() shouldn't take pointers, it should just take doubles.
void displayComplexNumber(double num, double imagine)
You need to fix this in the prototype at the beginning, and in the function definition.
Then when you call it, you don't put the parameter types. It should just be:
displayComplexNumber(num, imagine);
I am trying to compile a program in C, but I keep getting the following error:
Segmentation fault
Here is the code:
#include <stdio.h>
#define calculation1 main
void calculation1(int *num1, int *num2) {
int total;
printf("Program One: \n");
printf("=========== \n");
printf("Number One: ");
scanf("%d", &num1);
printf("Number Two: ");
scanf("%d", &num2);
total = *num1 + *num2;
printf("%d + %d = %d \n",&num1,&num2,total );
}
What am I doing wrong here? How can I fix this error?
What am I doing wrong here? How can I fix this error?
Problem 1
By using
#define calculation1 main
void calculation1(int *num1, int *num2) {
you are essentially using:
void main(int *num1, int *num2) {
This is wrong. main needs to be:
int main(void) {
or
int main(int argc, char** argv) {
Your program is subject to undefined behavior.
Problem 1
You are using
scanf("%d", &num1);
scanf("%d", &num2);
when num1 and num2 are of type int*. Thy need to be:
scanf("%d", num1);
scanf("%d", num2);
Problem 3
You are using
printf("%d + %d = %d \n",&num1,&num2,total );
Given the type of num1 and num2, that needs to be:
printf("%d + %d = %d \n", *num1, *num2, total );
Fix
Your program needs a bit of an overhaul. Try:
#include <stdio.h>
#define calculation1 main
int calculation1() {
int num1; // Not int*. If you use int*, you'll need to allocate memory
int num2;
int total;
printf("Program One: \n");
printf("=========== \n");
printf("Number One: ");
scanf("%d", &num1); // You need to use &num1 since num1 is of type int.
printf("Number Two: ");
scanf("%d", &num2);
total = num1 + num2;
printf("%d + %d = %d \n", num1, num2, total);
}
scanf("%d", num1);
scanf("%d", num2);
scanf needs an address and num1 and num2 already contain the address as you pass them as pointers in the function.
The other thing to change is:
printf("%d + %d = %d \n",*num1,*num2,total );
*num1 dereferences a pointer to provide the value
Despite errors pointed by #jayant-
#define calculation1 main
void calculation1(int *num1, int *num2) {
So , calculation1 will be replaced by main ,so in short this is your main function .
This is not a valid and should avoid it any case . I am confused how you make a call or take command line argument ? But this is definitely incorrect .
Simply,do this -
int main(void) or int main(int argc,char *argv[]) and declare num1 and num2 as int variables and then take input in it and perform desired operation.
I want to change this codes to get 20 Numbers from input and count how many are Odd and how many are Even? please can anyone help?!
#include <conio.h>
#include <stdio.h>
int main()
{
int n;
int odd=0;
int even=0;
printf("\nEnter any number \n");
scanf("%d",&n);
if(n%2!=0)
{
printf("%d is an odd number",n);
odd++;
}
else
{
printf("%d is an even number",n);
even++;
}
printf("\n odd%d / even%d",odd,even);
}
Here is a function that solves your problem:
Tip: You need a loop to take your input 20 times.
void countForJHikaam(){
int n,i;
int odd=0;
int even=0;
for(i=0;i<20;i++){
scanf("%d\n",&n);
if(n%2==0){
even++;
}else{odd++;}
}
printf("Odds: %d, Evens: %d",odd,even);
}
It won't really help you in learning. Now go learn what a function is.
#include <conio.h>
#include <stdio.h>
int main()
{
int n;
int odd=0;
int even=0;
printf("\nEnter any number \n");
while(scanf("%d",&n))
(n%2) ? (++odd) : (++even);
printf("\n odd%d / even%d",odd,even);
}
#include<stdio.h>
main()
{
int odd=0,even=0,no,count=20;
printf("Enter the 20 numbers...\n");
here:
scanf("%d",&no);
(no%2==0)? odd++ : even++ ;`
count--;
if(count>0)
goto here;
printf("No of odd numbers... :%d\n",odd);
printf("No of even numbers... :%d\n",even);
}
Guys can you help me solve this problem is c programming. I dont get it why i get nested functions when i try to compile the code. I always got ISO forbids nested functions. Please help me understand the code. 3 days. im loosing my mind. Im still new to c programming. thank you!
#include<stdio.h>
#define lcost 35;
#define tax 0.85;
void readData(int* Len,int* Wid,float* Disc,float* cost);
int calcInPrice(float area,float iPrice,float cost,int Len,int Wid);
int calcSTotal(float sTotal,float Disc,float iPrice,float tDisc);
int calcTPrice(float tPrice,float ctax,float sTotal);
void printMes(int Len, int Wid);
void printCharges(float area,float cost,float iPrice,float Disc,float tDisc,float tPrice,float ctax,float sTotal);
int main(void)
{
int x;
int Len, Wid;
float item;
float area, cost, Disc, iPrice, sTotal, tDisc, tPrice, ctax;
do{
system("cls");
printf("[1] Perform\n");
printf("[2] Exit progres\n");
printf("\n\nInput Selection:");
scanf("%d", &x);
switch (x)
{
case 1:
readData(&Len,&Wid,&Disc,&cost);
calcInPrice(area,iPrice,cost,Len,Wid);
calcSTotal(sTotal,Disc,iPrice,tDisc);
printMes(Len,Wid);
printCharges(area,cost,iPrice,Disc,tDisc,sTotal,ctax,tPrice);
printf("\n\nPress any key to return to main menu");
getch();
break;
case2:
system("cls");
printf("Exiting Program");
break;
default:
printf("\n\nInvalid Selection!");
}
}while(x < 3);
void readData(int* Len,int* Wid,float* Disc,float* cost)
{
printf("Input Length of Room: ");
scanf("%d",Len);
printf("Input Width of Room: ");
scanf("%d",Wid);
printf("Input Discount of Customer: ");
scanf("%f",Disc);
printf("Input the cost per square foot: ");
scanf("%f",cost);
return;
}
int calcInPrice(float area,float iPrice,float cost,int Len,int Wid)
{
area = Len * Wid;
iPrice = (lcost * area) + (cost * area);
return;
}
int calcSTotal(float sTotal,float Disc,float iPrice,float tDisc)
{
Disc = (Disc / 100)*iPrice;
sTotal = iPrice - (tDisc);
return;
}
int calcTPrice(float tPrice,float ctax, float sTotal)
{
ctax = sTotal * tax;
tPrice = sTotal + ctax ;
}
void printMes(int Len, int Wid)
{
printf("\n\t\tMEASUREMENT\n\n");
printf("Length\t\t %d ft", Len);
printf("Width\t\t %d ft", Wid);
printf("\nArea\t\t %d square ft", Len * Wid);
return;
}
void printCharges(float area,float cost,float iPrice,float Disc,float tDisc,float tPrice,float ctax,float stotal)
{
float item = cost * area;
printf("\n\n\t\tCHARGES\n\n");
printf("DESCRIPTION\tCOST/SQ.FT.\tCHARGE");
printf("\n________\t _________\t______");
printf("\nCarpet \t%.2f \t %0.2f ",cost,item);
printf("\nLabor \t %lf \t %0.2f",lcost, area);
printf("\n\t\t\t__________");
printf("INSTALLED PRICE \t\t %.2f", iPrice);
printf("\nDiscount \t %.2f \t %.2f ",Disc,tDisc);
printf("\n\t\t\t\t______");
printf("\nSUBTOTAL\t\t\t %.2f",sTotal);
printf("\nTax\t\t\t\t %2f",ctax);
printf("\nTOTAL\t\t\t\t %.2f",tPrice);
return;
}
return 0;
}
Your code looks like this:
int main(void)
{
...
void readData(int* Len,int* Wid,float* Disc,float* cost)
{
...
}
... more functions ...
void printCharges(float area,float cost,float iPrice,float Disc,float tDisc,float tPrice,float ctax,float stotal)
{
...
}
return 0;
}
which means that you're "nesting" all of your function definitions (readData, printCharges, etc.) inside your main function.
That's not allowed.
Instead, you need to write something more like this:
int main(void)
{
...
return 0;
}
void readData(int* Len,int* Wid,float* Disc,float* cost)
{
...
}
... more functions ...
void printCharges(float area,float cost,float iPrice,float Disc,float tDisc,float tPrice,float ctax,float stotal)
{
...
}
You cannot define a function within another function in standard C.
You can declare a function inside of a function, but it's not a nested function.
gcc has a language extension that allows nested functions. They are nonstandard, and as such are entirely compiler-dependent.
The problem is that your main function doesn't end before you start to define your readData() function, so to the compiler it looks like you are defining "readData()" inside of main(). This is called nesting, and is not valid in C. This is one reason why it's important to choose a style for indentation and bracket placement in languages like C.
I'm going to help you figure out how to figure out how to fix it, by winding your program back to a better basis to start from, and you can gradually reintroduce the rest of your code -- don't just add it all in at once, but add it piece-by-piece and make sure it works as you go.
#include <stdio.h>
// proto-type, tells the compiler that we will define this function later.
void readData(int*, int*, float*, float*);
int main()
{
int Len, Wid;
float Disc, Cost;
printf("in main\n");
readData(&Len, &Wid, &Disc, &Cost);
return 0;
} // end of main()
void readData(int* Len, int* Wid, float* Disc, float* Cost)
{
printf("We're in readData now\n");
} // end of readData()
Here it is on ideone so you can see it works.