Asking for input again with do while loop in c - c

I am trying to have the user type in a number between 1 and 8 to print out stuff on screen (height), if the number is lower than 1 or higher than 8 I would like for the program to prompt again for an input instead of it quitting out and say "invalid". I know I have to set it up with do on top and when on the bottom in C, my question is how would I go about it when I have 2 possible outcomes, i.e. too low or too high?
do {
int height = get_int(Height: ");
if (height < 1)
~ ~printf("invalid");
else if (height > 8)
~ ~printf("invalid");
} when(???????)
printf("valid")

The key is Logical OR operator (||) in logical operator.
A logical operator would :
treats its operands as boolean values
treats a non-zero value operand as true
treats a zero value operand as false
operates the boolean algebra on its operands
returns the boolean algebra result
returns 1 for true boolean result
returns 0 for false boolean result
The Logical OR operator (||) :
lhs || rhs
Here, lhs and rhs are the 2 operands of OR(||) operator
The OR operator returns 1 if any of the following conditions is true:
only lhs is true
only rhs is true
both lhs and rhs are true
In other words, the boolean algebra OR result is true if either lhs or rhs is true.
In your example, there are 2 conditions :
height is less than 1
height is larger than 8
if either of them is true, then the result is true (the loop will continue), so the expression in this example would be :
int height; /* move height outside the loop so it can be used in do-while condition */
do {
height = get_int();
if (height < 1)
printf("invalid\n");
else if (height > 8)
printf("invalid\n");
} while(height < 1 || height > 8);
printf("valid\n");
Furthermore, since we have already done the logical condition in the loop, we can just add a flag variable instead:
int flag; /* record the boolean result */
do {
int height = get_int();
if (height < 1) {
printf("invalid\n");
flag = 1;
} else if (height > 8) {
printf("invalid\n");
flag = 1;
} else {
flag = 0; /* !(height < 1 || height > 8) */
}
} while(flag);
printf("valid\n");

Try this to see if it works for you.
If not, leave a comment to tell me what you really want.
#include <stdio.h>
int main()
{
int height = 0;
while (height < 1 || height > 8) {
scanf("%d", &height);
if (height >= 1 && height <= 8) {
break;
} else {
printf("Invalid, please try again.\n");
}
}
printf("Valid.\n");
return 0;
}

A simple way of checking if the user has made an invalid choice would be to use a simple Boolean variable and set it to "false" when an invalid choice is made.
I have modified your own example code to include what I mean here:
bool valid = true;
do {
valid = true; //reset valid to true at the beginning of the loop, otherwise it will remain false when the loop runs again
int height = get_int("Height: ");
if (height < 1)
valid = false; //set valid to false when an invalid choice is made
else if (height > 8)
valid = false; //set valid to false when an invalid choice is made
} while (!valid) //if valid is not true ( or ==false), repeat. If valid is true or not equal to false, don't repeat and continue to the print statement below.
print("valid");
Please note that I used the same get_int function you provided in your post. I am not aware of this function. For ways of reading integers (for example scanf) see other answers in this thread.

I hope this helps you.
#include <stdio.h>
#include <stdlib.h>
int main() {
int num;
do {
printf("Enter 0 to quit\n");
printf("Print Enter number between 1 and 8\n");
scanf("%d",&num);
if (num > 8) {
printf("Number to high\n");
} else if (num < 0) {
printf("Number to low\n");
}
if (num == 0) {
printf("You quit the program goodby\n");
} else {
printf("Number within range\n");
}
} while(num != -0);
return 0;
}

Related

Program that inputs a number and then prints the set bits of that number with the bit integer displayed next to it

for example, if I enter 12, I want to get 81 41 as the set bits in 12 are 1100
This is what I have for now, I do not think I am implementing the for loop correctly
#include <stdio.h>
void bin(unsigned n)
{
char list[6];
int x = 0, y = 1;
/* step 1 */
if (n > 1)
bin(n / 2);
/* step 2 */
list[x] = n % 2;
x++;
/*for(int i = 0; i < x; i++) {
printf("%d\n",list[i]);
}*/
for(int i = 0; i < 5; i++) {
if(list[i] == 1 && i == 5) {
printf("32%i",y);
}
if(list[i] == 1 && i == 4) {
printf("16%i",y);
}
if(list[i] == 1 && i == 3) {
printf("8%i",y);
}
if(list[i] == 1 && i == 2) {
printf("4%i",y);
}
if(list[i] == 1 && i == 1) {
printf("2%i",y);
}
if(list[i] == 1 && i == 0) {
printf("1%i",y);
}
}
}
I checked that I was correctly storing the bytes in the array, and it outputted correctly, but when I try to look for them one at a time in a loop, it seems to get stuck on the 32 bit integer, so for 12, it would print 321 321
This program has Undefined Behaviour from accessing uninitialized values of list. I'm going to refactor this code so its easier to talk about, but know this refactored code is still incorrect.
x is always 0. y is always 1. x++ has no effect. This function can be rewritten as:
void bin(unsigned n)
{
char list[6];
if (n > 1)
bin(n / 2);
list[0] = n % 2;
for (int i = 0; i < 5; i++) {
if (list[i] == 1) {
switch (i) {
case 5: printf("321"); break;
case 4: printf("161"); break;
case 3: printf("81"); break;
case 2: printf("41"); break;
case 1: printf("21"); break;
case 0: printf("11"); break;
}
}
}
}
There are some problems here.
Firstly, list is not shared between calls to bin, nor are any other variables.
In every call to bin, only list[0] is assigned a value - all others indices contain uninitialized values. You are (un)lucky in that these values are seemingly never 1.
With your example of 12 as the starting value:
When you initially call bin(12), what happens is:
bin(12) calls bin(6), bin(6) calls bin(3), bin(3) calls bin(1).
Starting from the end and working backwards, in bin(1):
n = 1, so list[0] = n % 2; assigns 1. The loop checks each element of list for the value 1, finds it when the index (i) equals 0, and prints 11.
This is repeated in bin(3), as 3 % 2 is also 1, and again this result is assigned to the first element of list. Again, we print 11.
In bin(6), 6 % 2 is 0. The loop finds no elements of list that equal 1. Nothing is printed.
And again, this is repeated in bin(12), as 12 % 2 is 0. Nothing is printed.
To reiterate, it is pure luck that this program appears to work. Accessing list[1] through list[4] (i < 5 ensures you never access the last element) in each function call is Undefined Behaviour. It is generally not worth reasoning about a program once UB has been invoked.
When dealing with bits, it would be a good time to use some bitwise operators.
Here is a program that more-or-less does what you have described.
It assumes 32-bit unsigned (consider using fixed width types from <stdint.h> to be more precise).
This program works by repeatedly shifting the bits of our initial value to the right b number of places and testing if the rightmost bit is set.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
unsigned num = argc > 1 ? atoi(argv[1]) : 42;
unsigned b = 32;
while (b--)
if ((num >> b) & 1)
printf("%u1 ", 1 << b);
putchar('\n');
}
$ ./a.out 12
81 41

