473,761 Members | 4,082 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<iostre am>
#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
14 2479
On 3 Apr, 13:29, "arnuld" <geek.arn...@gm ail.comwrote:
On Apr 3, 12:02 am, "Alf P. Steinbach" <a...@start.now rote:
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<iostre am>

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.dkwro te:
::
::
::: So PLEASE read Chapter 20 real SOON.
::
:: OK.
::
::: You know we told you to read "Accelerate d C++". There you have
::: Chapter 1 "Working with strings". :-)
::
:: i remember you advised "Accelerate d 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.sewro te:

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(con st 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.dkwro te:
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 "Accelerate d C++" both of which
are not available in my country :-(
but a book named "Essential C++" bt Lippman is available which looks
like "Accelerate d 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.sewro te:

>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(con st 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
3152
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 had done myself a disservice by having not attempted and completed the exercises. I intend to rectify that. My current routine is to read a chapter and then attempt every exercise problem, and I find that this process is leading to a greater...
7
2350
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? which calls cause the compiler to to introduce a temporary variable? solution: this is the code ----------------------------------------------------------- #include <iostream> void f(char) {};
0
1753
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 opposite as an exercise which is writing it as a "declaration without definition". please check whether i am right or wrong:
0
1758
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 // printing the sizes of different types
2
5339
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 -------------- /* Stroustrup 3e, section 4.11, exercise 5 STATEMENT:
16
2374
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 the types: unsigned char
11
1844
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 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
6
3043
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 table of the name sof months o fyear and the number of days in each month. write out that table. Do this twice:
27
2457
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 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
0
9522
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9336
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9948
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8770
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6603
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5364
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3866
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2738
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.