443,983 Members | 1,627 Online
Need help? Post your question and get tips & solutions from a community of 443,983 IT Pros & Developers. It's quick & easy.

# string with mutiple characters

 P: n/a Here is the program problem: Month with 30 days have 720 hours, and month with 31 days have 744 hours. February, with 28 days, has 672 hours. Enhance the input validation of the program code below by asking user for the month (by name), and validating that the number of hours entered is not more than the maximum number of the entire month. There is a table that display how many days in each month and their hours. It is unnecessary to for me to type them out. I new to C++ and I have problem assigning string value wheather with the " or ' symbol. Please look at my variable declaration for the months and tell me what I did wrong. Plus sugguest wheather i should use the switch or if statements approve to 12 months. Note, I cannot use array function to solve this problem. Here is the code. #include #include #include using namespace std; int main() { int Hour; double Charge; char Choice ,Month[40]; char January[40]='January'; // ask for information cout<<"This program calculate the monthly internet usage charge for a customer. "<>Choice; while (!(Choice =='A' || Choice =='B'|| Choice == 'C')) { cout<<"You must enter package A B or C. Please try again!"< 744 && Hour < 0 ) { cout<<"There are only 744 hours in the month of "<>Hour; cout<>Hour; //if (Hour > 744 || Hour < 1) //cout<<"You cannot have internet access longer than 744 hours or less than 1 hour."< 10) { Charge = 9.95 +((Hour-10)*2); cout<<"Your hours online is: "<= 20) { Charge = 14.95 +((Hour-20)*1); cout<<"Your hours online is: "<
