473,395 Members | 2,151 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,395 software developers and data experts.

Stroustrup 5.9, exercise 11

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:
Read a sequence of words from the input. use "quit" as the
word
to terminate the input. Print the words in the order they
were
entered. don't print a word twice.modify the programme to sort
the
words before printing
them.

*/
#include<iostream>
#include<string>

int main()
{
const std::string EOI = "quit"; // End Of
Input
char* user_input = "";

const int max_input = 1000;
char* collect_input[max_input + 1];

for(int i=0; user_input != EOI || i < max_input; ++i)
{
std::cin >user_input;
collect_input[i] = user_input;
}

return 0;
}

------------- OUTPUT -------------
[arch@voodo tc++pl]$ g++ 5.9_ex-11.cpp
[arch@voodo tc++pl]$ ./a.out
like
Segmentation fault
[arch@voodo tc++pl]$

Apr 2 '07 #1
14 2442
arnuld wrote:
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:
Read a sequence of words from the input. use "quit" as the
word
to terminate the input. Print the words in the order they
were
entered. don't print a word twice.modify the programme to sort
the
words before printing
them.

*/
#include<iostream>
#include<string>

int main()
{
const std::string EOI = "quit"; // End Of
Input
char* user_input = "";
WHY? Why 'char*'? Why couldn't this also be 'std::string'?
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 2 '07 #2
On Apr 2, 8:43 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
WHY? Why 'char*'? Why couldn't this also be 'std::string'?
BECAUSE...Because ... ;-)

i have not across "std::string" yet. presently at Stroustrup chpater 5.

Apr 2 '07 #3
arnuld wrote:
>On Apr 2, 8:43 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>WHY? Why 'char*'? Why couldn't this also be 'std::string'?

BECAUSE...Because ... ;-)

i have not across "std::string" yet. presently at Stroustrup chpater
5.
So? You have another object there already of type 'std::string'
Be consistent.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 2 '07 #4
arnuld wrote:
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:
Read a sequence of words from the input. use "quit" as the
word
to terminate the input. Print the words in the order they
were
entered. don't print a word twice.modify the programme to sort
the
words before printing
them.

*/
#include<iostream>
#include<string>

int main()
{
const std::string EOI = "quit"; // End Of
Input
char* user_input = "";

const int max_input = 1000;
char* collect_input[max_input + 1];

for(int i=0; user_input != EOI || i < max_input; ++i)
{
std::cin >user_input;
collect_input[i] = user_input;
You set user_input to point to a string literal of one character
length. There's no storage there to read any characters into. You
either have to allocate storage, or use a construct that does so
automatically, as Victor suggested (std::string).


Brian
Apr 2 '07 #5
arnuld wrote:
::: On Apr 2, 8:43 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
::
::: WHY? Why 'char*'? Why couldn't this also be 'std::string'?
::
:: BECAUSE...Because ... ;-)
::
:: i have not across "std::string" yet. presently at Stroustrup chpater
:: 5.

So PLEASE read Chapter 20 real SOON.
You know we told you to read "Accelerated C++". There you have Chapter 1
"Working with strings". :-)
Bo Persson
Apr 2 '07 #6
On Apr 2, 10:47 pm, "Bo Persson" <b...@gmb.dkwrote:

So PLEASE read Chapter 20 real SOON.
OK.
You know we told you to read "Accelerated C++". There you have Chapter 1
"Working with strings". :-)
i remember you advised "Accelerated C++" but i just fascinated with
Stroustrup's book.

BTW, Stroustrup gave this exercise in chapter 5 (Pointer, Structures
and Arrays). hence this exercise must be solved without std::strings,
i just used it accidentally.

i am not asking for solution but asking for a "method" only, a
theoretical solution using the concepts of chapter 5. i will convert
that to code myself.


Apr 2 '07 #7
* arnuld:
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:
Read a sequence of words from the input. use "quit" as the
word
to terminate the input. Print the words in the order they
were
entered. don't print a word twice.modify the programme to sort
the
words before printing
them.