my do while loop isnt meeting both requirements

When I am trying to get the input for my variable, it is only meeting one of the requirements (ie: the < 1 requirement) and skips the other requirement even though im using the && operator.
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int x;
do {
x = get_int("what is the height of the pyramid?:");
} while (x > 0 && x < 8);
printf("%i", x);
}
I tried just using the x < 8 for the requirement but it still went through when I entered 9, 10, 11 etc.
If you want x to be between 0 and 8 (both ends exclusive), then you need to repeatedly ask for input when this condition is not satisfied.
In other words, when x is outside this range it means x is less than or equal to 0 OR greater than or equal to 8.
That said, I believe the proper input range for that problem set is actually 1-8 (both ends inclusive):
do {
x = get_int("What is the height of the pyramid?: ")
} while (x < 1 || x > 8);
The test is exactly the opposite of your intent. The do/while condition should test a condition for repeating the input and write while (!(x > 0 && x < 8)); or equivalently: while (x < 1 || x >= 8);
It is unclear what your requirements are, but it seems the number should be between 1 and 7 inclusively. If 8 should be included, the test should be modified as while (!(x > 0 && x <= 8)); or equivalently: while (x < 1 || x > 8);
do/while loops are often confusing and error prone. I suggest using a for(;;) aka for ever loop and break statements when conditions are met:
#include <cs50.h>
#include <stdio.h>
int main(void)
{
int x;
for (;;) {
x = get_int("What is the height of the pyramid? ");
if (x == INT_MAX) {
printf("Input error or end of file\n");
return 1;
}
if (x > 0 && x < 8) {
break
}
printf("The height should be between 1 and 7\n");
}
printf("%i\n", x);
return 0;
}

'if' statement in C not executing even though conditions are met

