473,732 Members | 2,171 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 #1
14 2476
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<iostre am>
#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...@com Acast.netwrote:
WHY? Why 'char*'? Why couldn't this also be 'std::string'?
BECAUSE...Becau se ... ;-)

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

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

BECAUSE...Becau se ... ;-)

i have not across "std::strin g" 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<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;
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...@com Acast.netwrote:
::
::: WHY? Why 'char*'? Why couldn't this also be 'std::string'?
::
:: BECAUSE...Becau se ... ;-)
::
:: i have not across "std::strin g" yet. presently at Stroustrup chpater
:: 5.

So PLEASE read Chapter 20 real SOON.
You know we told you to read "Accelerate d 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.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.


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<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;
}
'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*********@gm ail.comwrote in message
news:11******** *************@d 57g2000hsg.goog legroups.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<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;
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.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];
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_word s[]"
Apr 3 '07 #10

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

Similar topics

26
3147
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
2349
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
1752
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
1757
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
5336
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
2370
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
1841
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
3041
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
2450
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
8774
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
9447
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9235
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9181
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
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...
1
6735
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
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
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2180
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.