*/
Are you sure this exercise is from the same chapter as the other
exercises you've done lately?

#include<iostream>
#include<string>

int main()
{
const std::string EOI = "quit"; // End Of
Input
char* user_input = "";

const int max_input = 1000;
char* collect_input[max_input + 1];

for(int i=0; user_input != EOI || i < max_input; ++i)
{
std::cin >user_input;
collect_input[i] = user_input;
}

return 0;
}
'user_input' is a pointer initialized to point to (the first character
of) an empty string literal.

operator>tries to store the user's input in that literal. It writes
all over memory. Crash.

You need to store the input characters in some array. std::string does
that for you automatically. Alternatively, if you don't want to use
library classes, you can just use a suitably huge char array.

Then you need to store the strings in some array. One way, without
using std::string, is: for each read string, allocate a new char array
just large enough for the string (number of characters + 1), and store
that pointer in an array of pointers, and increment a variable that
denotes the current number of strings.

Then, I think it's best to just output the strings, don't think about
sorting yet.

--
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 2 '07 #8

"arnuld" <ge*********@gmail.comwrote in message
news:11*********************@d57g2000hsg.googlegro ups.com...
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:
Read a sequence of words from the input. use "quit" as the
word
to terminate the input. Print the words in the order they
were
entered. don't print a word twice.modify the programme to sort
the
words before printing
them.

*/
#include<iostream>
#include<string>

int main()
{
const std::string EOI = "quit"; // End Of
Input
char* user_input = "";

const int max_input = 1000;
char* collect_input[max_input + 1];

for(int i=0; user_input != EOI || i < max_input; ++i)
{
std::cin >user_input;
Hint: What does the pointer 'user_inpt' point to?
collect_input[i] = user_input;
}

return 0;
}

------------- OUTPUT -------------
[arch@voodo tc++pl]$ g++ 5.9_ex-11.cpp
[arch@voodo tc++pl]$ ./a.out
like
Segmentation fault
No, it's your fault. :-)

-Mike
Apr 2 '07 #9
On Apr 3, 12:02 am, "Alf P. Steinbach" <a...@start.nowrote:
You need to store the input characters in some array. std::string does
that for you automatically. Alternatively, if you don't want to use
library classes, you can just use a suitably huge char array.

Then you need to store the strings in some array. One way, without
using std::string, is: for each read string, allocate a new char array
just large enough for the string (number of characters + 1), and store
that pointer in an array of pointers, and increment a variable that
denotes the current number of strings.
Then, I think it's best to just output the strings, don't think about
sorting yet.
OK, this is what i have created what i call a "partial
implementation", partial because i am unable to think of anything
further:

/* Stroustrup, 5.9, exercise 11

STATEMENT:
Read a sequence of words from the input. use "quit" as the word
to terminate the input. Print the words in the order they were
entered. don't print a word twice.modify the programme to sort the
words before printing them.
METHOD:

1.) we will creat ana "array of pointers" to store all of the input.

2.) we will count each word a 1 input.

3.) to take input from user we will use an "array of CHARs". each
element
of "array of pointers" will point to this input"

4.) when user will enter "quit", we will terminate the programme and
print all of the input each word per line.

*/
#include<iostream>

int main()
{
const char* EOI = "quit";
const int max_input = 1000;
const max_wordlen = 10;
const int NULL = 0;

const char* collected_words[max_input + 1];
char** pwords = words;

const char* user_input;
while(std::cin >user_input)
{
for(char* pui = user_input; pui != '\0'; ++pui)

-------------------------------------

i can' think:

1.) what should i do to equate the input with EOI.

2.) how to copy the input into the new array and the pointing the new
array
to the "collected_words[]"
Apr 3 '07 #10
On 3 Apr, 13:29, "arnuld" <geek.arn...@gmail.comwrote:
On Apr 3, 12:02 am, "Alf P. Steinbach" <a...@start.nowrote:
You need to store the input characters in some array. std::string does
that for you automatically. Alternatively, if you don't want to use
library classes, you can just use a suitably huge char array.
Then you need to store the strings in some array. One way, without
using std::string, is: for each read string, allocate a new char array
just large enough for the string (number of characters + 1), and store
that pointer in an array of pointers, and increment a variable that
denotes the current number of strings.
Then, I think it's best to just output the strings, don't think about
sorting yet.

