C++ Code Problem I was doing a lab for school and was doing it a way the teacher
ID: 3677011 • Letter: C
Question
C++ Code Problem
I was doing a lab for school and was doing it a way the teacher didn't like it. He changed parts of my code. So now I can't get it to work. Can someone please help.
Its suppose to ask the user to enter a positive (above zero) odd number between 0 and 40. Then if the person enter a number that dosn't work three times then the problem will stop running. Heres the code that was changed.
#include <iostream>
using std::cin; using std::cout; using std::endl;
int i = 0;
int checkpos;
int checksize;
int check;
int main(){
while (i < 3)
{
cout << "Please input a positve odd number lower then 40: " << endl;
int number;
cin >> number;
int check = number % 2;
bool checkpos = number > 0;
bool checksize = number < 40;
if (check != 0 && checkpos && checksize)
cout << "Thank You!" << endl;
else {
cout << "Incorrect entry. Please try again. " << endl
<< endl << endl;
i++;
}
}
if (i = 2){
cout << "Too many Tries.";
}
if (check == 0){
cout << "Your number is even " << endl;
}
if (!checkpos){
cout << "Your number is negative" << endl;
}
if (!checksize){
cout << "Your Number is too big!" << endl;
}
}
Explanation / Answer
#include <iostream>
using std::cin; using std::cout; using std::endl;
int main(){
int i = 0;
bool checkpos = false;
bool checksize = false;
int check;
while (i < 3)
{
cout << "Please input a positve odd number lower then 40: " << endl;
int number;
cin >> number;
check = number % 2;
checkpos = number > 0;
checksize = number < 40;
if (check != 0 && checkpos && checksize) {
cout << "Thank You!" << endl;
break;
}
else {
cout << "Incorrect entry. Please try again. " << endl
<< endl << endl;
i++;
}
}
if (i == 2){
cout << "Too many Tries.";
}
if (check == 0){
cout << "Your number is even " << endl;
}
if (!checkpos){
cout << "Your number is negative" << endl;
}
if (!checksize){
cout << "Your Number is too big!" << endl;
}
}
--output--
Please input a positve odd number lower then 40:
22
Incorrect entry. Please try again.
Please input a positve odd number lower then 40:
24
Incorrect entry. Please try again.
Please input a positve odd number lower then 40:
42
Incorrect entry. Please try again.
Your number is even
Your Number is too big!
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.