Why is the variable input2 = -1? [closed] - c

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 2 years ago.
Improve this question
Why is the variable input2 assigned -1 and why does it give me a memory value when I run the code?
#include <stdio.h>
int main(void)
{
int input1;
int input2 = -1;
while(input2 != 0)
{
scanf("%d", &input1);
scanf("%d", &input2);
printf("Sum: %d\n", input1 + input2);
}
if(input1==99)
puts("Finish.");
return 0;
}

You have a while loop that continues as long as input2 is not equal to 0, so in order to enter it the variable needs to be initialized with some arbitrary value that isn't 0. Functionally, you could have used 1, -123, 999 or any other non zero value and get the same effect.

Arbitrary value with no reason. If uninitialized, it will cause undetermined
behavior whether the while loop is taken or not (depending on what garbage is inside input2). The writer of this code wanted the while loop to enter. Could have rewritten it
int input1;
int input2;
do
{
scanf("%d", &input1);
scanf("%d", &input2);
printf("Sum: %d\n", input1 + input2);
} while(input2 != 0);

#Jonas Rye Nielsen this program is just a simple calculator to add 2 Integer number and that's it.
Now in order to quit and program, what it does is to check the second Input with some logic behind it.
If you enter a 0 to add 2 integer (or anyway in any kind of addition really) add a 0 would just make no sense at all, hence the progam is implicitly assuming that you want to exit it!.

Related

