Using Do While in C [Find Mistake] [closed] - c

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 7 years ago.
Improve this question
I have a relatively simple program that asks for user input between a range and then checks it using Do - while loop.
int n;
do
{
print ("hello, world\n");
n = getvalue() //just making this syntax up.It takes value from user
}
while (n<0 && n>99);
print ("%d\n",n);
I want to prompt the user to input another value if he/she enters either a negative number or a triple digit number (or higher). But the condition within while is not being validated.
What the output looks like:
No matter what number I enter, it get printed. i.e. -2 is printed as -2, 101 as 101 and 50 as 50. Ideally, -2 should prompt user to enter a number again, so should 101 and only 50 should print out.

I think the problem is your rather cryptic condition of n < 0 && n > 99, which can't reasonably be satisfied.
You need to look more closely at the logic of your loop. If you want to prompt the user again if they enter a number less than zero or greater than 99, you need to use the logical or operator ||.
while (n < 0 || n > 99);

Related

How am I accidentally filtering my input? [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 3 months ago.
Improve this question
int velikostVstupu=0;
while(scanf("%d", vstup+velikostVstupu)!=EOF){
velikostVstupu++;
if(velikostVstupu>2000){
printf( "Nespravny vstup.\n");
return 0;
}
}
This code is supposed to input no more than 2000 int values into my array "vstup[2000]". But nowhere do I check if the input is int, yet if it isn't, it succeeds my "if(velikostVstupu>2000)" (that's where I check if I am not over 2000).
Why is that? What is the math behind that?
scanf() may fail to read a number and return 0. Unless the input buffer changes it will do so forever till you reach your count limit. Each of arrays will be undefined after that point. You haven't told us how what that input looks like to handle the "skip non-numbers".
You overflow the array when you read entry with vstup == 2000 as the test is after you already read in the value (undefined behavior). This is how I would write it:
#include <stdio.h>
#define MAX_VSTUP 2000
int main(void) {
int vstup[MAX_VSTUP];
size_t velikostVstupu=0;
for(; velikostVstupu < sizeof vstup / sizeof *vstup; velikostVstupu++) {
if(scanf("%d", vstup+velikostVstupu) != 1)
break;
}
printf("Nespravny vstup %zu.\n", velikostVstupu);
}
and here are some example runs:
$ seq 1 | ./a.out
Nespravny vstup 1.
$ seq 2000 | ./a.out
Nespravny vstup 2000.
$ seq 2001 | ./a.out
Nespravny vstup 2000.
Thanks to #JonathanLeffler i think i know what is happening when inputing non number. it reads only until non number leaving the rest in buffer. When the next one asks it ignores white spaceses and he instantly finds a non number taking nothing and leaving buffer the same but incementing velikostVstupu. And this goes on until velikostVstupu>2000.

If Statement doesn't even seem to exist [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
Im trying to write a program that takes array inputs from the user and assigns the the odd elements to the first index until there are no more odd elements and the even elements are assigned to the end of the array until they're done so for example
Assuming this a size 10 array,The user enteres 1 for the first element and 2 for the second element and 3 for the third element ,so the final array would have indices 0 and 1 to have the values of 1 and 3 and indice 10 to have the value of 2 and so on and so forth,and here's my code
int main() {
int array1[31];
int array2[31];
for(int i=0;i<31;i++) {
if(scanf("%d",&array1[i])%2==0) {
array2[31-i]=array1[i];
}
else {
array2[i]=array1[i]
}
}
for (int i=0;i<31;i++) {
printf ("%d\t",array2[i]);
}
return 0;
}
But this code is only printing exactly what the user has entered in the exact same order,it's like my if condition doesn't execute and im not sure why,I'm still a beginner in C so I apologize if this problem is too trivial,but yeah any help is appreciated
First of all, learn that scanf() does not return the matched value, it returns the number of items matched. You should
Check the scanf() return value against the number of expected matches (1 in this case)
If scanning is success, use the supplied argument to check the scanned value.
If scanning is failure, clean up the input buffer and ask for input again.
That said, in the code, when you start the loop from i value of 0, you cannot use
array2[31-i]=array1[i];
as, for i value of 0, it will be like
array2[31]=array1[i];
which is off-by-one, you need to use
array2[31-i-1]=array1[i];
So that your array indexes are [0,31).