10 Replies

 P: n/a Richard wrote: [blatant "do my homework" request redacted] Tell you what, Richard. Give me your instructors email address, and I'll send the answer directly to him, saving you the trouble. Oct 12 '05 #2

 P: n/a char January[40]='January'; change the line above to: char January[40]="January"; character variables use single quotes, strings use double quotes. Oct 12 '05 #3

 P: n/a Thanks so much. That is all I need. wrote in message news:11**********************@g49g2000cwa.googlegr oups.com... char January[40]='January'; change the line above to: char January[40]="January"; character variables use single quotes, strings use double quotes. Oct 12 '05 #4

 P: n/a Richard wrote: char Choice ,Month[40]; char January[40]='January'; The incorrect initialization has already been mentioned. What is your reason for 40 character month arrays? No month has that long of a name. In particular, the January array is oversized. This would be an improvement: const char *January = "January"; // pointer to a string literal You have no reason for that array to be mutable. Likewise the longest any month name can be is 8 characters, so you don't need that big honking input array. // ask for information cout<<"This program calculate the monthly internet usage charge for a customer. "<>Choice; while (!(Choice =='A' || Choice =='B'|| Choice == 'C')) { cout<<"You must enter package A B or C. Please try again!"<

 P: n/a On Wed, 12 Oct 2005 10:50:22 -0500, "Richard" wrote: Here is the program problem:Month with 30 days have 720 hours, and month with 31 days have 744 hours.February, with 28 days, has 672 hours. Enhance the input validation of theprogram code below by asking user for the month (by name), and validatingthat the number of hours entered is not more than the maximum number of theentire month. There is a table that display how many days in each month andtheir hours. It is unnecessary to for me to type them out. I new to C++and I have problem assigning string value wheather with the " or ' symbol.Please look at my variable declaration for the months and tell me what I didwrong. Plus sugguest wheather i should use the switch or if statementsapprove to 12 months. Note, I cannot use array function to solve thisproblem. Here is the code.#include #include #include using namespace std;int main(){int Hour;double Charge;char Choice ,Month[40];char January[40]='January'; Use double quotes here Single quotes are not forbidden by the standard, but I believe the behavior is either undefined (probably) or implementation-specific. // ask for informationcout<<"This program calculate the monthly internet usage charge for acustomer. "<>Choice;while (!(Choice =='A' || Choice =='B'|| Choice == 'C')) This is the correct usage of single quotes: for a single character. {cout<<"You must enter package A B or C. Please try again!"< 744 && Hour < 0 ){cout<<"There are only 744 hours in the month of "<>Hour;cout<>Hour;//if (Hour > 744 || Hour < 1)//cout<<"You cannot have internet access longer than 744 hours or less than1 hour."< 10){Charge = 9.95 +((Hour-10)*2);cout<<"Your hours online is: "<= 20){Charge = 14.95 +((Hour-20)*1);cout<<"Your hours online is: "<

 P: n/a On 12 Oct 2005 18:57:32 GMT, "Default User" wrote: Richard wrote: char Choice ,Month[40]; char January[40]='January';The incorrect initialization has already been mentioned. What is yourreason for 40 character month arrays? No month has that long of a name.In particular, the January array is oversized. This would be animprovement:const char *January = "January"; // pointer to a string literalYou have no reason for that array to be mutable.Likewise the longest any month name can be is 8 characters, so youdon't need that big honking input array. [snip] The length issue is something of a mute point ... the program actually includes already, so I would use a std::string here. You are making an equally questionable assumption that the longest any month name can be is 8 characters. What about other languages? Why make any assumptions at all? -- Bob Hairgrove No**********@Home.com Oct 12 '05 #8

 P: n/a Bob Hairgrove wrote: On 12 Oct 2005 18:57:32 GMT, "Default User" wrote: [snips]Likewise the longest any month name can be is 8 characters, so youdon't need that big honking input array. [snip] The length issue is something of a mute point ... the program actually includes already, so I would use a std::string here. Well, it is a mute point, as it does not make any sound. But I think you meant it's a moot point, that is, a case suitable only for discussion for the sake of discussion. Actually, allowing the input array to be longer than required is a thorny issue. Does one want to make the array lots longer so the program exits gracefully if the user types in a long line? Or, do you want an "explosion" that will alert everybody to this hole in your program? Coding style WRT errors and especially unusual circumstances is a non-trivial topic. What exactly is the desired behaviour of your code when a situation arises you didn't prepare for? Certainly in this case, using will allow this particular issue to be quite thoroughly dealt with. Few users are going to be prepared to type in on a console more than a character will hold. Then, one day, your prog gets "piped" onto an automatic data feed, and then what? Oh well, that gets us into assumptions. You are making an equally questionable assumption that the longest any month name can be is 8 characters. What about other languages? Why make any assumptions at all? Um. Because the OP was posting in English, and used such things as "January." In order to deal with other languages, one would have to get into a lot of stuff that is clearly far beyond the OP. At some point, one is forced to make some assumptions in creating a code. Now, it's usualy highly desirable to make them explicit. And to document them. But it's really not possible to create a program that makes no assumptions. The usual thing is to try to explore the problem space in such a way that reasonably expectable changes will be easy to implement. In this case, it's quite reasonable to expect, as an example, upper or lower case to be entered by the user, and to accept either. Or to have the common abbreviations for the month names. Or to include or not include the period at the end, such as both Jan. and Jan being acceptable. Or, in the other direction, to list the choices for the user and force the user to select one and type it exactly. That would be the "database-like" solution, force the user to pick from a prepared list. Whether it's reasonable to expect other languages for the month names is something you'd need to explore with the client. Does the client need that? Might they in the near future? In the OP's case, the client is the class instructor, and the spec for the problem is the problem statement. So, including the ability to deal with other languages is probably reasonable to rule as out of scope. Whether to have the code deal with upper/lower case or abbreviations is a question of how many marks the assignment is worth to the final grade. The more flexibility you put in the code, the more effort is usually required. Of course, not in any linear fashion. But if you wind up designing something that can deal with any language, specified at run time, any character set, ASCII or not, and it's all brilliant and wonderful, but costs 10 times what the client is prepared to pay, then you probably don't get the contract. Socks Oct 12 '05 #9

 P: n/a On 12 Oct 2005 14:03:16 -0700, "Puppet_Sock" wrote: Bob Hairgrove wrote: On 12 Oct 2005 18:57:32 GMT, "Default User" wrote:[snips] >Likewise the longest any month name can be is 8 characters, so you >don't need that big honking input array. [snip] The length issue is something of a mute point ... the program actually includes already, so I would use a std::string here.Well, it is a mute point, as it does not make any sound.But I think you meant it's a moot point, that is, acase suitable only for discussion for the sake of discussion. lol ... my bad! Actually, allowing the input array to be longer than requiredis a thorny issue. Does one want to make the array lots longerso the program exits gracefully if the user types in a long line?Or, do you want an "explosion" that will alert everybody to thishole in your program? Coding style WRT errors and especiallyunusual circumstances is a non-trivial topic. What exactly isthe desired behaviour of your code when a situation arises youdidn't prepare for?Certainly in this case, using will allow this particularissue to be quite thoroughly dealt with. Few users are going to beprepared to type in on a console more than a characterwill hold. Then, one day, your prog gets "piped" onto an automaticdata feed, and then what? Oh well, that gets us into assumptions. All good points ... but I don't think the OP was ready (nor concerned with) coding for possible "automatic data feeds" here But I do believe that coding for date/time things, in general, is usually done with way too many assumptions of this kind: e.g. that there will only be 12 months in a year, that names take a maximum of so-and-so many characters, etc. If he/she wants to stick with naked character arrays, I would perhaps recommend 32 or a multiple thereof for an array size because this is often covenient as a cache line size, resulting in faster performance on most platforms -- but then one should also align the variable to a multiple of 32 :) ... however, I think we can stick to std::string and forget about the rest. [snip] -- Bob Hairgrove No**********@Home.com Oct 12 '05 #10

 P: n/a Bob Hairgrove wrote: On 12 Oct 2005 18:57:32 GMT, "Default User" wrote: Richard wrote: char Choice ,Month[40]; char January[40]='January'; The incorrect initialization has already been mentioned. What is your reason for 40 character month arrays? No month has that long of a name. In particular, the January array is oversized. This would be an improvement: const char *January = "January"; // pointer to a string literal You have no reason for that array to be mutable. Likewise the longest any month name can be is 8 characters, so you don't need that big honking input array. [snip] The length issue is something of a mute point ... the program actually includes already, so I would use a std::string here. He was using arrays so it isn't moot at all. You are making an equally questionable assumption that the longest any month name can be is 8 characters. What about other languages? Why make any assumptions at all? I would agree that std::string is better. Brian Oct 12 '05 #11

### This discussion thread is closed

Replies have been disabled for this discussion.