variable inside int in c giving exceptional output [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 1 year ago.
Improve this question
In below given program if I put n=0, then the program is giving right answer but if I write n instead of n=0, the program is giving me wrong answer. If I put k=10 then output is 94 but the correct answer is 55. Why it is adding additional 39?
int main(){
// program to calculate the sum of 'n' numbers
int i=1,k,n;
printf("Enter number: ");
scanf("%d",&k);
do{
n+=i;
i++;
}while(i<=k);
printf("The sum is: %d",n);
return 0;
}
Your loop is adding a value to the current value of n, but you never set the initial value of n. That means its value is indeterminate, and reading an indeterminate value (in this case when you add to it), when the variable in question never had its address taken triggers undefined behavior.
Initialize n to 0 so you have a valid starting point.
int i=1, k, n=0;

How can I fix this code and print the output [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 1 year ago.
Improve this question
I just started C programming , My code doesn't work properly . can you help me?
This is my code :
#include <stdio.h>
int main(){
int n,sum=0,a;
scanf("%d", &n);
while (n!=0)
{ a=n%10;
sum=sum+a;
n=n/10;
}
printf("%d",&sum);
return 0;
}
You should remove "&" from your printf statement. In C, using & before a variable name means you are referencing that variable's address location. When printing, placing %d indicates the variable you will pass into the print statement will be a number in decimal format, and the return type of &sum does not match this.
If you replace &sum with sum, you will be properly referencing the value of sum instead of its address, which matches the expected type for %d. Replacing your printf statement will give you this code:
#include <stdio.h>
int main(){
int n,sum=0,a;
scanf("%d", &n);
while (n!=0)
{
a=n%10;
sum=sum+a;
n=n/10;
}
printf("Sum of digits: %d", sum);
return 0;
}
You are not supposed to use & before sum in your printf. We use & in scanf in order to change change a variable. But by using printf, you are just trying to show the variable's value, not its address.
So all you need to do is to omit the & before sum in you printf.
This is how the last part of your code should look like:
printf("%d", sum);

C function with if return [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 2 years ago.
Improve this question
Im writing a code in C for college that involves the game rock(0)-paper(1)-scissor(2)-ecshe(3). The game should run with random numbers for both Player. In the end I have to get statiscal date of how often one player has won and how often rock(0)-paper(1)-scissor(2)-ecshe(3) apperead.
When I write a single code I can get all the information.
But when I am using a function I get only zero to calculate the statiscal data.
The value for Player_1 is always set as rock(1) in one of the rounds. The function is to calculate how many times Player_2 got paper(2) or scissor(3)
Making long story short:
How do I create a function to return me to possible answer according to the condition?
int gewinn_summe_a(int spl_2, int a, int b , int c, int d){
if(spl_2== a){
return ++b; //the counter for a variable that i have in the main function
}else if(spl_2==c){
return ++d;
}
// return 0;
if (player1 == 0) //this if set the value for player 1
if (player2 == 1 || player2 == 3){ //this 'if' says that the player2 can have this two option
//When the code below inside the program it does what is suppose to do:
//calculate how many time player2 gave option 1 or how many time he gave option3
if(spl_b==1){
count_sh_b++;
}else if(spl_b==3){
count_ec_b++;
}
//However, when i use the function, I always get zero as the counter.
gewinn_summe_a(1, count_sh_b , count_ec_b, spl_b)
printf("Contador SH B %d\n", count_sh_b);
printf("Contador EC B %d\n", count_ec_b);
printf("Spieler A: %d - %s\n", taste ,spiel_name[taste]);
printf("Spieler B: %d - %s\n", spl_b ,spiel_name[spl_b]);
printf("Player 1 A WON\n\n");
You need to pass the counters by reference, like this:
int gewinn_summe_a(int spl_2, int a, int *b, int c, int *d)
{
// ...
}
Then you call it by giving the addresses of the caller's variables:
gewinn_summe_a(spl_b, 1, &count_sh_b, 3, &count_ec_b)
BTW, your source is terrible, because it is incomplete, bad indented, and the variable names are mindlessly chosen.

C for loop troubleshooting [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 5 years ago.
Improve this question
So basically I was creating a program to ask the user how many times they wanted to test the program.But I couldnt figure out the problem with my for loop. So:
If the user wants to test the program 3 times, it should ask for the value of a 3 times only and it should quit after.
This is my code as follows:
#include <stdio.h>
int main()
{
int test;
printf("How many times do you want to test the program?");
scanf("%d", &test);
test = 0; // Reinitializing the test from 0
for (test=0; test=>1; test++) //I cant figure out whats going on with the for loop.
{
printf("Enter the value of a: \n");
scanf("%d", &test);
;
}
return 0;
}
The output should be:
"How many times do you want to test the program": 3
Enter the value of a : any numerical value
Enter the value of a: any numerical value
Enter the value of a: any numerical value
(exit)
In this section of your code:
scanf("%d", &test);
test = 0; // Reinitializing the test from 0
for (test=0; test=>1; test++)
First, memory owned by test is populated with a value entered by user. (this is OK)
Next, you nullify that new value in memory by setting test to zero. (this is not OK)
finally the construction of your loop statement is incorrect.
In a correct version of your for loop, test should be a value used as a limit against which an index is tested as that index is incremented across a range of values, for example, from 0 to some positive value.
You probably intended:
scanf("%d", &test);
//test = 0; // Reinitializing the test from 0 (leave this out)
for(int i = 0; i <= test; i++)
{
...
Where a separate index value (i) is incremented and tested against the limit test.

Bug in simple C source code? [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
I want to know if the format of user defined function i wrote i.e return(xxx) is correct or not .
Because when i compile my code, i have to enter the input 2 times.It might be a silly mistake because i just began learning C language
****MY CODE:****``
#include<stdio.h>
long cube(long x );
long input,answer;
int main (void )
{
printf("Enter a number:");
scanf("%ld ",&input);
answer = cube(input);
printf(" The cube of %ld is %ld",input ,answer);
return 0;
}
long cube(long x )
{
return (x*x*x);
}
****ANswer****
#include <stdio.h>
long cube(long x);
long input, answer;
int main( void )
{
printf("Enter an integer value: ");
scanf("%d", &input);
answer = cube(input);
printf("\nThe cube of %ld is %ld.\n", input, answer);
return 0;
}
long cube(long x)
{
long x_cubed;
x_cubed = x * x * x;
return x_cubed;
}
remove the space after '%ld' it will take one input.
according your code,
scanf("%ld '&input) ;
here compiler at first wants one input for '%ld' then it waits for blank space you used after '%ld'. remove it then it will go next step after one input.
you should use,
scanf("%ld%",&input);

Resources