473,385 Members | 1,863 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,385 software developers and data experts.

Stroustrup 5.9, exercise 7

/* Stroustrup: 5.9 exercise 7

STATEMENTS:
Define a table of the name sof months o fyear and the number of days
in each month. write out that table. Do this twice:

1.) using ar array of char for names of months and an array of numbers
for number of days.

2.) using an array of structures. each structure holds the name of the
month
and its corresponding number of days.
for now, i have tried the (1) only.
*/
#include<iostream>

int main()
{
const int months = 12;
int i = 0;

const char* ArrOfMonths[] = { "JAN", "FEB", "MAR", "APR", "MAY",
"JUN",
"JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };

const int ArrOfDays[] = {31, 28, 31, 30, 31,30, 31, 31, 30, 31, 30,
31};

std::cout << "\tMONTH\tDAYS\n";

const char** pmonths = ArrOfMonths;
const int* pdays = ArrOfDays[0];

for(int i = 0; *pmonths != '\0' || *pdays != '\0'; ++i)
{
std::cout << "\t" << *pmonths
<< "\t" << *pdays
<< std::endl;
++pmonths;
++pdays;
}
return 0;
}
----------------- OUTPUT -----------------
[arch@voodo tc++pl]$ g++ 5.9_ex-07.cpp
5.9_ex-07.cpp: In function 'int main()':
5.9_ex-07.cpp:31: error: invalid conversion from 'const int' to 'const
int*'
[arch@voodo tc++pl]$

Apr 1 '07 #1
11 1808
arnuld wrote:
/* Stroustrup: 5.9 exercise 7

STATEMENTS:
Define a table of the name sof months o fyear and the number of days
in each month. write out that table. Do this twice:

1.) using ar array of char for names of months and an array of numbers
for number of days.

2.) using an array of structures. each structure holds the name of the
month
and its corresponding number of days.
for now, i have tried the (1) only.
*/
#include<iostream>

int main()
{
const int months = 12;
int i = 0;

const char* ArrOfMonths[] = { "JAN", "FEB", "MAR", "APR", "MAY",
"JUN",
"JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };

const int ArrOfDays[] = {31, 28, 31, 30, 31,30, 31, 31, 30, 31, 30,
31};

std::cout << "\tMONTH\tDAYS\n";

const char** pmonths = ArrOfMonths;
const int* pdays = ArrOfDays[0];

for(int i = 0; *pmonths != '\0' || *pdays != '\0'; ++i)
{
std::cout << "\t" << *pmonths
<< "\t" << *pdays
<< std::endl;
++pmonths;
++pdays;
}
return 0;
}
----------------- OUTPUT -----------------
[arch@voodo tc++pl]$ g++ 5.9_ex-07.cpp
5.9_ex-07.cpp: In function 'int main()':
5.9_ex-07.cpp:31: error: invalid conversion from 'const int' to 'const
int*'
[arch@voodo tc++pl]$
Here:
const int* pdays = ArrOfDays[0];
you are trying to assign ArrOfDays[0], which is a
const int to a const int* and the compiler tells
you exactly that.

HTH,
- J.
Apr 1 '07 #2
On Apr 1, 9:09 pm, Jacek Dziedzic <jacek.dziedzic.n.o.s.p....@gmail.comwrote:
Here:
const int* pdays = ArrOfDays[0];
you are trying to assign ArrOfDays[0], which is a
const int to a const int* and the compiler tells
you exactly that.
yes, a pointer is always "int*". i am trying to create a pointer to
the 1st element of an Array of Integers. so it needs to be "int*".
since integers(elements of the array) are "const" so declared a
"pointer to a const".

i am not trying to convert anything, just want to declare a "pointer"
to the 1st element of an array.

Apr 1 '07 #3
On Apr 1, 9:09 pm, Jacek Dziedzic <jacek.dziedzic.n.o.s.p....@gmail.com
Here:
const int* pdays = ArrOfDays[0];
you are trying to assign ArrOfDays[0], which is a
const int to a const int* and the compiler tells
you exactly that.
ok, here is the corrected code. it took me 6 hours to correct it and
it runs and it does what i want but it also produces some *additional*
output. notice the last lines of output:

---------- PROGRAMME -------------
/* Stroustrup: 5.9 exercise 7

STATEMENTS:
Define a table of the name sof months o fyear and the number of days
in each month. write out that table. Do this twice:

1.) using ar array of char for names of months and an array of numbers
for number of days.

2.) using an array of structures. each structure holds the name of the
month
and its corresponding number of days.

*/
#include<iostream>

int main()
{
char* ArrOfMonths[] = { "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
"JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };

const int ArrOfDays[] = {31, 28, 31, 30, 31,30, 31, 31, 30, 31, 30,
31};

std::cout << "\tMONTH\tDAYS\n";

char** pmonths = ArrOfMonths;
const int* pdays = &ArrOfDays[0];

for(int i = 0; *pmonths != '\0' || *pdays != '\0'; ++i)
{
std::cout << "\t" << *pmonths
<< "\t" << *pdays
<< std::endl;
++pmonths;
++pdays;
}
return 0;
}
--------- OUTPUT -----------
[arch@voodo tc++pl]$ g++ -ansi -pedantic -Wall -Wextra 5.9_ex-07.cpp
[arch@voodo tc++pl]$ ./a.out
MONTH DAYS
JAN 31
FEB 28
MAR 31
APR 30
MAY 31
JUN 30
JUL 31
AUG 31
SEP 30
OCT 31
NOV 30
DEC 31
l.???
5128522
FEB 4343110
Segmentation fault
[arch@voodo tc++pl]$

Apr 1 '07 #4
On Apr 1, 10:08 pm, "arnuld" <geek.arn...@gmail.comwrote:
...[SNIP]...
AUG 31
SEP 30
OCT 31
NOV 30
DEC 31
l.?? ?
5128522
FEB 4343110
Segmentation fault
[arch@voodo tc++pl]$
ok, i got it. the arrays do not have a '\0' character i the end as i
used "{}" to create them. only string literals have '\0' in the end.
so the pointers went beyond the array comparing whatever was beyond
the array. now i used the array size (== 12) to control the "for" loop
and it does exactly what it was intended to.
Hmmm.. at least after 6-8 hours i was able to solve the half of the
exercise and now i will get down to the remaining part.

Apr 1 '07 #5
On Apr 1, 10:16 pm, "arnuld" <geek.arn...@gmail.comwrote:
ok, i got it. the arrays do not have a '\0' character i the end as i
used "{}" to create them. only string literals have '\0' in the end.
so the pointers went beyond the array comparing whatever was beyond
the array. now i used the array size (== 12) to control the "for" loop
and it does exactly what it was intended to.

Hmmm.. at least after 6-8 hours i was able to solve the half of the
exercise and now i will get down to the remaining part.

sorry forgot to post code:

----------- PROGRAMME -------
/* Stroustrup: 5.9 exercise 7

STATEMENTS:
Define a table of the name sof months o fyear and the number of days
in each month. write out that table. Do this twice:

1.) using ar array of char for names of months and an array of numbers
for number of days.

2.) using an array of structures. each structure holds the name of the
month
and its corresponding number of days.

*/
#include<iostream>

int main()
{
const int months = 12;
char* ArrOfMonths[] = { "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
"JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };

//an array of constant integers
const int ArrOfDays[] = {31, 28, 31, 30, 31,30, 31, 31, 30, 31, 30,
31};
std::cout << "\tMONTH\tDAYS\n";

char** pmonths = ArrOfMonths;
const int* pdays = &ArrOfDays[0]; // pointer to the constant integer

for(int i = 0; i < months; ++i)
{
std::cout << "\t" << *pmonths
<< "\t" << *pdays
<< std::endl;
++pmonths;
++pdays;
}
return 0;
}

---------- OUTPUT ------------
[arch@voodo tc++pl]$ g++ -ansi -pedantic -Wall -Wextra 5.9_ex-07.cpp
[arch@voodo tc++pl]$ ./a.out
MONTH DAYS
JAN 31
FEB 28
MAR 31
APR 30
MAY 31
JUN 30
JUL 31
AUG 31
SEP 30
OCT 31
NOV 30
DEC 31
[arch@voodo tc++pl]$

Apr 1 '07 #6
* arnuld:
>On Apr 1, 9:09 pm, Jacek Dziedzic <jacek.dziedzic.n.o.s.p....@gmail.com
>Here:
const int* pdays = ArrOfDays[0];
>you are trying to assign ArrOfDays[0], which is a
const int to a const int* and the compiler tells
you exactly that.

ok, here is the corrected code. it took me 6 hours to correct it
You're not a quitter, that's for sure.

and
it runs and it does what i want but it also produces some *additional*
output. notice the last lines of output:

---------- PROGRAMME -------------
/* Stroustrup: 5.9 exercise 7

STATEMENTS:
Define a table of the name sof months o fyear and the number of days
in each month. write out that table. Do this twice:

1.) using ar array of char for names of months and an array of numbers
for number of days.

2.) using an array of structures. each structure holds the name of the
month
and its corresponding number of days.

*/
#include<iostream>

int main()
{
char* ArrOfMonths[] = { "JAN", "FEB", "MAR", "APR", "MAY", "JUN",
"JUL", "AUG", "SEP", "OCT", "NOV", "DEC" };

const int ArrOfDays[] = {31, 28, 31, 30, 31,30, 31, 31, 30, 31, 30,
31};

std::cout << "\tMONTH\tDAYS\n";

char** pmonths = ArrOfMonths;
const int* pdays = &ArrOfDays[0];

for(int i = 0; *pmonths != '\0' || *pdays != '\0'; ++i)
Make that condition simply

i < 12

You don't have zero-terminated arrays.

Thus with the current condition the loop only stops when it causes an
operating system level exception, or coincidentally the condition
becomes true for some arbitrary memory content somewhere.

Other nits, a.k.a. possible improvements: (1) ArrOfMonths could and
probably should be declared as

char const* const ArrOfMonths[] = ...

since it's all very constant, and (2) instead of using pointers in the
loop, you could and probably should just use array indexing, x[i].

Hth.,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Apr 1 '07 #7
On Apr 1, 10:20 pm, "Alf P. Steinbach" <a...@start.nowrote:

You're not a quitter, that's for sure.
:-)

Make that condition simply

i < 12
yep, before reading your reply, i just used:

const int month = 12;

to avoid any magic numbers in code.

You don't have zero-terminated arrays.

Thus with the current condition the loop only stops when it causes an
operating system level exception, or coincidentally the condition
becomes true for some arbitrary memory content somewhere.

yes, i knew that. it seems like either you did not read my next reply
or you are a kind man :-)

Other nits, a.k.a. possible improvements: (1) ArrOfMonths could and
probably should be declared as

char const* const ArrOfMonths[] = ...

this one i don't know. does it mean:

"a constant pointer to a constant char"

if it means this then i culd have declared it like this:

const char *const ArrOfMonths[] = ....

is it right ?
since it's all very constant, and (2) instead of using pointers in the
loop, you could and probably should just use array indexing, x[i].
yes, i could but i did not because that way, to me, an array look like
a vector and it confuses my thinking-level. "pointers" keep my
thinking on "array-level" and "indexing" keeps me thinking about
"vectors".
Hth.,
yep, it did :-)

Apr 1 '07 #8
arnuld wrote:
const int* pdays = ArrOfDays[0];
Try this way:

const int* pdays = ArrOfDays;

You need the array's address, not the first element.
--
Linux engine 2.6.17-11-generic i686 GNU/Linux
Apr 1 '07 #9
On Apr 2, 2:48 am, "arnuld" <geek.arn...@gmail.comwrote:
On Apr 1, 10:20 pm, "Alf P. Steinbach" <a...@start.nowrote:
You're not a quitter, that's for sure.

:-)
Make that condition simply
i < 12

yep, before reading your reply, i just used:

const int month = 12;

to avoid any magic numbers in code.
You don't have zero-terminated arrays.
Thus with the current condition the loop only stops when it causes an
operating system level exception, or coincidentally the condition
becomes true for some arbitrary memory content somewhere.

yes, i knew that. it seems like either you did not read my next reply
or you are a kind man :-)
Other nits, a.k.a. possible improvements: (1) ArrOfMonths could and
probably should be declared as
char const* const ArrOfMonths[] = ...

