This is my assignment I am supposed to do however whenever I run my code I get the error stated in my question.
Create a function,
return type: char
parameters: int *, int *
Inside the function, ask for two integers. Set the user responses to the int * parameters.
Prompt the user for a character.
Get their response as a single character and return that from the function.
In main:
Define two variables and call your function.
Print the result of your function and the values of your variables.
Here's my code:
#include<stdio.h>
#include<stdlib.h>
char load(int *x, int *y);
void main()
{
int integer1 = 0;
int integer2 = 0;
int *pointer1;
int *pointer2;
pointer1 = &integer1;
pointer2 = &integer2;
char returnValue;
returnValue = load(*pointer1, *pointer2);
printf("%c", returnValue);
system("pause");
}
char load(int *x, int *y)
{
char input;
printf("Please enter two integers: ");
scanf_s("%d %d", x, y);
printf("Please enter a character:");
scanf_s(" %c", &input);
return input;
}
Related
the following is given
#include <stdio.h>
void func2 (int num, int *result);
int main()
{
int num, result;
printf("Enter a number: \n");
scanf("%d", &num);
func2(num, &result);
printf("func2(): %d\n", result);
return 0;
}
void func2(int num, int *result)
{
//key in code
}
in the void func2 i wrote
int i=0;
result=&i;
while (num!=0)
{
i+=((num%10)*(num%10));
num=num/10;
}
but the programming is not returning the value of variable i properly. what's wrong with my variable assignment?
expected output:
Enter a number:
24 (user enter)
func2(): 20
actual output:
Enter a number:
24 (user enter)
func2(): 32767
You need to assign indirectly through result, not set result to the address of another variable.
int i=0;
while (num!=0)
{
i+=((num%10)*(num%10));
num=num/10;
}
*result = i;
#include <stdio.h>
void getScores(int a, char n[10][15], int s[10]) {
int score;
printf("Enter the number of students: ");
scanf("%d",&a);
for (int i=0; i < a;i++)
{
scanf("%s",n[i]);
scanf("%d",&score);
s[i]=score;
}
}
void printScores(int a, char n[10][15], int s[10] ) {
for (int i=0; i < a;i++)
{
printf("%s", n[a]);
printf(" ");
printf("%d\n",s[a]);
}
}
int main() {
char names[10][15];
int scores[10];
int num;
getScores(num,names,scores);
printScores(num,names,scores);
}
What I am trying to accomplish is have the parameter value of int a from the getScores function to be used in the printScores function as an array length as it is being used in getScores.
The arrays are saving its value when used in the print function but the a value is resetting to an unassigned number 896 when I need it to be what the user enters in the get function. Any tips?
Print scores will not print anything.
void getScores(int *a,
And the call
getScores(&num
You need also change accordingly the functions code
This question already has answers here:
How to use a function containing scanf() in the main function in C
(3 answers)
Closed 3 years ago.
The function is not mapping the user input to the variable
#include <stdio.h>
void input(int x);
int main (void)
{
int h = 0;
input(h);
printf("%d\n", h);
}
void input(int x)
{
printf("Enter a number: ");
scanf("%d", &x);
}
When entering something like 5, I expect 5 to be printed
x was passing by value, scanf changed the local copy of x. You need to return user input instead:
int input();
int main (void)
{
int h;
h = input();
printf("%d\n", h);
}
int input()
{
int x;
printf("Enter a number: ");
scanf("%d", &x);
return x;
}
This is because parameters in C are passed by copy. So the x that you modify is just a copy of what you passed, and at the end of input, that copy is discarded. Instead, you could pass a pointer. Since scanf already operates on a pointer, the fix is simple:
void input(int *x); // parameter is now a pointer to an int
int main(void)
{
int h = 0;
input(&h); // pass the address instead of the value
printf("%d\n", h);
}
void input(int *x)
{
printf("Enter a number: ");
scanf("%d", x); // no need to take the address again
}
Any decent book, tutorial or class should have mentioned that arguments in C are passed by value. That means the value is copied into the local argument variable, and any modifications to that local variable won't reflect on the original variable.
There are two possible solutions:
The simple one: Return the value from the function
int input(void)
{
int x;
printf("Enter a number: ");
scanf("%d", &x);
return x;
}
...
int h = input();
printf("%d\n", h);
The complicated solution: Emulate pass by reference
void input(int *x)
{
printf("Enter a number: ");
scanf("%d", x);
}
...
int h;
input(&h); // Pass a pointer to the variable
printf("%d\n", h);
You call input(h); and then void input(int x) { ... }. The value of h is copied into x
If you want to get back the changed value, you can either :
return a value from the function input() :
// notice the int before function name
int input();
int main (void)
{
int h = 0;
// notice h = ...
h = input(h);
printf("%d\n", h);
}
// notice the int before function name
int input()
{
int x;
printf("Enter a number: ");
scanf("%d", &x);
// notice the return
return x;
}
Or, pass the variable as reference (pointer) :
// notice int *
void input(int *x);
int main (void)
{
int h = 0;
// notice the &h this is used to pass addresses of variables
input(&h);
printf("%d\n", h);
}
// notice int *
void input(int *x)
{
printf("Enter a number: ");
// notice the & removed
scanf("%d", x);
}
The problem is that you didn't understand the basic workflow of functions. Maybe you should once again revise your basics and theory.
This is how your code should have been written (I have tried my best to make minimal changes to your code):
#include <stdio.h>
int input();
int main (void)
{
int y;
y = input();
printf("%d\n", y);
}
int input()
{
printf("Enter a number: ");
scanf("%d", &x);
return x;
}
You should have set the return type of function input() as int. Then, a variable (y in this case) could get the value returned by input() and use it in the main() function to display the actual input that the user had given.
I am new to programming. I would like to call from functions(first, second, third the variables in function "enter" and in function "math" accordingly).
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void math (void);
void first (void);
void second (void);
void third (void);
void enter(void);
void scan(void);
int main()
{
first(); //function
second(); //function
third(); //function
enter(); //function
printf("\n\n Would you like to edit? (Y/N)\n\n");
scan(); //function
return(0);
}
Here is the user input:
void first (void){
float a;
printf("Enter First number: ");
scanf("%f",&a);
}
void second (void){
float b;
printf("Enter Second number: ");
scanf("%f",&b);
}
void third (void){
float c;
printf("Enter Third number: ");
scanf("%f", &c );
}
How can I call the variables from the user input to the function below?
void enter(){
float a,b,c;
printf("you have entered a: %f, b: %f, c:%f", a,b,c);
}
This is the loop in case the user would like to edit or continue with the initial input numbers.
void scan(void){
int ch;
scanf("%d", &ch);
if (ch==1){
main();
}else{
math();
}
}
Here I would also like to call the input variables from function (first, second third) in order to perform some math calculations.
void math (void){
float a,b,c;
float x,y,z;
x=a+b+c;
y= powf(x,2);
z=sqrt(y);
printf("Final Result of Addition is: %f \n Final Result of
Multiplication is: %f \n Final Result of squareroot is:%f\n
",x,y,z);
}
It's best if functions don't depend or modify data external to them. It's not always possible but it is better if they can be defined with that goal in mind.
In your case, the functions first, second, and third are doing essentially the same thing. It will be better to use just one function and give it a more appropriate name, such as read_float and change the return type so that the value input by the user is returned to the calling function.
Declare it as:
float read_float(char const* prompt);
Then, you can use it as:
float a = read_float("Enter the first number:");
float b = read_float("Enter the second number:");
float c = read_float("Enter the third number:");
etc.
The function enter does not correctly convey what it is doing. It should be renamed to something more appropriate, such as display_input_values. It can accept the values input by the user as its arguments.
Declare it as:
void display_input_values(float a, float b, float c);
and use it as:
float a = read_float("Enter the first number:");
float b = read_float("Enter the second number:");
float c = read_float("Enter the third number:");
display_input_values(a, b, c);
The scan function does not convey its meaning clearly either. You could divide scan to two functions -- one that returns you the option of whether to continue with the next inputs or to call a function that computes something with the user input.
You can get the option from the user by using a function named read_int function as:
int option = read_int("Enter 1 for next round of inputs and 2 to compute with current input:");
and use that returned value as:
if ( option == 2 )
{
// Do the computations
}
You also need another option to exit the program. The call to get the option needs to be:
char const* prompt = "Enter 1 for next round of inputs.\n"
"Enter 2 to compute with current input.\n"
"Enter 3 to exit program.\n";
int option = read_int(prompt);
Always add code to check whether scanf succeeded before proceeding to use the data that you were expecting to read into.
Here's an updated version of your code.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
float read_float(char const* prompt);
int read_int(char const* prompt);
void display_input_values(float a, float b, float c);
void math(float a, float b, float c);
int main()
{
while ( 1 )
{
float a = read_float("Enter first number: ");
float b = read_float("Enter second number: ");
float c = read_float("Enter third number: ");
display_input_values(a, b, c);
char const* prompt = "Enter 1 for next round of inputs.\n"
"Enter 2 to compute with current input.\n"
"Enter 3 to exit program.\n";
int option = read_int(prompt);
if ( option == 3 )
{
break;
}
else if ( option == 2 )
{
math(a, b, c);
}
}
}
float read_float(char const* prompt)
{
float num;
printf("%s", prompt);
// For flushing the output of printf before scanf.
fflush(stdout);
if (scanf("%f", &num ) != 1)
{
print("Unable to read number. Exiting.\n");
exit(1);
}
return num;
}
int read_int(char const* prompt)
{
int num;
printf("%s", prompt);
// For flushing the output of printf before scanf.
fflush(stdout);
if (scanf("%d", &num ) != 1)
{
print("Unable to read number. Exiting.\n");
exit(1);
}
return num;
}
void display_input_values(float a, float b, float c)
{
printf("You have entered a: %f, b: %f, c:%f\n", a, b, c);
}
void math(float a, float b, float c)
{
// Do whatever makes sense to you.
}
For example declare float a,b,c; in your Main and change your functions to return a float
float first() { ... }
Then writea = first();
Local variables (as name suggests) are local and can't keep their values after function ends. You have two options:
Make functions first, second and third return their numbers and another functions get them as arguments.
Make a,b,c global variables.
The second one is much easier, but it's so called bad programming style.
This is a simple enough problem I'm trying to figure out how to pass the int variables back to the other functions from the input function so it can be used in the stuff function to do the math then it returns the added variable and then passes all three to the output function.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int input(int first, int second, int third);
int stuff(int first, int second, int third, int added);
int output(int first, int second, int third, int added);
int main(){
int first,second,third;
int added;
//sub functions
input(first,second,third);
stuff(first, second, third, added);
output(first, second, third, added);
return(0);
}
int input(int first, int second, int third){
printf("Enter an interger for #1:");
scanf("%d",&first);
printf("Enter an interger for #2:");
scanf("%d",&second);
printf("Enter and interger for #3:");
scanf("%d",&third);
return first,second,third;
}
int stuff(int first, int second, int third, int added){
added = first + second + third;
return added;
}
int output(int first, int second, int third, int added){
printf("Integer 1 = %d\n",first);
printf("Integer 2 = %d\n",second);
printf("Integer 3 = %d\n",third);
printf("Integer 1,2,3 added together = %d\n",added);
}
Either use a struct
struct Foo {
int first, second, third;
}
struct Foo input() {
struct Foo foo;
printf("Enter an interger for #1:");
scanf("%d",&foo.first);
printf("Enter an interger for #2:");
scanf("%d",&foo.second);
printf("Enter and interger for #3:");
scanf("%d",&foo.third);
return foo;
}
Or pass pointers:
void input(int* first, int* second, int* third){
printf("Enter an interger for #1:");
scanf("%d",first);
printf("Enter an interger for #2:");
scanf("%d",second);
printf("Enter and interger for #3:");
scanf("%d",third);
}
int main(){
int first,second,third;
int added;
//sub functions
input(&first,&second,&third);
stuff(first, second, third, added);
output(first, second, third, added);
return(0);
}
You can use pointers and pass the address of those variables into each function. Every time you use it, you can dereference each variable in the functions.
You could just use pointers. I.e., change the function declaration to:
int input(int *first, int *second, int *third);
In the function itself, you use:
int input(int *first, int *second, int *third){
printf("Enter an interger for #1:");
scanf("%d",first);
printf("Enter an interger for #2:");
scanf("%d",second);
printf("Enter and interger for #3:");
scanf("%d",third);
}
And when you call it, use:
input(&first,&second,&third);
You could do the following:
void input(int *a, int *b, int *c);
int main()
{
int first, second, third;
/* ... */
input(&first, &second, &third);
/* ... */
}
void input(int *a, int *b, int *c)
{
printf("Enter an interger for #1:");
scanf("%d", a);
printf("Enter an interger for #2:");
scanf("%d", b);
printf("Enter and interger for #3:");
scanf("%d", c);
}