console exits loop with incorrect value in C - c

I have a function exits the loop when INCORRECT value is entered.
I want the function to ask the user to enter the correct value BEFORE it exits the loop.
I am passing the value of hours entered by the user into the function wrongHours
if the user is to enter hours below 0 or above 23, the function should ask the user to enter something again, but it just goes to the next line? am I missing a flag or something?
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <conio.h>
// function prototypes
int wrongHours(int n);
int main(void)
{
int hours;
int minutes;
printf("Enter the first hour value (0 - 23): ");
scanf("%d", &hours);
while (wrongHours(hours)) {
printf("Enter the first hour value (0 - 23): ");
scanf("%d", &hours);
}
printf("\nEnter the first minute value (0 - 59): ");
scanf("%d", &minutes);
_getch();
return 0;
} // end main
// function validates that hours are between 0 - 23
int wrongHours(int n) {
int i;
if (n < 0 || n > 23) {
printf("Invalid data entered, try again!");
return 0;
}
return 1;
}

I dont know why you want to use for loop , below is some formatted code , please check if it satisfy your need
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <conio.h>
// function prototypes
int wrongHours(int n);
int main(void)
{
int hours;
int minutes;
printf("Enter the first hour value (0 - 23): ");
scanf("%d", &hours);
while( wrongHours(hours)) {
printf("Enter the first hour value (0 - 23): ");
scanf("%d", &hours);
}
printf("\nEnter the first minute value (0 - 59): ");
scanf("%d", &minutes);
_getch();
return 0;
} // end main
// function validates that hours are between 0 - 23
int wrongHours(int n)
{
if (n < 0 || n > 23) {
printf("Invalid data entered, try again!");
return 1;
}
return 0;
}

Related

Fixing do-while loop problem and how to add do you want to play again?

I started C courses today and teacher assigned to do:
Guess the number and if the number is higher say it is higher but if it is lower say it is lower and count every time you guess AND if you guess it ten times already then say do you want to try again?
I don't know why my code is stop when I just play it only 1 time and how to do the "do you want to play again?"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int x;
char name[20], result;
int count = 0;
int number;
srand(time(NULL));
x = rand() % 100 + 1;
printf("What's your name :");
scanf("%s", &name);
printf("Hello!! %s\n", name);
printf("Guess the number : ");
scanf(" %d", &number);
do {
count++;
if (x > number) {
printf("This is your count : %d\n",count);
printf("The number is higher\n");
} else
if (x < number) {
printf("This is your count : %d\n",count);
printf("The number is lower\n");
} else
if (x == number) {
printf("\nYou're right!!, the number is %d",x);
}
} while (count == 10);
}
The code allows only 1 try because the test while (count == 10) is false at the end of the first iteration. You should have while (count < 10).
You should move the input call scanf(" %d", &number); inside the loop.
Also note that you should break from the loop if the number was found.
For the Do you want to play again? part, you could wrap this code in another loop.
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
char name[20] = "", ch;
srand(time(NULL));
printf("What's your name: ");
scanf("%19s", &name);
printf("Hello!! %s\n", name);
for (;;) {
int x = rand() % 100 + 1;
int number;
for (int count = 1; count <= 10; count++) {
printf("Enter your guess: ");
if (scanf(" %d", &number) != 1)
break;
if (x > number) {
printf("This is your count: %d\n",count);
printf("The number is higher\n");
} else
if (x < number) {
printf("This is your count: %d\n",count);
printf("The number is lower\n");
} else
if (x == number) {
printf("You're right!!, the number is %d\n",x);
break;
}
}
printf("Do you want to play again? ");
if (scanf(" %c", &ch) != 1)
break;
if (ch != 'y' && ch != 'Y')
break;
}
return 0;
}
The loop condition in the line
} while (count == 10);
is wrong, because it will be false after the first loop iteration. It would be more meaningful to write
} while (count < 10);
Also, you probably want to put the lines
printf("Guess the number : ");
scanf(" %d", &number);
inside the loop, so that they get executed more than once. Otherwise, you will be processing the same user guess 10 times, which is not what you want.
Another issue is that you don't want the loop to always run 10 times. You only want it to run 10 times if the user hasn't guessed the number. If the user has guessed the number, you want to break out of the loop immediately, without waiting for count to reach 10. For this, you can use the break statement.
I'm a VB guy mostly but can you use while count <= 10?
Perhaps Do Until?