OK, this is what i have created what i call a "partial
implementation", partial because i am unable to think of anything
further:

/* Stroustrup, 5.9, exercise 11

STATEMENT:
Read a sequence of words from the input. use "quit" as the word
to terminate the input. Print the words in the order they were
entered. don't print a word twice.modify the programme to sort the
words before printing them.

METHOD:

1.) we will creat ana "array of pointers" to store all of the input.

2.) we will count each word a 1 input.

3.) to take input from user we will use an "array of CHARs". each
element
of "array of pointers" will point to this input"

4.) when user will enter "quit", we will terminate the programme and
print all of the input each word per line.

*/

#include<iostream>

int main()
{
const char* EOI = "quit";
const int max_input = 1000;
const max_wordlen = 10;
const int NULL = 0;

const char* collected_words[max_input + 1];
Skip the +1, 1000 words are enough, remember that 'int a[10]' will
give you an array with 10 elements numbered from 0 to 9.
char** pwords = words;
What's this? I can't see any need for it at the moment.
const char* user_input;
while(std::cin >user_input)
Should you not allocate memory for the input?

To see if the word entered is "quit" you can use strcmp(), if it isn't
then you should probably allocate a new string/char-array the size of
the word and use strcpy() to copy the word from user_input into this
string. Then you need to add the string to collected_words (you'll
need a variable to keep track of how many words have been entered too)
which is the same as setting collected_words[i] = the_string, where i
is the counter, and the_string is the newly allocated string.

To be honest I don't think you'll learn much from doing the rest of
the exercises in that chapter. It's mostly about low-levels stuff that
you'll probably have little use of in the real world and it's quite
complicated when you can't use std::string, std::vectors and such. My
advice is to skip them and perhaps do them later.

--
Erik Wikström

Apr 3 '07 #11
arnuld wrote:
::: On Apr 2, 10:47 pm, "Bo Persson" <b...@gmb.dkwrote:
::
::
::: So PLEASE read Chapter 20 real SOON.
::
:: OK.
::
::: You know we told you to read "Accelerated C++". There you have
::: Chapter 1 "Working with strings". :-)
::
:: i remember you advised "Accelerated C++" but i just fascinated with
:: Stroustrup's book.
::
:: BTW, Stroustrup gave this exercise in chapter 5 (Pointer, Structures
:: and Arrays). hence this exercise must be solved without std::strings,
:: i just used it accidentally.
::
:: i am not asking for solution but asking for a "method" only, a
:: theoretical solution using the concepts of chapter 5. i will convert
:: that to code myself.

My point was rather that you try the hard C-style way first, rarely used in
real C++ code.

After reading chapter 20, you will know that you can do just:
std::string user_input;

std::cin >user_input;

if (user_input == "quit")
// do something
No pointers, no arrays, no overflows, no fixed size limits, no strcmp, ...
If you collect the input in a std::vector<std::string, you don't have to
decide how long the strings can be, or if there can be more than 1000 of
them. It just works!

Why punish yourself? :-)
Bo Persson
Apr 3 '07 #12
On Apr 3, 5:52 pm, "Erik Wikström" <eri...@student.chalmers.sewrote:

To see if the word entered is "quit" you can use strcmp(), if it isn't
then you should probably allocate a new string/char-array the size of
the word and use strcpy() to copy the word from user_input into this
string. Then you need to add the string to collected_words (you'll
need a variable to keep track of how many words have been entered too)
which is the same as setting collected_words[i] = the_string, where i
is the counter, and the_string is the newly allocated string.
Aha... my moments from the exercises of Chapter 1, K&R2 are recalled.
it *is* the 'C' thing... YES...this is exactly how i created solutions
in 'C'.

