By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,983 Members | 1,627 Online
Bytes IT Community
+ Ask a Question
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 <iostream>

#include <iomanip>

#include <string>

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. "<<endl<<endl;

cout<<"What Internet package have you purchased A,B or C? : ";

cin>>Choice;

while (!(Choice =='A' || Choice =='B'|| Choice == 'C'))

{

cout<<"You must enter package A B or C. Please try again!"<<endl;

return 0;

}

cout<<"What month do you need to calculate customer usage charge? :";

cin.getline(Month,40);

cin.ignore();

cout<<endl<<endl;

if (strcmp(Month,January)==0)

cout<<"The string";

/*switch(Month)

{

case "January": cout<<"Enter the hours usage for the month of "<<Month<<" !
:";

while (Hour > 744 && Hour < 0 )

{

cout<<"There are only 744 hours in the month of "<<Month<<endl;

cout<<"and you cannot enter hour usage less than 0."<<endl;

cout<<"please enter the correct hour! : ";

cin>>Hour;

cout<<endl<<endl;

}

}

*/

//cout<<"How many hours were used for the month of "<<Month" ? : ";

//cin>>Hour;

//if (Hour > 744 || Hour < 1)

//cout<<"You cannot have internet access longer than 744 hours or less than
1 hour."<<endl;
switch (Choice)

{
case 'A': if (Hour <=10)

{

Charge =9.95;

cout<<"Your hours online is: "<<Hour<<endl;

cout<<"Your internet charge is: "<<Charge<<endl;

break;

}

if (Hour > 10)

{

Charge = 9.95 +((Hour-10)*2);

cout<<"Your hours online is: "<<Hour<<endl;

cout<<"Your internet charge is: "<<Charge<<endl;

break;

}
case 'B': if (Hour <=20)

{

Charge =14.95;

cout<<"Your hours online is: "<<Hour<<endl;

cout<<"Your internet charge is: "<<Charge<<endl;

break;

}

if (Hour >= 20)

{

Charge = 14.95 +((Hour-20)*1);

cout<<"Your hours online is: "<<Hour<<endl;

cout<<"Your internet charge is: "<<Charge<<endl;

break;

}
case 'C': Charge = 19.95;

cout<<"Your hours online is: "<<Hour<<endl;

cout<<"Your internet charge is: "<<Charge<<endl;

break;


}

}
Oct 12 '05 #1
Share this Question
Share on Google+
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.

<An**********@gmail.com> 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:
[homework help]

First, you should search through the archives for this news group
for the "welcome" message. It includes a pointer to the FAQ. You
should get it and read it end-to-end very carefully. It's got a
*LOT* of great info.

Ok, what does "I cannot use array function to solve this problem"
mean? Does it mean you are not allowed to use a character array?
Because you've got one. (Though it's not declared properly, as
another poster indicated.) If you can't use character arrays, then
you must use <string> variables. You will need to read your text
about them.

You will also need to carefully read about how to use:
- if
- while
- switch/case

For example, the case in C++ can only be an integer constant.
You can't for example, put this:

case "January":

You can only put compile time constant integers in there.

case 1:

You can't even put a variable in there. The following is not legal.

int x;
x=1;

// set up case stuff here

case x:

You should also begin, right at the start, developing a good typing
style for how you arrange your code. Example:

int main()
{
if(condition)
{
stuff to do
}
return 0;
}

Compare that to this.

int main()
{
if(condition)
{
stuff to do
}
return 0;
}

It makes it much easier to see what belongs to what. And the compiler
does not care.

And speaking of the compiler, it's your friend when you are learning.
When you are writing short programs, write some, then try to compile
it. If you get error messages, fix the problems first.

Also, you need to read up on how to create functions, how to pass
information to them and back from them. Your example cries out for
use of functions to make it easier to understand. For example:

int MonthNumFromName(string monthName);

This might be what you would call a function that takes a string
containing the name of a month, and returns a number indicating
the month, as 1 for January, 2 for Feb. and so on. And be sure
to decide what to do if monthName is not actually the name of
a month.

Or this:

int HoursInMonth(int monthNum);

