So this a three part problem and I believe I have 2 parts working but when I changed logic statement in loops I end up with problems, I have read and reread every book and web post and still I have some sort of disconnect in my brain with loops and logic statements. Here is what I wrote the last one is not working, it works after I compile it and then stops following the 3 count and at this point I have confused myself and started just moving things around until I got it to stay open as it kept exiting with any key input and wouldn't enter loop at all.
For loop takes user input and controls the number of times a user can enter numbers, if statement tests number for a multiple of for and has a break to exit if true.
Code:
/*HW3Q1a.c program that ask the user to enter a number that is positive and
a multiple of 4. The user has three attempts to find such a number.*/
#include<stdio.h>
int main(void)
{
//declare variables
int user_num;//input form user
int counter=0;//counter to stop program after 3 tries
printf("Enter a number that is positive and a multiple of 4 you have 3 attempts\n");
for (counter = counter+1;counter <= 3; counter++)
{
scanf("%d", &user_num);
printf("\n");
if(user_num<0)
{
printf("please enter a positive number\n");
break;
}
else if(user_num % 4 == 0)
{
printf("%d. Input: %d\nCorrect! It took you %d attempts!\n",counter,user_num,counter);
break;
}
else
{
printf("%d. Input: %d\nWrong! Try again!\n",counter,user_num);
}
}
system("PAUSE");
return 0;
}
This one is the same except it uses a while loop and a ! (not) in statement, program loops until counter hits 3 or a multiple of 4 is entered.
Code:
/*HW3Q1b.c program that ask the user to enter a number that is positive and
a multiple of 4. The user has three attempts to find such a number.*/
#include<stdio.h>
int main(void)
{
//declare variables
int user_num=-1;//input form user
int counter=0;//counter to stop program after 3 tries
printf("Enter a number that is positive and a multiple of 4 you have 3 attempts\n");
while(!(counter >= 3 || user_num %4 == 0))//loop stops when counter hits 3
{
scanf("%d", &user_num);
counter++;
printf("\n");
if(user_num < 0)
{
printf("Only positive numbers, Try again\n");
break;
}
else
{
if(user_num%4==0)
{
printf("%d. Input: %d\nCorrect! It took you %d attempts!\n",counter,user_num,counter);
}
else
{
printf("%d. Input: %d\nWrong! Try again!\n",counter,user_num);
}
}
}
system("PAUSE");
return 0;
}
this one is the same except I couldn't use a global !(not) but I wanted to do an inverse statement of the last, I can use a ! but it can't be global. I can not get both conditions to work together. change
Code:
/*HW3Q1c.c program that ask the user to enter a number that is positive and
a multiple of 4. The user has three attempts to find such a number.*/
#include<stdio.h>
int main(void)
{
//declare variables
int user_num=1;//input form user
int counter=0;//counter to keep track of user attempts
printf("Enter a number that is positive and a multiple of 4 you have 3 attempts\n");
while((counter < 3) && (user_num %4 == 1))//loop stops when counter hits 3
{
scanf("%d", &user_num);
counter++;
printf("\n");
if(user_num < 0)
{
printf("Only positive numbers, Try again\n");
break;
}
else
{
if(user_num%4==0)
{
printf("%d. Input: %d\nCorrect! It took you %d attempts!\n",counter,user_num,counter);
}
else
{
printf("%d. Input: %d\nWrong! Try again!\n",counter,user_num);
}
}
}
system("PAUSE");
return 0;
}
Thank You for any help
marie