To be honest I don't think you'll learn much from doing the rest of
the exercises in that chapter. It's mostly about low-levels stuff that
you'll probably have little use of in the real world and it's quite
complicated when you can't use std::string, std::vectors and such. My
advice is to skip them and perhaps do them later.
then i think i need to forget about Stroustrup too, as after
Procedural parts he starts describing Classes and he talks about
std::string only at Chapter 20. i tried to take a peek into chapter 20
and at the very beginning of chapter this one blew me up:

template<class Ch>struct char_traits { };

static bool eq_int_type(const int_type&, const int_type&);

static char_type* move(char_type* s, const char_type* s2, size_t n);

i think if anybody will explain it to me in FULL-DETAILs i still will
not make any sense out of this thing, always get amazed at "why(s) and
Why not(s)". it seems like either these things are written for a
Software Engineer (i am not that kind) or for the persons who are
capable of understanding Bible (C++ Bible). it is not a tutorial.

Apr 3 '07 #13
On Apr 3, 9:02 pm, "Bo Persson" <b...@gmb.dkwrote:
My point was rather that you try the hard C-style way first, rarely
used in
real C++ code.

After reading chapter 20, you will know that you can do just:

std::string user_input;

std::cin >user_input;

if (user_input == "quit")
// do something

No pointers, no arrays, no overflows, no fixed size limits, no strcmp, ...

If you collect the input in a std::vector<std::string, you don't have to
decide how long the strings can be, or if there can be more than 1000 of
them. It just works!
Hmm... i read these kind of things, COMPLETELY these kinds of ways to
solve problems in "C++ Primer 4e" and "Accelerated C++" both of which
are not available in my country :-(
but a book named "Essential C++" bt Lippman is available which looks
like "Accelerated C++" in design and presentation, i am not sure but
this i what i have felt. "C++ Primer 4e" is the best one i have ever
seen.

Why punish yourself? :-)

after banging my head with C++ and C, i have learnt:

1.) 'C' is really a simple design, never meant to create Application
kind of things, only targeted towards System Programming a.k.a bit-
twiddling, but it feels like GNU's official language of choice for
everything.

2.) C++ is much much larger and more confusing, as compared to 'C'. it
is intended to create Application Software, System Programming etc
etc. it seems Stroustrup wanted to make it for all.

this is what i have experienced, no biasing here.

Apr 3 '07 #14
On 2007-04-03 18:34, arnuld wrote:
>On Apr 3, 5:52 pm, "Erik Wikström" <eri...@student.chalmers.sewrote:

>To see if the word entered is "quit" you can use strcmp(), if it isn't
then you should probably allocate a new string/char-array the size of
the word and use strcpy() to copy the word from user_input into this
string. Then you need to add the string to collected_words (you'll
need a variable to keep track of how many words have been entered too)
which is the same as setting collected_words[i] = the_string, where i
is the counter, and the_string is the newly allocated string.

Aha... my moments from the exercises of Chapter 1, K&R2 are recalled.
it *is* the 'C' thing... YES...this is exactly how i created solutions
in 'C'.

>To be honest I don't think you'll learn much from doing the rest of
the exercises in that chapter. It's mostly about low-levels stuff that
you'll probably have little use of in the real world and it's quite
complicated when you can't use std::string, std::vectors and such. My
advice is to skip them and perhaps do them later.

then i think i need to forget about Stroustrup too, as after
Procedural parts he starts describing Classes and he talks about
std::string only at Chapter 20. i tried to take a peek into chapter 20
and at the very beginning of chapter this one blew me up:

template<class Ch>struct char_traits { };

static bool eq_int_type(const int_type&, const int_type&);

static char_type* move(char_type* s, const char_type* s2, size_t n);
Well, chapter 20 is very nice if you've ever have any technical
questions regarding strings, but to use them you don't have to read it.
A quick look shows that he'll be using strings some in the examples of
chapter 6 so just by reading that chapter you should get most of what
you need to use strings. And you can always ask us if something seems
strange.

--
Erik Wikström
Apr 4 '07 #15

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...
11
by: arnuld | last post by:
/* 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...
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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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...
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...

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.