this one i don't know. does it mean:

"a constant pointer to a constant char"

if it means this then i culd have declared it like this:

const char *const ArrOfMonths[] = ....

is it right ?
since it's all very constant, and (2) instead of using pointers in the
loop, you could and probably should just use array indexing, x[i].

yes, i could but i did not because that way, to me, an array look like
a vector and it confuses my thinking-level. "pointers" keep my
thinking on "array-level" and "indexing" keeps me thinking about
"vectors".
Hth.,

yep, it did :-)
Hi Arnuld I've two suggestions(practices)

1. const int* pdays = &ArrOfDays[0]; and const int* pdays = ArrOfDays;
are not having any difference but still the latter representation is
more meaningful as you are represeting a whole array. The first style
of representation will be meaningful when you are iterating from a
radomly selected index.

2. It's better to use strict size for arrays as it not supposed to
modify
const char* ArrOfMonths[months] declaration will ensure that you are
having exactly 12 elements in the array. same case for
ArrOfDays[months]. This is not apppropariate in all cases but in this
cases (or such cases)

Hope you get what I meant.

Regards,
Sarath - http://sarathc.wordpress.com/

Apr 2 '07 #10
On Apr 2, 6:27 am, "Sarath" <CSar...@gmail.comwrote:
Hi Arnuld I've two suggestions(practices)

