Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
The user input cannot put in int that is 0. Or anything above 25. If they do, I need an error message and to repeat the question again.

I would need to put in something like this. But I don't know how, or where to put it. I keep getting an error.

if (number1 < 0); 
printf("Must choose a number greater than 0 or below 25"); 

else (number1 > 25); 
printf("Must choose a number greater than 0 or below 25");

-------------------------------------------------------------------------

int main(void)

{


int number1, number2; int sum;

printf("Enter the first integer to add: "); scanf("%d",&number1);

printf("Enter the second integer to add: "); scanf("%d",&number2);

sum = number1 + number2;

printf("Sum of %d and %d = %d \n",number1, number2, sum);

return 0; }


What I have tried:

I have tried putting the code in directly below the question. And after the the sum =

Both give me errors. I also need to loop it so it will ask the same question.
Posted
Updated 10-Nov-21 4:20am
v4

Write a function that accepts three parameters: the minimum, the maximum, and an error message, and which returns an integer.

In the function, use a while loop to go around forever.
Inside the loop, prompt the user to enter a number between the min and max.
Read the response.
If it's not a number, tel him so, and go around again.
If it's below the min or above the max, show him the error message, and go around again.
If it's valid, return it.

Easy to do, easy to call. But this is your homework, so the actual code is up to you!
 
Share this answer
 
Comments
OriginalGriff 8-Nov-21 8:16am    
They are really easy:
while ( ...  condition is true ... )
   {
   ... do statements here ...
   }
... when it's false, do statements here ...
For example:
int i = 5;
while ( i > 0 )
   {
   printf("i is now %u\n", i--);
   }
printf("i is now zero\n");
CPallini 10-Nov-21 12:26pm    
5.
Your if and else clauses are both incorrect:
C++
// The semi-colon at the end maens 'do nothing'.
// Also, you are testing less than zero, but it should be less than or equal
if (number1 < 0); 
printf("Must choose a number greater than 0 or below 25"); 

// The same here, and also because you have an expression you need 'else if'
else (number1 > 25); 
printf("Must choose a number greater than 0 or below 25");

So the correct code would be:

C++
if (number1 <= 0)
{ 
    printf("Please choose a number greater than 0 and below 25"); 
}
else if (number1 > 25)
{
    printf("The number must be less than 25");
}

or perhaps a simpler version:
C++
if (number1 <= 0 || number1 > 25)
{ 
    printf("Please choose a number greater than 0 and less than 25");
}

Note, it is a good idea always to use curly braces around blocks of code following boolean expressions. Even though the block is a single line, it is a good habit to cultivate.
 
Share this answer
 
v2
Comments
Richard MacCutchan 8-Nov-21 8:01am    
Loops are fairly simple, see 7.9 — For statements – Learn C++[^]. Although it is a C++ reference site, loops in plain C operate the same.
OriginalGriff 8-Nov-21 7:58am    
if (number1 <= 0 || > 25)
{ 
    printf("Please choose a number greater than 0 and less than 25");
}
Are you sure? That doesn't compile in any flavour of C I ever used ...
Richard MacCutchan 8-Nov-21 8:04am    
Ha ha, seeing that too many times here in QA. :(
OriginalGriff 8-Nov-21 8:17am    
:DIt rubs off on you, doesn't it?
mondo2 9-Nov-21 8:28am    
del

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900