I want to write a loop that runs until the user enters a number greater than 10, but I have to do something wrong because it creates an infinite loop.
int main()
{
int a;
printf("Enter 'a' value (min 10): ");
scanf("%d",&a);
for(int i=0;a<10;i++){
printf("Enter value>10");
i++;
printf("%d",&a);
}
printf("Result:%d",a+a-2+a-4+a-6+a-8+a-10);
return 0;
}
You mix an index that does not make sense. Also you print the memory address of variable instead of its value, not sure it is what you wanted?
Code partially corrected (because I don't know what is your ultimate goal):
#include <stdio.h>
int main()
{
int a;
do {
printf("Enter 'a' value (min 10): ");
scanf("%d",&a);
printf("\na: %d\n",a);
} while (a <= 10);
printf("Result:%d\n",a+a-2+a-4+a-6+a-8+a-10);
return 0;
}
ps: \n is line return and added do while which is what you want when you want to execute a loop at least once.
Have a look at your for-loop: you let i start at zero, you continue until a is not smaller than ten anymore, but it's not the value of a you need to check, it's the one of i.
In top of that, you are doing a i++ within your for-loop, while this is already covered in the definition of the for-loop.
I think this is the code that you are looking for: See comments
#include <stdio.h>
int main()
{
int a, ok = 0, end_of_input = 0;
do {
printf("Please input an integer value (min. 10): ");
fflush(stdout); // So the user can see the above line!
switch(scanf("%d",&a)) {
case EOF: // End of input - Give up!
end_of_input = 1;
break;
case 1: // Got a number - Check it!
if (a < 10)
{
ok = 1;
} else {
printf("%d - Not appropriate input. Please try again.\n\n",a);
}
break;
default: // Summat else - "eat" the input to the next line
scanf("%*[^\n]\n"); // "eats" the rest of the line in the buffer w/o assignment
break;
}
} while (end_of_input == 0 || ok == 0);
if (ok) { // User entered a valid number
printf("Got a that is smaller than ten %d\n", d);
} else { // We have ran out of input
printf("See you want to leave us :-(\n");
}
return 0;
}
I am not sure what you are trying to achieve but one problem that I found in your logic is you prompting user for input outside the loop. So whenever you enter number less than 10 it always goes in infinite iteration.
Try following code, with scanf inside loop
int main()
{
int a;
printf("Enter 'a' value (min 10): ");
scanf("%d",&a);
int i=0;
for(;a<10;){
printf("Enter value>10");
scanf("%d",&a);
printf("%d",a);
i++;
}
printf("Result:%d",a+a-2+a-4+a-6+a-8+a-10);
return 0;
}
Related
I wrote this loop to add numbers, and the break to get out of the loop if the number entered is less than zero, and in last print the calculated numbers without adding the negative number. but the problem is even I wrote the break statement before the addition when I enter 15 and 15 and -2 the output is 28 rather than 30
I found out how to fix that, what I want to know is why
and thank you.
#include <stdio.h>
void main()
{
int j = 1, num = 0, rslt = 0;
while (1) {
if (num < 0) break;
printf("enter a number : ");
scanf("%d", &num);
rslt = rslt + num;
}
printf("the resluts are %d\n", rslt);
}
Currently, you are effectively testing the input of the previous iteration, after already adding it to your result. Instead, check the number immediately after the user enters it, before you perform any calculations.
#include <stdio.h>
int main(void)
{
int num = 0, rslt = 0;
while (1) {
printf("enter a number : ");
scanf("%d", &num);
if (num < 0)
break;
rslt += num;
}
printf("the results are %d\n", rslt);
}
You might also want to check that scanf returns the number of successful conversions you were expecting (in this case one), to handle the event where the user enters invalid input.
if (1 != scanf("%d", &num))
break;
So I am writing this code for simple shape determining program, and I was trying to loop it, the condition, for the loop to end is when the value of the variable x is equal to 0 (all of teh variables are angels), since an angle cannot be 0, I think it is only logical to have the loop loop until the value entered is 0. However, the code does not seem to stop iterating even after the condition is met. the code is below:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x=0;
int y=0;
int A=0;
do {
printf("What is the value of x?");
scanf("%d", &x);
printf("What is the value of y?");
scanf("%d", &y);
printf("What is the value of A?");
scanf("%d", &A);
if ((A==90)&&(x==y))
{
printf("Square\n");
}
else if ((A==90)&&(x!=y))
{
printf("Rectangle\n");
}
else if ((A==60)||(A==120))
{
printf("Hexagonal\n");
}
else if ((A!=60)||(A!=120)&&(x==y))
{
printf("Rhombic\n");
}
else if ((A!=60)||(A!=120)&&(x!=y))
{
printf("Parallelogram\n");
}
else
{
printf("The values you have entered are not supported by the program,
try again!");
}
}while(x!=0);
printf("Thanks for using the program!");
system("pause");
return 0;
}
I cannot really see what the problem with the condition for the while loop is, please help!
The easiest way may be:
scanf("%d", &x);
if (!x) break; // break the loop, not asking anything else
I'm trying to create a program in which the user enters three integers, and another function checks to see that their input is valid. If the input is not valid, then the user must input three new numbers.
#include <stdio.h>
int sanitizedInput(int a, int b, int c)
{
if(scanf("%d", &a)==0)
{
printf("Not a number\n");
return 1;
}
else if(scanf("%d", &b)==0)
{
printf("Not a number\n");
return 1;
}
else if(scanf("%d", &c) == 0)
{
printf("Not a number\n");
return 1;
}
else
return 0;
}
int main()
{
int a;
int b;
int c;
int check = 1;
do
{
check = 0;
printf("Enter a number:");
scanf("%d",&a);
printf("Enter a number:");
scanf("%d",&b);
printf("Enter a number:");
scanf("%d",&c);
check = sanitizedInput(a,b,c);
}while(check);
}
However when I run this code, after entering three valid integers nothing shows up in the terminal and the code only terminates after entering 6 integers. (There are other functions and code in the main function, if that code is necessary to find the problem tell me and I will post it.)
Your code and your writing part is not matching.....
You should check the three numbers are valid or not firstly.
int sanitizedInput(int a, int b, int c)
{
if(a==0 || b==0 || c==0)
{
return 1;
}
else
{
printf("They are valid.....\n");
return 0;
}
}
Then if one of them are invalid, you will be able to take another three input for the returning value of 1. Because while(1) is a true condition.
remove
printf("Enter a number:");
scanf("%d",&a);
printf("Enter a number:");
scanf("%d",&b);
printf("Enter a number:");
scanf("%d",&c);
and stay with check = sanitizedInput(a,b,c);, and add printf("something\n") to the
else
return 0; block
and see what happens
In main() you are taking input for three numbers a,b,c and passing these variables as arguments for sanitizedInput().
Here, instead of checking the variables you are again using scanf() which will take new input.
if(scanf("%d", &a)==0)
The above if condition will not check the value of 'a', it will check the return value of scanf() with '0'.
if statement should be like this
if(a==0)
scanf("%d",&a);
this is same for all three variables.
In main function you are passing variables to sanitizedInput(), there you are checking variables and if not valid you are taking input again, so the variable which you changed are local to that function, which will not reflect in main(). So take care about that.
Hope this will help you.
In your while loop,you actually call scanf twice each variable(a,b,c),so you input number for 6 times.When sanitizedInput(a,b,c) finished,it return 0;so check is 0,the loop is over.I think you can do with in your main:
int main
{
int a;
int b;
int c;
int check = 0;
do
{
check = sanitizedInput(a,b,c);
printf("check = %d\n",check);
}while(!check);
return 0;
}
I want a program that can get two integers from user and put the sum of those inputs in a variable, after that checks that is sum more than 5 or not ? (I know I can do it with if , ... but I want to do it with while). I myself did it but it has some problems, would you mind saying what is the problem and how can I debug it ? Here is my code :
#include <stdio.h>
int main()
{
int ui1;
int ui2;
puts("Please enter two numbers:");
scanf("%2i", &ui1, &ui2);
int sum;
sum = ui1+ui2;
while(sum > 5) {
printf("Whats up !");
}
return 0;
}
This line is only scanning for 1 integer (%i with a 2 format, indicating only take 2 digits.):
scanf("%2i", &ui1, &ui2);
But it seems you expected to receive two integers.
This will leave the second argument, ui2, uninitialized.
(It should fill ui1 successfully, at least)
Try instead:
scanf("%i %i", &ui1, &ui2);
Try including the scanf statement into the loop, it will no longer be an infinite loop... (also need to dereference the integers, see EDIT)
#include <stdio.h>
int main()
{
int ui1;
int ui2;
puts("Please enter two numbers:\n");
//scanf("%2i", &ui1, &ui2);
int sum = 10;//(so that it will enter the loop at least once)
//sum = ui1+ui2;
while(sum > 4)
{
printf("enter number 1:\n");
scanf("%i", &ui1); //EDIT &
printf("enter number 2:\n");
scanf("%i", &ui2); //EDIT &
sum = ui1+ui2;
}
printf("result is: %d\n", sum);
getchar();//so you can see the result;
getchar();
return 0;
}
Actually while is a loop stmt not a conditional checker
if you want conditional checker use if...else series , switch etc
Note: in your code loop starts if (sum > 5) and never ends (infinate "Whats up !")
sum = ui1+ui2;
while(sum > 5) ///loop starts if (sum > 5) and never ends (infinate "Whats up !")
{
printf("Whats up !"); // (infinate "Whats up !")
}
if(sum > 5)
{
//greater stuff
}
else
{
//lower stuff
}
See Tutorial Here conditionals Stmts
You need to reset the "sum", because otherwise the while loop will be true FOREVER.
Second the input scanf is simply wrong.
Here the correct code
#include <stdio.h>
int main()
{
int ui1;
int ui2;
puts("Please enter two numbers:");
scanf("%d %d", &ui1, &ui2);
int sum;
sum = ui1+ui2;
while(sum > 4) { printf("Whats up !");
sum=0;}
return 0;
}
I'm not sure that i got what you want to do... but if you simply want to check the sum of the two integers using the while statement, you can put a break inside the while loop and everything will work :)
#include <stdio.h>
int main()
{
int ui1;
int ui2;
puts("Please enter two numbers:");
scanf("%2i", &ui1, &ui2);
int sum;
sum = ui1+ui2;
while(sum > 5) {
printf("Whats up !");
break;
}
return 0;
}
As others told you, using a if is the best solution
#include <stdio.h>
#include <process.h>
int main()
{
int check;
int enter[7];
int i,j;
printf("enter any 7 number to be stored");
for(i = 0; i < 7; i++)
scanf("%d" ,&enter[i]);
printf("\nenter any number to check:");
scanf("%d" ,&check);
for (i = 0; i < 7; i++)
{
if (enter[i]=check)
{
printf("your entry is valid");
exit(0);
}
else if(enter[6]!=check)
{
printf("your entry is not valid");
exit(0);
}
else
continue;
}
return 0;
}
this executes without error but dont work correctly .. always prints out the input is valid.... even i enter the number which is not in array :(
This is assignment, not equality:
if (enter[i]=check)
Change to:
if (enter[i] == check)
Additionally, always check the result of input operations:
if (1 != scanf("%d" ,&enter[i]))
{
/* Handle invalid value. */
}
to ensure subsequent code is operating on variables that have been assigned values.
This line
if (enter[i]=check)
does not do what you expect. You probably meant
if (enter[i]==check)
The assignment is valid C, but instead of checking for equality, it sets enter[i] equal check, and then checks the value of check for being zero. If it is non-zero, the condition succeeds, regardless of the initial value of enter[i]. If the check is zero, then the condition fails, - again, regardless of the initial value of enter[i]. This is a very common mistake; many compilers issue warnings to alert you to the situation.
= is the assignment operator, not equality at all. Doing:
if (enter[i]=check)
enter[i] will take the value check, and then it will check whether enter[i] is nonzero.
if (enter[i] == check)
enter[i]==check)// 2 for compare
proper usage of = is assignment operator whilst == is testing the equality
#include<stdio.h>
#include<process.h>
int main()
{
int check;
int enter[7];
int i,j;
printf("enter any 7 number to be stored");
for(i=0;i<7;i++)
{
scanf("%d" ,&enter[i]);
}
printf("\nenter any number to check:");
scanf("%d" ,&check);
for (i=0;i<7;i++)
{
// printf("\nvalue of i is %d\n" ,i);
if (check==enter[i])
{
printf("your entry is valid");
exit(0);
}
else if(enter[i]!=check && i==6)
{
printf("your entry is not valid");
exit(0);
}
else
continue;
}
return 0;
}
now i got it all right . thanks :)