1. const int* pdays = &ArrOfDays[0]; and const int* pdays = ArrOfDays;
are not having any difference but still the latter representation is
more meaningful as you are represeting a whole array. The first style
of representation will be meaningful when you are iterating from a
radomly selected index.
i think "ArrOfdays" will be simply and implicitly converted to
"&ArrOfDays[0]" where as the latter shows that you are only pointing
to the 1st element of the array, the true part of "pointers and
arrays", hence i find it meaningful.
2. It's better to use strict size for arrays as it not supposed to
modify
const char* ArrOfMonths[months] declaration will ensure that you are
having exactly 12 elements in the array. same case for
ArrOfDays[months]. This is not apppropariate in all cases but in this
cases (or such cases)
that i will use, for sure.

Apr 2 '07 #11
On Apr 2, 6:18 am, "arnuld" <geek.arn...@gmail.comwrote:
On Apr 2, 6:27 am, "Sarath" <CSar...@gmail.comwrote:
Hi Arnuld I've two suggestions(practices)
1. const int* pdays = &ArrOfDays[0]; and const int* pdays = ArrOfDays;
are not having any difference but still the latter representation is
more meaningful as you are represeting a whole array. The first style
of representation will be meaningful when you are iterating from a
radomly selected index.
i think "ArrOfdays" will be simply and implicitly converted to
"&ArrOfDays[0]" where as the latter shows that you are only pointing
to the 1st element of the array, the true part of "pointers and
arrays", hence i find it meaningful.
I think her point is that at the language level, C++ does not
make a distinction between a pointer to the first element of an
array, and a pointer to a single object (which might be the
first element of an array, or might not be). Such a distinction
is useful for the reader, however; you don't manipulate the
resulting pointers in the same way. Thus, the convention:
&arrOfDays[0] for the address of a single object (which just
happens to be the first element of an array), and simply
arrOfDays for the address of the first element of an array (used
as a substitute for the actual array).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 2 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
by: Oplec | last post by:
Hi, I am learning standard C++ as a hobby. The C++ Programming Language : Special Edition has been the principal source for my information. I read the entirety of the book and concluded that I...
7
by: arnuld | last post by:
problem: define functions F(char), g(char&) & h(const char&). call them with arguments 'a', 49, 3300, c, uc & sc where c is a char, uc is unsigned char & sc is signed char. whihc calls are legal?...
0
by: arnuld | last post by:
Stroustrup has a table in section 4.9 of declarations and definitions. he asks to write a similar table but in opposite sense: char ch; // declaration with definition he asks to do the...
0
by: arnuld | last post by:
this programme runs without any trouble. it took 45 minutes of typing. i posted it here so that people can save their precious time: // Stroustrup special edition // chapter 4 exercise 2 //...
2
by: arnuld | last post by:
MAX and MIN values of CHAR could not be displayed. Why ? BTW, any advice on improvement ? (please remember i have covered chapter 4 only) ------------- PROGRAMME -------------- /*...
16
by: arnuld | last post by:
i did what i could do at Best to solve this exercise and this i what i have come up with: ----------- PROGRAMME -------------- /* Stroustrup 5.9, exercise 3 STATEMENT: Use typedef to define...
6
by: arnuld | last post by:
this one was much easier and works fine. as usual, i put code here for any further comments/views/advice: --------- PROGRAMME ------------ /* Stroustrup: 5.9 exercise 7 STATEMENTS: Define a...
14
by: arnuld | last post by:
there is no "compile-time error". after i enter input and hit ENTER i get a run-time error. here is the code: ---------- PROGRAMME -------------- /* Stroustrup, 5.9, exercise 11 STATEMENT:...
27
by: arnuld | last post by:
it works fine without any trouble. i want to have advice on improving the code from any angle like readability, maintenance etc: ---------- PROGRAMME ------------ /* Stroustrup, 5.9, exercise 11...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.