I'm a first time programmer trying to complete a simple command line program as part of the first assignment for an online course I am taking, but I seem to have hit a roadblock that I can't figure out with GDB or my own research.
After hours of rewrites, and hours of debugging, I finally got the code below to compile. The program is supposed to take a credit card number as an input, and then check whether it's valid per the specifications of the assignment. I used a test number from here: PayPal Test Credit Cards
The odd thing is, when I enter an AMEX card number, it correctly produces the text "AMEX", but when I try a Visa or a Master Card, it prints "INVALID".
In GDB I broke at the Verify function and it seems to incorrectly skip these two if/else if statements without proceeding to the Checksum function even though conditions appear to be met.
if (firstDigit == 4 && totalDigits == (13 | 16) && Checksum(cardNumber, totalDigits) == 0) // checks for a valid Visa.
...
else if (firstDigit == 5 && secondDigit == (1 | 2 | 3 | 4 | 5) && totalDigits == 16 && Checksum(cardNumber, totalDigits) == 0) // checks for a valid Mastercard.
...
The AMEX line of code that correctly executes is:
else if (firstDigit == 3 && secondDigit == (4 | 7) && totalDigits == 15 && Checksum(cardNumber, totalDigits) == 0) // checks for a valid American Express.
The arguments for all three lines seem to be formatted exactly the same. That is far as I could get in GDB though. I would print totalDigits, firstDigit, and secondDigit in GDB right before stepping through the above two non-executing lines and everything looked correct. So I'm stumped, why is the AMEX line executing, but not the others?
Thanks in advance everyone. This is the first program after hello.c that I've tried to write, so I am open to absolutely any criticism or suggestions if it looks like I'm doing something weird/wrong.
Full code:
checker.c
#include <stdio.h>
#include <stdlib.h>
int MAX = 16;
int* DigitSort(unsigned long long x, int* array);
int Verify(int* array);
int main (void)
{
int* output = malloc (sizeof(int) * (MAX + 2)); // creates a blank array for the individual digits of the card number.
unsigned long long userInput = 0;
do
{
printf("Please enter a credit card number:\n");
scanf("%lld", &userInput);
}
while (userInput <= 0); // checks to make sure the user entered a number.
switch(Verify(DigitSort(userInput, output))) // sorts the user's input into individual digits and verifies the card type and validity.
{
case 1 :
printf("VISA\n");
break;
case 2 :
printf("MASTERCARD\n");
break;
case 3 :
printf("AMEX\n");
break;
case 0 :
printf("INVALID\n");
break;
default :
printf("INVALID\n");
}
free(output);
return 0;
}
int Verify(int* array) // verifies whether or not a card number is valid. Must pass the function a sorted array of individual digits.
{
int* cardNumber = array;
int firstDigit = cardNumber[0];
int secondDigit = cardNumber[1];
int totalDigits = 0;
int Checksum(int* cardNumber, int totalDigits);
int i = 0;
while (firstDigit >= 1 && cardNumber[i] >= 0) // this step counts the number of digits in the array.
{
totalDigits = totalDigits + 1;
i++;
}
if (firstDigit == 4 && totalDigits == (13 | 16) && Checksum(cardNumber, totalDigits) == 0) // checks for a valid Visa.
{
return 1;
}
else if (firstDigit == 5 && secondDigit == (1 | 2 | 3 | 4 | 5) && totalDigits == 16 && Checksum(cardNumber, totalDigits) == 0) // checks for a valid Mastercard.
{
return 2;
}
else if (firstDigit == 3 && secondDigit == (4 | 7) && totalDigits == 15 && Checksum(cardNumber, totalDigits) == 0) // checks for a valid American Express.
{
return 3;
}
else // if the card number doesn't match any of the above conditions or fails the checksum, an 'I' for Invalid is returned.
{
return 0;
}
}
int* DigitSort(unsigned long long x, int* array) // takes a long long as input and sorts it into individual digits
{
int* arrayReversed = malloc (sizeof(int) * (MAX + 2)); // creates a new array to hold the reversed order of digits.
int i = 0;
arrayReversed[0] = 0;
if (i < (MAX - 1) && x >= 10)
{
do
{
arrayReversed[i] = x % 10;
x = x / 10;
i++;
}
while (i < (MAX -1) && x >= 10);
}
if (i < MAX && x >= 1 && x <= 9)
{
arrayReversed[i] = (int) x;
x = (x - x);
}
if (x == 0)
{
int j = 0;
do
{
array[j] = arrayReversed[i]; // sorts the digits from the reversed array and places them into the sorted array.
j++;
i--;
}
while (j < MAX && i >= 0);
array[j] = -1;
}
free(arrayReversed);
return array;
}
int Checksum(int* cardNumber, int totalDigits)
{
int sum1 = 0;
int sum2 = 0;
int i = (totalDigits - 2);
int j = (totalDigits - 1);
while (i >= 0)
{
sum1 = ((cardNumber[i] * 2)%10) + ((cardNumber[i] * 2)/10) + sum1;
i -= 2;
}
while (j >= 0)
{
sum2 = (cardNumber[j] + sum2);
j -= 2;
}
if (((sum1 + sum2) % 10) == 0)
{
return 0;
}
else
{
return 1;
}
}
Your first problem is here:
if (firstDigit == 4 && totalDigits == (13 | 16) && ...
You need to write:
if (firstDigit == 4 && (totalDigits == 13 || totalDigits == 16) && ...
Your first check is looking for 0x1D == 29 as the number of digits (because, as paisanco points out in a comment, the | operator is the bitwise OR operator), and no credit card needs 29 digits (yet, and not for a long time to come). Note the extra parentheses for clarity and accuracy. Don't mess around risking removing them — the code won't work properly again. And in general, be explicit if your condition has both && and || operators and use parentheses to group terms explicitly.
You have similar problems elsewhere. As it happens, (4 | 7) is the same value as 7, so the condition works when the second digit is 7 (but not when it is 4). But it doesn't mean what you intended it to mean.
Computer languages don't work the same as human languages. Get used to writing out the condition somewhat more verbosely. Some other languages provide shorthands for these conditions; C is not such a language.

checking every digit in a number for oddness

I was writing a function which checks if every digit in a number is odd. I came accross this weird behaviour. Why does the second function return different (incorrect) results, eventhough its basically the same? (implemented in an opposite way)
#include <stdio.h>
int all_odd_1(int n) {
if (n == 0) return 0;
if (n < 0) n = -n;
while (n > 0) {
if (n&1 == 1)
n /= 10;
else
return 0;
}
return 1;
}
int all_odd_2(int n) {
if (n == 0) return 0;
if (n < 0) n = -n;
while (n > 0) {
if (n&1 == 0)
return 0;
else
n /= 10;
}
return 1;
}
int main() {
printf("all_odd_1\n");
printf("%d\n", all_odd_1(-131));
printf("%d\n", all_odd_1(121));
printf("%d\n", all_odd_1(2242));
printf("-----------------\n");
printf("all_odd_2\n");
printf("%d\n", all_odd_2(131));
printf("%d\n", all_odd_2(121));
printf("%d\n", all_odd_2(2242));
return 0;
}
warning: suggest parentheses around comparison in operand of '&'
Well, how about adding them ? Change n&1 to (n&1). Always ask for warnings.
The == operator has higher precedence than the & operator, so your if (n&1 == 0) statement is not doing what you expect!
(and the if (n&1 == 1) statement works only by coincidence that 1 == 1 evaluates to 1 ;)
Operator precedence. n & 1 == 0 is equivalent to n & (1 == 0)
It is a problem related to the order of execution. Try to use if ((n&1) == 0) in all_odd_2 and everything will work.

Having trouble with a Collatz Conjecture test in C

I am trying to create a code that will take the number 2 to 100, and test each for the collatz conjecture.
The goal is that for each number, if it is even, divide it by two, and if it is odd, then multiply it by 3 and add 1.
It prints each step, and each number should stop testing if it reaches 1. Why doesn't it work?
#include <stdio.h>
int main()
{
int number, position;
position == 2;
number == 2;
while (position <= 100)
{
while (number != 1)
{
if (number % 2 == 0)
{
number = number/2;
printf("%d\n", number);
}
else if (number % 2 != 0)
{
number = number*3;
number = number + 1;
printf("%d\n", number);
}
}
position = position + 1;
number = position;
}
}
It prints recurring Os
Fix the == vs =:
position = 2;
number = 2;
Also, the else if is unnecessary. The opposite of even is odd, so a plain else will suffice :-)
You have set position and number with a double equal == (Comparision Operator) instead of using single equal = (Assignment Operator) so that the algorithm is comparing them instead of assigning a value.
The assignment should look like this:
position = 2;
number = 2;
Also you can do it when you first define them:
int number=2, position=2;
Apart from that the code is correct, the only thing to highlight is that you don´t need to use else if because it can just be even or odd so a single else would be enough.
Hope I´ve helped :-)

Resources