473,407 Members | 2,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

string with mutiple characters

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
10 2315
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: varadha | last post by:
Hi, My Application listening to a port of my machine and it response with data to the requesting system. This application is provided as a service in my machine. Now i am planning to have...
4
by: Mantorok | last post by:
Hi I have a couple of encryption methods but when I call decrypt I get the string back but with a load \0 escape characters on the end? Any idea why? It is actually causing problems in some...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
0
by: deepak | last post by:
i have set multiple selection property in bith listboxes(html control) to true. i have taken 2 buttons(html control) say Button1,Button2.now i want to add mutiple selected items to another listbox...
18
by: Ger | last post by:
I have not been able to find a simple, straight forward Unicode to ASCII string conversion function in VB.Net. Is that because such a function does not exists or do I overlook it? I found...
0
by: saravana | last post by:
I have to display the mutiple xml filename path in treeview control using c#. net some thing like this In treeview control....... ========= =C:\\a.xml=
7
by: shanmugamit | last post by:
hi, i using mutiple check box but i didn't get all value... as $res=mysql_query("select * from cie where des='Resource Pipeline Associate' order by name"); $j=0;...
2
by: ravitunk | last post by:
hi..i have a datagridview in my windows application using C#......i want to select mutiple cells(by pressing shift key) or select mutiple columns or multiple rows.....if this happens then i should...
6
by: dewraj | last post by:
Hi I am using windows applicaiton(.net) as front end, and I want to get mutiple tables in Dataset by executing a single stored procedure i.e. SP would return mutiple tables (record sets) with the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.