Ask your own question, for FREE!
Mathematics 18 Online
OpenStudy (bahrom7893):

Imran/anyone else familiar with C++, how would I do this?

OpenStudy (bahrom7893):

here

OpenStudy (bahrom7893):

im trying to use a while loop.. like while ( month !=1 || month != 2, etc..) ask for a new month

OpenStudy (anonymous):

for (int i = 0 ; i ++;i<12) { if(month = i ) { break;} else { flag =1;} } if flag==1 then cout <<"enter valid month"; else {exit;}

OpenStudy (bahrom7893):

I can't use for loops yet, I can only use while. and if/else

OpenStudy (bahrom7893):

actually the assignment says: If the value of month is between 0 and 12, output: A valid month has been entered. Otherwise, ask and allow the user to enter a new value for month.

OpenStudy (bahrom7893):

I tried this: #include<iostream> using namespace std; int main() { int month; cout << "Please enter a month (1-12): "; cin >> month; while (month != 1 || month != 2 || month != 3 || month != 4 || month != 5 || month != 6 || month != 7 || month != 8 || month != 9 || month != 10 || month != 11 || month != 12) { cout << "An incorrect month has been entered, try again: "; cin >> month; if (month = 1 || month = 2 || month = 3 || month = 4 || month = 5 || month = 6 || month = 7 || month = 8 || month = 9 || month = 10 || month = 11 || month = 12) cout << "A correct month has been entered."; break; } system("pause"); return 0; }

OpenStudy (bahrom7893):

But I'm getting some really weird error...

OpenStudy (bahrom7893):

In this line: || month = 10 || month = 11 || month = 12) non-Ivalue assignment error

OpenStudy (anonymous):

declare flag out of the while loop ..

OpenStudy (anonymous):

int flag =0; int i = 0; int month ; while (flag == 0 ) { cout<<"Enter Month "; cin>>month; i =0 ; while ( i < 12) { if (month == i) { flag =1; break; } else {flag = 0;} i ++; } if (flag== 1) {cout<<"Month is Correct"; exit; }else {cout<<"Month In correct"; } }

OpenStudy (anonymous):

It should do it only while and if statement

OpenStudy (anonymous):

Bahrom is it right ? It should be

OpenStudy (bahrom7893):

sorry I was away.. I can't use flags..

OpenStudy (anonymous):

you can't use flags but they are just integers

OpenStudy (bahrom7893):

wait..

OpenStudy (bahrom7893):

oh lol i see, why did u call it flag? I could've also just called it month etc.. let me see what's going on..

OpenStudy (anonymous):

ok

OpenStudy (bahrom7893):

nope the conditions need to be refined..

OpenStudy (bahrom7893):

This one: i < 12 If i enter say 1.1 it's still accepting

OpenStudy (bahrom7893):

because it's takin the integer part, 1

OpenStudy (anonymous):

but why would someone enter 1.1 for a month

OpenStudy (bahrom7893):

lol I dunno but I have to work that out too, since I already had one that would not accept anything other than 0<x<12

OpenStudy (anonymous):

Ok but your program the one you posted is fine too why is it showing error

OpenStudy (bahrom7893):

Oh i meant it's not giving me errors, not that one, but it is not doing what it's supposed to do

OpenStudy (anonymous):

and if i enter 1.1 there then it would also accept it

OpenStudy (bahrom7893):

yea

OpenStudy (anonymous):

what if we take float

OpenStudy (bahrom7893):

no i was thinking reject everything but integers

OpenStudy (bahrom7893):

Oh and I also tried something.. that one rejects everything other than the right numbers but it won't ask them to try again

OpenStudy (anonymous):

even with mine one ...(actually it's my father's computer and i don't have compiler)

OpenStudy (bahrom7893):

ah well let me take a break from this i got a headache i will come back again later and figure this out, i did that with everything before

OpenStudy (anonymous):

this is simple. int month; assert month > 0; while(month>12) { cout<<"enter a valid month"; cin>> month; } cout<<"A valid month has been entered";

OpenStudy (bahrom7893):

what is assert? I don't think im allowed to use anything other than if/else/while, because we didn't use assert yet

OpenStudy (bahrom7893):

*we didn't learn assert yet

OpenStudy (anonymous):

okay, then change it to: unsigned int month; cin>>month; if (month=0) { cout<<"enter a valid month"; } while(month>12) { cout<<"enter a valid month"; cin>> month; } cout<<"A valid month has been entered";

OpenStudy (phi):

2 thoughts: 1) a single = as in A=B is an assignment, while == is a test for equality. Perhaps this is part of your problem? 2) Try to keep the logic simple. For example, using the ideas already proposed int valid= 0; while(valid==0) { input month; if month valid (insert your test here) valid=1; else complain to user } The idea is it will loop until (if!) the user puts in a valid number.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!