"Guess The Number" In C But If Statements Don't Work Right

I have a program in C that the user runs to play a "Guess The Number" game. It runs correctly to start but after the user enters 2 numbers (1 for the initial and 1 for the try again) the program repeats when it is supposed to have a limited number of tries.
Here is my code for the program:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
//----Guess a Number Game.-----------------------------------------
// srand and rand needs the #include <stdlib.h> library
srand(time(NULL)); //seeding the guess a number game with the system time, so the guess a # game always starts at a different point.
int guess;
int correctnum;
correctnum = rand();
printf("Enter a number:");
scanf("%i",&guess);
if(guess>correctnum) // If aa is greater than bb AND aa is greater than cc.
{
printf("Please enter another number, lower this time!");
scanf("%i",&guess);
main();
}
else if (guess<correctnum)
{
printf("Please enter another number, higher this time!");
scanf("%i",&guess);
main();
}
else if (guess==correctnum)
{
printf("You are a WINNER!\n");
printf("You guessed the number right and it was %i!\n",correctnum);
}
int repeat;
printf("Would you like to play again? 1=Yes and 2=No.");
scanf("%i",&repeat);
if(repeat==1)
{
main();
}
if(repeat==2)
{
printf("Hope you had a good time playing the game! See you soon!\n");
return 0;
}
}
Don't call main() recursively. What you need is a simple loop. Something like:
int main(void) {
srand(time(NULL));
int correctnum = rand();
int guess;
int done = 0;
while (! done) {
printf("Enter a number:");
scanf("%i",&guess);
if (guess>correctnum) {
printf("Please enter another number, lower this time!");
} else if (guess<correctnum) {
printf("Please enter another number, higher this time!");
} else /* if (guess==correctnum) */ {
printf("You are a WINNER!\n");
printf("You guessed the number right and it was %i!\n",correctnum);
done = 1;
}
}
}
You should probably check for errors from scanf() too, but first things first.
you can implement all functions you want in a loop:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void main(){
srand(time(NULL));
int guess;
int correctnum;
correctnum = rand() % 100; #it's better to get a number between 1~100
int done = 1;
printf("guess the number:\t");
scanf("%i", &guess);
while(done){
if(guess < correctnum){
printf("Please enter another number, higher this time:\t");
scanf("%i", &guess);
}
else if(guess > correctnum){
printf("Please enter another number, lower this time:\t");
scanf("%i", &guess);
}
else if(guess == correctnum){
printf("well done! if you want to play again, input 1, else input 0:\t");
scanf("%i", &done);
if(done == 1){
correctnum = rand() % 100;
printf("guess the number:\t");
scanf("%i", &guess);
}
}
}
printf("Hope you had a good time playing the game! See you soon!\n");
}

Two questions, why the double print and how to start it over again! C programming