What's the propper use of && operator in a do while loop? [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 3 years ago.
Improve this question
I could not understand why the program defines Heigth only for greater values then 9, I would be very pleased if someone can show me my mistake, thank you for your time.
#include <cs50.h>
include <stdio.h>
int Heigth;
int main(void) {
do {
Heigth = get_int("A level value please ? ");
} while (9 > Heigth && Heigth > 0);
printf("Heigth is %d", Heigth);
}
OUTPUT:
$ ./mario
A level value please ? 5
A level value please ? 7
A level value please ? 98
Heigth is 98$ ^C
The code fragment:
do {
something();
} while (condition1 && condition2);
means to repeat something() if both condition1 and condition2 are true. In your case, get_int() will be called repeatedly if Height is between 9 and 0. That is, both 9 > Height and Height > 0 are true.
If you actually meant to repeat calling get_int if Height was greater than 8 or less than 1, then you have to write the condition to say that. But, then you would not use &&. You would write 8 < Height || Height < 1 instead.
In boolean logic, it is true that ¬(A ∧ B) ≡ (¬A ∨ ¬B), so you could get the equivalent behavior by negating your original expression.
do {
something();
} while (!(condition1 && condition2));
The use of && in any conditioned cycle is to join conditions.
In your program, you specify that while both 9 > Heigth and Heigth > 0 conditions are met, the cycle will repeat itself.
9 > Heigth is not really the usual format, normally the variable comes before the literal so it would be preferable to have Heigh < 9.
So to translate it to text this means that the cycle will repeat itself while the inputed number is less than 9 and also more than 0, so values that range from 2 to 8, including.
If a number that doesn't meet the condition is inputed the cycle will end and the program will continue to the next instruction, in this case it will exit.

int inequality as while condition not working in C [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
Im creating a program that asks the user for an integer and checks if it is between two values and and if not, asks the user to try again. Im currently having trouble with the condition for the while loop. I want the program to check if the input number from the user is between 28 and 31 but when i put the inequality in, it acts as if its completely skipping the while loop. statement on line 8
Thanks in advance for the help everyone, I've just started learning and want to learn as much as possible. here is the code in text form instead of image form:
printf("Enter days in the month (between 28 & 31): ");
int d = get_int();
while ( d<28 && d>31 )
{
printf("error");
}
printf("how many pennies on the first day? ");
float p = get_float();
You are having a wrong logical statement in your while loop condition.
It should be d<28 || d>31. You are falsely using the and operator instead of or operator.
I think what you are to do with the while loop is not what the code is actually doing. Based on your description, you want to keep prompting the user to enter number of days until they enter the correct value. However, what your code is currently doing is asking once and then throwing error within the while loop. Perhaps try something like this
int d;
do {
printf("Enter days in the month (between 28 & 31): ");
d = get_int();
}
while ( d<28 || d>31 );
First we declare the variable d.
Then we start a do while loop that will keep running the code within the loop (asking the user to input days in month). If user enters a value that is less than 28 OR greater than 31, the while loop resets and asks the user to enter the value again. If the user enters a valid value, the loop exists and moves to next question.

display that precedes the number entered. (C programming) [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 7 years ago.
Improve this question
C Programming
Ask user to enter a random number between 1 and 100. Then ask how many numbers he wants to display that precedes first number he enters.
Let’s say if user enter 9 and wants 3 numbers that precedes 9, your program should display this:
• 6 7 8 9
Have no idea need some help.
For asking the user a question, you can use something like printf or puts.
To request numbers from the user, scanf is probably the best approach for the level you're working at.
By way of example, here's a complete program that asks the user for a number then gives them the next number:
#include <stdio.h>
int main (void) {
int num;
printf ("Enter a number: ");
if (scanf ("%d", &num) != 1) {
puts ("That wasn't a valid number");
return 1;
}
printf ("The next number is %d\n", num + 1);
return 0;
}
Analysing that code, and what it does when it runs, should be enough to get you started.
For your specific project, the following pseudo-code should help:
print "Enter the ending number: "
input endnum
print "Enter the count of preceding numbers: "
input count
num = endnum - count
do:
print num
num = num + 1
while num <= endnum
That's the algorithm you can use, I won't provide it as C code since you'll become a better coder if you do that yourself. In any case, those pseudo-code lines all pretty much have a one-to-one mapping with C statements so it should be relatively easy to get something going.

Resources