I need to pass by reference for my class. I'm getting the error in the title, but only for the functions divideByTweleve and multiplyByEleven. Why is that? What am I doing wrong?
Please help me figure this out.
#include <stdio.h>
void displayMenu();
float addTen (float* number);
float divideByTwelve(float* number);
float subtractSixteen(float* number);
float multiplyByEleven(float* number);
int menu;
float number, finalNumber;
float* ptr_number;
int main (void)
{
float* ptr_number=NULL;
ptr_number=&number;
printf("Please enter a number : ");
scanf("%f", &number);
displayMenu();
scanf("%d", &menu);
do {
printf("\nEnter 5 to see final number\n");
scanf("%d",&menu);
switch (menu)
{
case 1: number=addTen(ptr_number);
break;
case 2: number=divideByTwelve(ptr_number);
break;
case 3: number=subtractSixteen(ptr_number);
break;
case 4: number=multiplyByEleven(ptr_number);
break;
}
printf("Your number is: %.2f \n", number);
}while(menu!=5);//close of the do-while loop
}
void displayMenu()
{
printf("How would you like to manipulate your number\n");
printf("1. Add 10\n");
printf("2. Divide by 12\n");
printf("3. Subtract 16\n");
printf("4. Multiply by 11\n");
}
float addTen (float* number)
{
ptr_number=ptr_number+10;
}
float divideByTwelve (float* number)
{
ptr_number=ptr_number/12;
}
float subtractSixteen(float* number)
{
ptr_number=ptr_number-16;
}
float multiplyByEleven(float* number)
{
ptr_number=ptr_number*11;
}
Here is one of your (many) issues.
You declare addTen as:
float addTen (float* number);
And call it as:
number=addTen(ptr_number);
Which clearly indicates it returns a value.
Now, do you see a return statement in the function definition?
float addTen (float* number)
{
ptr_number=ptr_number+10; // No return statement!!
}
Next up, all your function take a float* as a parameter, which you should read as "Pointer to a Floating point number". Note that a pointer is not the same thing as the value itself.
To get from a pointer to a value, you need to "de-reference" it:
float addTen (float* number)
{
*ptr_number = *ptr_number + 10;
}
The star (*) before each ptr_number means to go from the pointer back to the actual value.
Before, you were trying to add 10 to a pointer.
(that is valid, but not appropriate in the current context)
Now, with the *, you're adding 10 to the value stored at the pointed-to location.
Ultimately, here's my corrected version of your code
void displayMenu();
void addTen (float* number);
void divideByTwelve(float* number);
void subtractSixteen(float* number);
void multiplyByEleven(float* number);
int main (void)
{
int menu;
float number;
printf("Please enter a number : ");
scanf("%f", &number);
displayMenu();
scanf("%d", &menu);
do {
printf("\nEnter 5 to see final number\n");
scanf("%d",&menu);
switch (menu)
{
case 1: addTen(&number); break;
case 2: divideByTwelve(&number); break;
case 3: subtractSixteen(&number); break;
case 4: multiplyByEleven(&number); break;
}
printf("Your number is: %.2f \n", number);
}while(menu!=5);//close of the do-while loop
printf("Your FINAL number is: %.2f \n", number);
}
void displayMenu()
{
printf("How would you like to manipulate your number\n");
printf("1. Add 10\n");
printf("2. Divide by 12\n");
printf("3. Subtract 16\n");
printf("4. Multiply by 11\n");
}
void addTen (float* ptr_number)
{
*ptr_number = *ptr_number+10;
}
void divideByTwelve (float* ptr_number)
{
*ptr_number = *ptr_number/12;
}
void subtractSixteen(float* ptr_number)
{
*ptr_number = *ptr_number-16;
}
void multiplyByEleven(float* ptr_number)
{
*ptr_number = *ptr_number*11;
}
Because you are not returning float values from these functions. If you want to pass a pointer to your functions then you should do this as
float divideByTwelve (float *)
{
*ptr_number= *ptr_number/12;
return *ptr_number;
}
But you have declared number as global variable, so no need to pass a pointer to these function. Just pass the number itself`;
float divideByTwelve (float number)
{
return number / 12;
}
Related
I wrote up a simple calculator that utilizes functions.
#include<stdio.h>
void operation_menu();
int getNumber(int,int);
int sum(int,int);
int diff(int,int);
int mult(int,int);
double quot(double,int);
int choice,num1,num2;
int main(){
getNumber(num1,num2);
operation_menu();
return 0;
}
int getNumber(int num1,int num2){
printf("Welcome to Simple Calculator!\n");
printf("Please input a number to begin:\n");
scanf("%d", &num1);
printf("Great! Now input the second number:\n");
scanf("%d", &num2);
}
void operation_menu(){
printf("Select an operation: \n");
printf("1. Addition\n");
printf("2. Subtraction\n");
printf("3. Multiplication\n");
printf("4. Division\n");
scanf("%d", &choice);
switch(choice){
case 1:
printf("The sum of %d and %d is %d.", num1,num2,sum(num1, num2));
break;
case 2:
printf("The difference of %d and %d is %d.", num1,num2,diff(num1, num2));
break;
case 3:
printf("The product of %d and %d is %d.", num1,num2,mult(num1, num2));
break;
case 4:
printf("The quotient of %d and %d is %.2lf.", num1,num2,quot(num1, num2));
break;
default:
printf("Please try again.");
}
}
int sum(int num1,int num2){
int answer;
answer=num1+num2;
return answer;
}
int diff(int num1,int num2){
int answer;
answer=num1-num2;
return answer;
}
int mult(int num1,int num2){
int answer;
answer=num1*num2;
return answer;
}
double quot(double num1,int num2){
double answer;
answer=num1/num2;
return answer;
}
The program worked correctly without breaking up my main() into several smaller functions, but after creating functions for each operation, input, and menu, I receive the wrong outputs. I figured this by printing num1 and num2 after using scanf and sure enough, I get the wrong outputs.
The function getNumber changes its local variables (parameters) num1 and num2. After exiting the function these local variables are not alive. The global variables with the same names stay unchanged.
You need to pass the global variables by reference through pointers to them. For example
int getNumber(int *num1,int *num2){
printf("Welcome to Simple Calculator!\n");
printf("Please input a number to begin:\n");
scanf("%d", num1);
printf("Great! Now input the second number:\n");
scanf("%d", num2);
}
The function is called like
getNumber( &num1, &num2 );
Pay attention to that there is no need to declare the variables num1, num2 and choice as global. You should declare them in the scope where they are used for example within the function operation_menu.
Also the both parameters of the function quot should have the type int. The function can be defined like
double quot( int num1,int num2){
double answer;
answer = ( double )num1/num2;
return answer;
}
I am somewhat done with making this function calculator on C program, but I have one issue, and it's really making me go nuts.
I can go through the code and everything, but I keep getting implicit declaration function and conflicting errors when I compile the code.
How can I fix my code to get rid of them?
I'm new to C, and programming in general. Thank you :)
#include <stdio.h>
int main(){
int response;
do{
displayWelcomeMessage();
scanf("%d",&response);
selectOption(response);
if(response<-1||response>8){
printf("Please enter a number between 1 and 8\n");
}
}while(response!=-1);
printf("Thanks for using the program!");
return 0;
}
void selectOption(int response){
int adder(int x,int y);
int subtract(int x,int y);
int multiplicate(int x,int y);
double divisor(int x,int y);
int modeOperator(int x,int y);
int factorialOperator(int x);
double powerOperator(int x,int y);
int FibonacciOperator(int x);
int x,y;
switch(response){
case 1 :
printf("the result is: %d\n",adder(x,y));
break;
case 2 :
printf("the result is: %d\n",subtract(x,y));
break;
case 3 :
printf("the result is: %d\n",multiplicate(x,y));
break;
case 4 :
printf("the result is: %lf\n",divisor(x,y));
break;
case 5 :
printf("the result is: %d\n",modeOperator(x,y));
break;
case 6 :
printf("Please enter a variable to calculate the factorial:\n");
scanf("%d",&x);
printf("the result is: %d\n",factorialOperator(x));
break;
case 7 :
printf("Enter base:\n");
scanf("%d", &x);
printf("Enter exponent:\n");
scanf("%d", &y);
printf("the result is: %lf\n",powerOperator(x,y));
break;
case 8 :
printf("enter a value:\n");
scanf("%d",&x);
for(int i=1;i<=x;i++){
printf("%d\n",FibonacciOperator(i));
}
break;
}
}
void displayWelcomeMessage(){
printf("welcome to final programming homework!\n");
printf("please enter 1 for the addition operation\n");
printf("please enter 2 for the subtraction operation\n");
printf("please enter 3 for the multiplication operation\n");
printf("please enter 4 for the division operation\n");
printf("please enter 5 for the mode operation\n");
printf("please enter 6 for the factorial operation\n");
printf("please enter 7 for the power operation\n");
printf("please enter 8 for the Fibonacci operation\n");
printf("please enter -1 to exit\n");
}
int adder(int x,int y){
printf("please enter two variables to be added:\n");
scanf("%d %d",&x,&y);
return x+y;
}
int subtract(int x,int y){
printf("please enter two variables to be subtracted:\n");
scanf("%d %d",&x,&y);
return x-y;
}
int multiplicate(int x,int y){
printf("please add two variables to be multiplied:\n");
scanf("%d %d",&x,&y);
return x*y;
}
double divisor(int x,int y){
printf("please add two variables to be divided:\n");
scanf("%d %d",&x,&y);
return (double)x/(double)y;
}
int modeOperator(int x,int y){
printf("please add two variables to find the reminder:\n");
scanf("%d %d",&x,&y);
return x%y;
}
int factorialOperator(int x){
if (x==1)
return 1;
else
return x*factorialOperator(x-1);
}
double powerOperator(int x,int y){
if(y == 0)
return 1;
else if(y > 0)
return (double)x * powerOperator(x,y-1);
else
return 1 / powerOperator(x,-y);
}
int FibonacciOperator(int x){
if (x==1 || x==2)
return 1;
else
return FibonacciOperator(x-1)+FibonacciOperator(x-2);
}
You're calling functions before they are defined or declared.
The C compiler runs in a single pass, so if you call a function before it's declared you'll get a warning. In that case the compiler will assume the function returns int. When the compiler then finds the actual definition, if it doesn't match the implicit definition you'll get an error.
You can fix this by either rearranging the functions so that they're defined before they're used, or by adding declarations for those functions at the top of the file.
So instead of:
int main(){
...
displayWelcomeMessage();
...
}
void displayWelcomeMessage(){
...
}
You want:
void displayWelcomeMessage(){
...
}
int main(){
...
displayWelcomeMessage();
...
}
Or:
void displayWelcomeMessage();
int main(){
...
displayWelcomeMessage();
...
}
void displayWelcomeMessage(){
...
}
Do the same for all functions other than main.
You need to include the header file for the functions that you use before defining. Either because they come from the standard library such as scanf or because you define them later.
I'm having serious trouble with my program it is supposed to provide a menu and do all the functions the code is pretty explanatory my problem is I only have visual studios which doesnt allow scanf and scanf_s and messes with things so I use online compilers but those are still iffy. Can any one help and give me some tips. I'm having trouble with the math and function to list accounts, some whiles are empty as well I wasn't sure if that was best to use. I'm relatively new to this forum. It also can't have struct and can only be in C :-(
#include <stdio.h>
char name[20];
float avail_bal;
void options();
void open();
void list();
void deposit();
void withdraw();
void exit();
int main(void)
{
char option;
while(1){
printf("****Banking System WELCOME****\n");
printf("Enter 1-5 of the following options: \n");
option = getchar();
scanf("%c\n", &option);
switch(option)
{
case '1': open();
break;
case '2': list();
break;
case '3': deposit();
break;
case '4': withdraw();
break;
case '5': return 0;
default: exit();
break;
}
}
return 0;
}
void options()
{
printf("1. Open Account\n");
printf("2. List Accounts\n");
printf("3. Deposit\n");
printf("4. Withdraw\n");
printf("5. Exit");
}
void open()
{
float avail_bal = 0;
char name[20];
int acc_num;
printf("Open new account(enter number 1-5)\n\n");
scanf("%d", &acc_num);
printf("Account number: %d\n");
printf("Available balance: %f\n");
}
void list()
{
}
void deposit()
{
float add;
int acc_num;
printf("Which count do you want to deposit money in?");
scanf(" %d", &acc_num);
printf("Amount to deposit: ");
scanf("%f", &add);
while()
{
}
}
void withdraw()
{
int acc_num;
float withdraw;
printf("Account to withdraw from: ");
scanf("%d", &acc_num);
printf("Amount to withdraw from account: ")
scanf("%f", &withdraw);
while()
{
printf("Current balance for account %d: %f ");
break;
} acc_num++
}
The problem with scanf was interesting. Here is an example for how to do without (although you shouldn't) such that you can play with your code easier.
#include <stdio.h>
#include <stdlib.h>
// only 5 accounts possible
int accounts[5];
// each its balance
float avail_bal[5];
void options();
// open(P) is a standard Posix function
void bopen();
void list();
void deposit();
void withdraw();
// exit(3) is a standard C function
void pexit();
int main(void)
{
char option;
while (1) {
printf("****Banking System WELCOME****\n");
printf("Enter 1-5 of the following options: \n");
options();
option = getc(stdin);
// swallow the '\n'
getc(stdin);
switch (option) {
case '1':
bopen();
break;
case '2':
list();
break;
case '3':
deposit();
break;
case '4':
withdraw();
break;
case '5':
pexit();
default:
pexit();
}
}
return 0;
}
void options()
{
puts("1. Open Account");
puts("2. List Accounts");
puts("3. Deposit");
puts("4. Withdraw");
puts("5. Exit");
}
void bopen()
{
int acc_num;
char c;
puts("Open new account(enter number 1-5)");
c = getc(stdin);
getc(stdin);
// assuming ASCII here where the digits 0-9 start at place 48 in the table
acc_num = (int) c - 48;
if (acc_num < 1 || acc_num > 5) {
puts("Account number must be between one and five inclusive");
return;
}
if (accounts[acc_num] != 0) {
printf("Account number %d is already taken\n", acc_num);
return;
}
// mark account as taken
accounts[acc_num] = 1;
// spend a fiver for the new client for being a new client
avail_bal[acc_num] = 5.0;
printf("Account number: %d\n", acc_num);
printf("Available balance: %f\n", avail_bal[acc_num]);
}
void list()
{
int i;
for (i = 0; i < 5; i++) {
if (accounts[i] != 0) {
printf("Account 000%d: %f\n", i, avail_bal[i]);
}
}
}
void deposit()
{
float add;
int acc_num;
char c;
char s[100];
puts("Which account do you want to deposit money in?");
c = getc(stdin);
getc(stdin);
acc_num = (int) c - 48;
printf("Amount to deposit: ");
// to get a number without scanf() we have to read the input as a string
// (fgets() adds a '\0' at the end, so keep a seat free for it)
fgets(s, 99, stdin);
// and convert it to a double (atof() only for brevity, use strtod() instead)
add = atof(s);
avail_bal[acc_num] += add;
printf("Amount deposited %f\n", add);
}
void withdraw()
{
int acc_num;
float withdraw;
char c;
char s[100];
// all checks ommitted!
puts("Account to withdraw from: ");
c = getc(stdin);
getc(stdin);
acc_num = (int) c - 48;
puts("Amount to withdraw from account: ");
fgets(s, 99, stdin);
withdraw = atof(s);
avail_bal[acc_num] -= withdraw;
printf("Current balance for account %d: %f\n", acc_num, avail_bal[acc_num]);
}
void pexit()
{
// place logic to save data here or use a function triggered by atexit() for that task
puts("Imagine all of your data would have been put in a safe place!");
exit(EXIT_SUCCESS);
}
Just replace the constructs with scanf before you pass your work for grading.
If you're using integer values as options why you are using character's
#include <stdio.h>
char name[20];
float avail_bal;
void options();
void open();
void list();
void deposit();
void withdraw();
void exit();
int main(void)
{
int option;
printf("****Banking System WELCOME****\n");
void options();
printf("Enter 1-5 of the following options: \n");
scanf("%d",&option);
switch(option)
{
case 1: open();
break;
case 2: list();
break;
case 3: deposit();
break;
case 4: withdraw();
break;
case 5: return 0;
default: exit();
break;
}
return 0;
}
void options()
{
printf("1. Open Account\n");
printf("2. List Accounts\n");
printf("3. Deposit\n");
printf("4. Withdraw\n");
printf("5. Exit");
}
void open()
{
float avail_bal = 0;
char name[20];
int acc_num;
printf("Open new account(enter number 1-5)\n\n");
scanf("%d", &acc_num);
printf("Account number: %d\n");
printf("Available balance: %f\n");
}
void list()
{
}
void deposit()
{
float add;
int acc_num;
printf("Which count do you want to deposit money in?");
scanf(" %d", &acc_num);
printf("Amount to deposit: ");
scanf("%f", &add);
while()
{
}
}
void withdraw()
{
int acc_num;
float withdraw;
printf("Account to withdraw from: ");
scanf("%d", &acc_num);
printf("Amount to withdraw from account: ")
scanf("%f", &withdraw);
while()
{
printf("Current balance for account %d: %f ");
break;
} acc_num++
}
Hi I'm trying to make an interactive menu with switch statement in C.
Though I'm unsure of how to trigger a function that has certain arguments.
I'm a total beginner and I'm stumped how to do this.
The function in the switch statement needs the arguments though I would like the function to ask for the numbers. I'm doing this as an assignment and cannot provide the actual code so I made this mock up. Thank you for your help.
Here is an example of code I might use.
#include <stdio.h>
void printMenu()
{
int choice;
do
{
printf("Main Menu:\n");
printf("1) do this\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
function(); /* though this needs the arguments */
break;
}
} while (choice != 7);
int main(void)
{
printMenu();
return 0;
}
void function(int number1, float number2)
{
/*calculation*/
printf("enter your numbers");
/* Not sure how to read the numbers in here */
printf("%d + %d = %d", number1, number2, number1 + number2);
return;
}
If you want the switch to be as minimal as possible then just call another function which takes in input and then calls the function...
case 1:
read_input_and_function()
break;
...
void read_input_and_function(void)
{
printf("Enter your numbers: ");
/* scanf number1, number2 */
function(number1, number2);
}
The function in the switch statement needs the arguments though I
would like the function to ask for the numbers.
How about asking the arguments first , and then calling the function. This way the two arguments can be declared once and be used in other functions of the same switch , but be defined according to the chosen case.
void function1(int, float);
void printMenu()
{
int choice = 0 , num1 = 0;
float num2 = 0;
do
{
printf("Main Menu:\n");
printf("1) do this\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("\nEnter number 1\n");
scanf("%d",&num1);
printf("\nEnter number 2\n");
scanf("%f",&num2);
function1(num1,num2);
break;
}
} while (choice != 7);
}
#include <stdio.h>
#include <stdlib.h>
#define Pi 3.14159216
/*
*small program of how to create a menu
*/
main ()
{
float degree,radians;
int input;
/*degrees to radians */
float degreesToRadians (float deg)
{
return ((Pi * deg) / 180.0);
}
/*radians to degrees*/
float radiansToDegrees (float rad)
{
return rad * (180 / Pi);
}
void menu ()
{
printf ("\n");
printf ("1. degrees\n");
printf ("2. radians\n");
printf ("3. quit\n");
do
switch (input) {
case 1:
printf ("\n");
printf ("\n");
printf (" Enter value of degrees: ");
scanf ("%f", °ree);
printf ("RADIANS = %f \n\n", degreesToRadians (degree));
menu ();
break;
case 2:
printf ("\n");
printf ("\n");
printf (" Enter value of radians: ");
scanf ("%f", &radians);
printf ("DEGREES = %f \n\n", radiansToDegrees (radians));
menu ();
break;
case 3:
printf (" quiting app \n");
exit (0);
break;
default:
printf ("wrong option\n");
break;
}
while (input != 3);
getchar ();
}
}
menu ();
}
Why do I keep getting this error? HELP ME this is homeowrk. I'm obiously new to programming help.
$ gcc homework.c
homework.c: In function ‘main’:
homework.c:32:6: error: static declaration of ‘DisplayMenu’ follows non-static declaration
homework.c:11:7: note: previous declaration of ‘DisplayMenu’ was here
#include <stdio.h>
void DisplayMenu();
void numberPlus10();
void numberTimes2();
void numberMinus1();
void numberTimesnumber();
int main (void)
{
int choice;
void DisplayMenu();
scanf("%i", &choice);
switch (choice)
{
case 1:
numberPlus10();
break;
case 2:
numberTimes2();
break;
case 3:
numberMinus1();
break;
case 4:
numberTimesnumber();
break;
default:
break;
}
void DisplayMenu()
{
printf("1. Number + 10\n");
printf("2. Number * 2\n");
printf("3. Number - 1\n");
printf("4. Number * Number\n");
}
void numberPlus10()
{
int x;
printf("Please enter a number:\n");
scanf("%i", &x);
printf("Your number + 10 is %i\n", x + 10);
}
void numberTimes2()
{
int x;
printf("Please enter a number:\n");
scanf("%i", &x);
printf("Your number * 2 is %i\n", x * 2);
}
void numberMinus1()
{
int x;
printf("Please enter a number:\n");
scanf("%i", &x);
printf("Your number - 1 is %i\n", x - 1);
}
void numberTimesnumber()
{
int x;
printf("Please enter a number:\n");
scanf("%i", &x);
printf("Your number squared is %i\n", x * x);
}
}
Pengyu CHEN is ofcourse right! But! You have another error there.
int choice;
void DisplayMenu(); // You should not declare a function here.
scanf("%i", &choice);
I guess you intend to call this function - so just remove "void" from the beginning of the line.
int choice;
DisplayMenu(); // Call DisplayMenu
scanf("%i", &choice);
And ... please read language specs
In C we don't implement functions inside any blocks. Instead functions shall be implemented in global scope.
Remove the very last right bracket and put it right after end of the switch in int main(void), and there shall be no more errors.
EDITED:
First of all.. I'm sure above is why your source code fails to compile.
Also, please check David's answer, since we all believe that you made a function declaration while you're intending to call it -- although this mistake didn't trigger an compile time error.