Guys I almost finished my yesterday's assignment, just need to tweak the code a little. Can anyone help?
Okay unfortunately, nobody's code yesterday worked.
#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; }
That's what I have and it works perfectly fine. All I need is...
lol is this the programming section? why store month as a double instead of an int? What error is the compiler returning?
this code accepts 1.0 or 1.00000 which is true, but i only want it to accept 1
that's it, store month as an int, not a double
well here's the thing, if I store it as an integer, it is accepting 1.1 etc..
Because that's how an integer works, it drops whatever is after the integer part
I was thinking of using characters..
but I can't figure out how to set the conditions, i keep getting errors and stuff
use the ascii value would they work ? i'm not sure i've never attempted it
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..
this is why C++ isn't for beginners. if this was in python you won't get weird problems like this.
lol I am actually liking C++, like I've only had a class for 2 weeks and we've done a lot..
construct an array of integers from 1 to 12, and compare 'month' to the elements in that array.
i can't.. i can only use if/else/while and we didn't learn arrays yet..
oh okay... let me reflect on the code.. >.< what is it supposed to do?
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
If it gets one of those values it is supposed to ask you to try again
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..
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
ah well gtg I will crack this eventually... and im really close haha
#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.
Join our real-time social learning platform and learn together with your friends!