Stroustrup 5.9, exercise 11

A

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.

*/


#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 = 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]$
 
V

Victor Bazarov

arnuld said:
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
 
A

arnuld

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.
 
V

Victor Bazarov

arnuld said:
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
 
D

Default User

arnuld said:
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 = 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
 
B

Bo Persson

arnuld wrote:
::
::: 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
 
A

arnuld

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.
 
A

Alf P. Steinbach

* 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 = 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.
 
M

Mike Wahler

arnuld said:
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 = 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
 
A

arnuld

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[]"
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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 = 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.
 
B

Bo Persson

arnuld wrote:
::
::
::: 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
 
A

arnuld

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 = 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.
 
A

arnuld

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.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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 = 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,150
Members
46,697
Latest member
AugustNabo

Latest Threads

Top