I'm writing the simplest game in C - Number guessing game. The game itself works good. Yaay me. The problem is that I don't know how to start it over. See code below:
int main()
{
int number, innum, times = 0;
char playAgain;
srand((unsigned)time(NULL));
number = 5;//rand() % 1000;
for(;;)
{
while(innum != number)
{
printf("Enter a number: ");
scanf("%d", &innum);
if(innum > number)
printf("The entered number is too big!\n");
if(innum < number)
printf("The entered number is too small!\n");
times++;
if(innum == number)
{
printf("Congrats you guessed right!\n");
printf("It took you %d tries\n", times);
}
}
printf("Do you want to play again?");
scanf("%c", &playAgain);
if(playAgain == 'n')
break;
}
return 0;
}
The first problem is that it prints "Do you want to play again?" two times. Why is that? And the other problem is, how do I get the game to start again?
Thanks in advance.
This should work for you:
(What i did? Added a space by the scanf and put the declaration of number, times and innum in the for loop)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int number, innum, times;
char playAgain;
srand((unsigned)time(NULL));
for(;;) {
/*Declare the variables here*/
number = 5; //rand() % 1000;
innum = 0;
times = 0;
while(innum != number) {
printf("Enter a number: ");
scanf("%d", &innum);
if(innum > number)
printf("The entered number is too big!\n");
if(innum < number)
printf("The entered number is too small!\n");
times++;
if(innum == number) {
printf("Congrats you guessed right!\n");
printf("It took you %d tries\n", times);
}
}
printf("Do you want to play again?");
scanf(" %c", &playAgain);
//^Added space here to 'eat' any new line in the buffer
if(playAgain == 'n')
break;
}
return 0;
}
possible output:
Enter a number: 2
The entered number is too small!
Enter a number: 6
The entered number is too big!
Enter a number: 5
Congrats you guessed right!
It took you 3 tries
Do you want to play again?y
Enter a number: 3
The entered number is too small!
Enter a number: 5
Congrats you guessed right!
It took you 2 tries
Do you want to play again?n

While Loop Understanding

I'm having a problem and I tried several times to solve this problem maybe you could help.
I need to modify the program below so that it asks the user to enter any number other than the number equal to the number of times they've been asked to enter a number. (i.e on the first iteration "Please enter any number other than 0" and on the second iteration "Please enter any number other than 1"m etc. etc. The program must behave accordingly exiting when the user enters the number they were asked not to.) Now the code that I have below reacts a bit differently and here's what's happening when I run it:
Enter a number: 4
Please enter a number other than 4
5
Enter a number other than 5
5
wrong
6
Enter a number other than 6
7
Enter a number other than 6
and this is my code below:
#include <stdio.h>
int main()
{
int number, x=0, counter = 0;
printf("Enter a number: ");
scanf("%d", &number);
printf("Please enter a number other than %d\n", number);
while (number!=x)
{
scanf("%d", &x);
while (x!=counter)
{
printf("Enter a number other than %d\n", x);
scanf("%d", &counter);
if (counter==x)
{
printf("wrong\n");
break;
}
}
if (number==x)
{
printf("wrong\n");
break;
}
}
return 0;
}
I really hope I explained the question correctly please let me know.
If I understood it well, you want a program that behaves like this, isn't it?
Please enter a number other than 0
4
Please enter a number other than 1
7
Please enter a number other than 2
8
Please enter a number other than 3
2
Please enter a number other than 4
4
** END OF PROGRAM **
Then, it's much more simpler than you thought...
#include <stdio.h>
int main()
{
int number, counter = 0;
do
{
printf("Please enter a number other than %d\n", counter);
scanf("%d", &number);
}
while (number != counter++);
return 0;
}
UPDATE:
#include <stdio.h>
int main()
{
int number = 0, previous;
do
{
previous = number;
printf("Please enter a number other than %d\n", previous);
scanf("%d", &number);
}
while (number != previous);
return 0;
}
Try:
#include <stdio.h>
int main()
{
int number, x=0, counter = 0;
int flag = 0;
printf("Enter a number: ");
scanf("%d", &number);
printf("Please enter a number other than %d\n", number);
while (number!=x)
{
scanf("%d", &x);
while (x!=counter)
{
printf("Enter a number other than %d\n", x);
scanf("%d", &counter);
if (counter==x)
{
printf("wrong\n");
flag = 1;
break;
}
}
if (number==x || flag=1)
{
printf("wrong\n");
break;
}
}
return 0;
}