This might be a function that gives you the hours in the month from
the number of the month. And be sure you include what to do if
monthNum is outside the range 1 to 12. And have a thought for what
to do about February in leap years.

And note that I'm trying to name things very descriptively to tell
what they are without any comments required. Again with good style.
Socks

Oct 12 '05 #5

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. "<<endl<<endl;

cout<<"What Internet package have you purchased A,B or C? : ";

cin>>Choice;

while (!(Choice =='A' || Choice =='B'|| Choice == 'C'))

{

cout<<"You must enter package A B or C. Please try again!"<<endl;

return 0;

}


Did you really mean to exit the entire program because the user entered
bad data? If so, why is it in a while loop? I suspect you did that
because you got an infinite loop when you entered an incorrect value,
but that's not the way to correct the problem.

http://www.parashift.com/c++-faq-lit....html#faq-15.2

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Oct 12 '05 #6

P: n/a
On Wed, 12 Oct 2005 10:50:22 -0500, "Richard" <no********@yahoo.com>
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 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 <iostream>

#include <iomanip>

#include <string>

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 information

cout<<"This program calculate the monthly internet usage charge for a
customer. "<<endl<<endl;

cout<<"What Internet package have you purchased A,B or C? : ";

cin>>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!"<<endl;

return 0;

}

oops ... you've already returned from main()...???
cout<<"What month do you need to calculate customer usage charge? :";

cin.getline(Month,40);

cin.ignore();

cout<<endl<<endl;

if (strcmp(Month,January)==0)

cout<<"The string";

/*switch(Month)

{

case "January": cout<<"Enter the hours usage for the month of "<<Month<<" !
:";

Oops ... Hour was never initialized! Anything could happen here...
The rest I didn't look at. I'm sure others will also help.
while (Hour > 744 && Hour < 0 )

{

cout<<"There are only 744 hours in the month of "<<Month<<endl;

cout<<"and you cannot enter hour usage less than 0."<<endl;

cout<<"please enter the correct hour! : ";

cin>>Hour;

cout<<endl<<endl;

}

}

*/

//cout<<"How many hours were used for the month of "<<Month" ? : ";

//cin>>Hour;

//if (Hour > 744 || Hour < 1)

//cout<<"You cannot have internet access longer than 744 hours or less than
1 hour."<<endl;
switch (Choice)

{
case 'A': if (Hour <=10)

{

Charge =9.95;

cout<<"Your hours online is: "<<Hour<<endl;

cout<<"Your internet charge is: "<<Charge<<endl;

break;

}

if (Hour > 10)

{

Charge = 9.95 +((Hour-10)*2);

cout<<"Your hours online is: "<<Hour<<endl;

cout<<"Your internet charge is: "<<Charge<<endl;

break;

}
case 'B': if (Hour <=20)

{

Charge =14.95;

cout<<"Your hours online is: "<<Hour<<endl;

cout<<"Your internet charge is: "<<Charge<<endl;

break;

}

if (Hour >= 20)

{

Charge = 14.95 +((Hour-20)*1);

cout<<"Your hours online is: "<<Hour<<endl;

cout<<"Your internet charge is: "<<Charge<<endl;

break;

}
case 'C': Charge = 19.95;

cout<<"Your hours online is: "<<Hour<<endl;

cout<<"Your internet charge is: "<<Charge<<endl;

break;


}

}


--
Bob Hairgrove
No**********@Home.com
Oct 12 '05 #7

P: n/a
On 12 Oct 2005 18:57:32 GMT, "Default User" <de***********@yahoo.com>
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 <string> 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" <de***********@yahoo.com>
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 <string> 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 <string> 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 <string> 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" <pu*********@hotmail.com>
wrote:
Bob Hairgrove wrote:
On 12 Oct 2005 18:57:32 GMT, "Default User" <de***********@yahoo.com>
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 <string> 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.


lol ... my bad!
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 <string> 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 <string> 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.


All good points ... but I don't think the OP was ready (nor concerned
with) coding for possible "automatic data feeds" here <g> 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" <de***********@yahoo.com>
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 <string> 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.