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

Guys I almost finished my yesterday's assignment, just need to tweak the code a little. Can anyone help?

OpenStudy (bahrom7893):

Okay unfortunately, nobody's code yesterday worked.

OpenStudy (bahrom7893):

#include<iostream> using namespace std; int main() { cout << "Enter a month (1-12): "; double month; 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) { 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) break; else { cout << "An invalid month has been entered, please try again: "; cin >> month; } } cout<<"A valid month has been entered."<<endl; system("pause"); return 0; }

OpenStudy (bahrom7893):

That's what I have and it works perfectly fine. All I need is...

OpenStudy (anonymous):

lol is this the programming section? why store month as a double instead of an int? What error is the compiler returning?

OpenStudy (bahrom7893):

this code accepts 1.0 or 1.00000 which is true, but i only want it to accept 1

OpenStudy (anonymous):

that's it, store month as an int, not a double

OpenStudy (bahrom7893):

well here's the thing, if I store it as an integer, it is accepting 1.1 etc..

OpenStudy (bahrom7893):

Because that's how an integer works, it drops whatever is after the integer part

OpenStudy (bahrom7893):

I was thinking of using characters..

OpenStudy (bahrom7893):

but I can't figure out how to set the conditions, i keep getting errors and stuff

OpenStudy (anonymous):

use the ascii value would they work ? i'm not sure i've never attempted it

OpenStudy (bahrom7893):

hmm I don't know lol let me try... but the code above is the closest i ever got to completing this crap, even yesterday's complicated codes that were posted here didn't work..

OpenStudy (anonymous):

this is why C++ isn't for beginners. if this was in python you won't get weird problems like this.

OpenStudy (bahrom7893):

lol I am actually liking C++, like I've only had a class for 2 weeks and we've done a lot..

OpenStudy (anonymous):

construct an array of integers from 1 to 12, and compare 'month' to the elements in that array.

OpenStudy (bahrom7893):

i can't.. i can only use if/else/while and we didn't learn arrays yet..

OpenStudy (anonymous):

oh okay... let me reflect on the code.. >.< what is it supposed to do?

OpenStudy (bahrom7893):

okay here's the thing it is only supposed to accept these things: 1 2 3 4 5 6 7 8 9 10 11 12 That's all, no 1.0 or 1.1 or 15 or 0

OpenStudy (bahrom7893):

If it gets one of those values it is supposed to ask you to try again

OpenStudy (bahrom7893):

And it works fine.. but it keeps accepting 1.0, which is always going to be accepted if I use any type of number data type..

OpenStudy (bahrom7893):

actually all i need is to find, the following codes so to speak: how can I write: Character is not and Character is char month; while (month!='1') didn't work

OpenStudy (bahrom7893):

ah well gtg I will crack this eventually... and im really close haha

OpenStudy (anonymous):

#include <iostream> #include <stdlib.h> #include <string> using namespace std; int main() { cout << "Enter a month (1-12)"; string month; 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 << "\n" << month <<" is not a valid month, please try again" << endl; cin >> month;} cout << month << " is a valid month!" << endl; system("PAUSE"); return 0; } There; by storing month as a string literal, we can effectively test month for a valid input value.

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!