Why won't my program stop looping?

My assignment is to use two different kinds of loops (For, While, do While). The task is to ask the user to enter a number between 1 and 10 and then the program will count from 0 to the users number. Also, the program must be able to display an error message and ask the user to enter a number again if the user enters a number outside of 1 through 10. The part of the code with the error message and the prompt to enter a number again is working just fine. However, when I enter a number within the range, it either does nothing or counts from 0 to their number an infinite amount of times and won't stop looping the count. Please help!
#include <stdio.h>
int main(void)
{
//Variables
int num;
int zero;
//Explains to the user what the program will do
printf("This program will count from 0 to a number you pick.\n\n");
//Asks the user to input a value
printf("Please enter a number (between 1 and 10): \n");
scanf("%d", &num);
//If the correct range was selected by the user
while ( num >= 1 && num <= 10 )
{
for ( zero = 0; zero <= num; zero++ )
{
printf("%d...", zero);
}
}
//If a value outside of the accepted range is entered
while ( num < 1 || num > 10)
{
printf("I'm sorry, that is incorrect.\n");
printf("Please enter a number (between 1 and 10): \n");
scanf("%d", &num);
}
printf("\n\n\n");
return 0;
}
while ( num >= 1 && num <= 10 )
{
for ( zero = 0; zero <= num; zero++ )
{
printf("%d...", zero);
}
}
will run forever if num is between 1 and 10, since num is not changed inside the loop - once you're in, you're in for good.
If you enter a "bad" value then you'll skip this and go into your second while loop. However once you get out of that while loop by entering a "good" value, all that's left to execute is
printf("\n\n\n");
return 0;
You need to get rid of the first while loop and move the second one above the for loop:
#include <stdio.h>
int main(void)
{
//Variables
int num;
int zero;
//Explains to the user what the program will do
printf("This program will count from 0 to a number you pick.\n\n");
//Asks the user to input a value
printf("Please enter a number (between 1 and 10): \n");
scanf("%d", &num);
//If a value outside of the accepted range is entered
while ( num < 1 || num > 10)
{
printf("I'm sorry, that is incorrect.\n");
printf("Please enter a number (between 1 and 10): \n");
scanf("%d", &num);
}
for ( zero = 0; zero <= num; zero++ )
{
printf("%d...", zero);
}
printf("\n\n\n");
return 0;
}
Change two of your while loops to if guards,
num=1;
while(num>0)
{
//Asks the user to input a value
printf("Please enter a number (between 1 and 10): \n");
scanf("%d", &num);
//If the correct range was selected by the user
if ( num >= 1 && num <= 10 )
{
for ( zero = 0; zero <= num; zero++ )
{
printf("%d...", zero);
}
}
//If a value outside of the accepted range is entered
if ( num < 1 || num > 10)
{
printf("I'm sorry, that is incorrect.\n");
printf("Please enter a number (between 1 and 10): \n");
//scanf("%d", &num);
}
while(0); while(0); while(0); while(0); //gratuitous loops
do ; while(0); for(;0;);
}
Really Simple!
You may use a break inside the while loop:
#include <stdio.h>
int main(void)
{
//Variables
int num;
int zero;
//Explains to the user what the program will do
printf("This program will count from 0 to a number you pick.\n\n");
//Asks the user to input a value
printf("Please enter a number (between 1 and 10): \n");
scanf("%d", &num);
//If the correct range was selected by the user
while ( num >= 1 && num <= 10 )
{
for ( zero = 0; zero <= num; zero++ )
{
printf("%d...", zero);
}
break; // !!! Here !!!
}
//If a value outside of the accepted range is entered
while ( num < 1 || num > 10)
{
printf("I'm sorry, that is incorrect.\n");
printf("Please enter a number (between 1 and 10): \n");
scanf("%d", &num);
}
printf("\n\n\n");
return 0;
}

Resources