Output syntax

Hello everyone,
I have a code outputs this:

"Enter a search word: Searching for the word [volleyball] ...
[Love sought is good, but given unsought is
better.][volleyball]Found! There were 2 words between
occurrences of volleyball."

I need it to output this:

Enter a search word: Searching for the word [volleyball]...
[Love][sought][is][good,][but][given][unsought][is][better.]Found!There
were 9 words between occurrences of "volleyball".

Heres my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string>
using namespace std;
int main()
{
	
	string a, b;
	cout << "Enter a search word: ";
	cin >> b;
	cout << "Searching for the word [" << b << "] ... "; 
	int count;
	cin.ignore();
	for(count = 0; a != b; count++)
	{
		cin >> a;
		cout << "[" << a << "]";
	}
	cout << "Found! There were " << count << " words between occurrences of " << b << ".";
}


How do I get the program to bracket individual words?
Hmm? it works for me
That's what I thought, but obviously something is not correct in the code.
I need each word to get individually bracketted when submitted.
I don't think I understand..

1. user enters word to search for
2. terminal shows users search word
3. user enters example phrase
4. terminal shows user example phrase
5. terminal shows user example phrase with brackets around each word
6. terminal finds word/terminal waits for new word

you want to get rid/modify 4? you can't, not that I know of.
Can you use an INT for variable a? Then maybe it would grab each word individually? If you could how would you modify the for loop to do that?
You would need to create a parsing algorithm that recognizes and "extracts" each word indevidually into a vector/array. From there you can search for an indevidual word (like vollyball), and have it display each word in brackets until it comes to the word. After that, the vector/array number that correlates with that word being searched for can be used to calculate how many words are inbetween the word being searched for, and the start of the sentence/file.
___________________________________________________________
To get you started on the algorithm:

Markers which speicfy a word:

space before OR after; space before OR a punctuation after.
we use 'OR', because after a word, there can be either a space, or punctuation.

treat the string like an array: STRING[x]

use the size() function: string.size() or vector.size() = the total number of characters/(strings, numbers etc...) in any given string/vector.
____________________________________________________________

Now, if I were you, i would write the algorithm into 1 function, and make that function use the address of the varaible arguments. ex:
 
void find_word(string& sentence, unsigned int& number_of_instances, unsigned int& number_of_separatingwords)


This will directly modify the variable argumants given to it by the function which called it. This technique is very usefull if you want to return more than 1 variable.

This is a lot of work, but it will prove to be extremely more reliable, as it can recognize what is a ward, and what isn't and sort them out. You can make it display the sentence, and then the data which is relevant to that sentence (such as the number of words.)

Last edited on
that isn't the problem, when ever you type an input that input will appear without being modified because it is written directly to the terminal, the only exception I can think of is getch, but that only gets a char, not a string.
Why not make string a into an array, and everytime it loops the word is placed into the array? then could you just output all the elements of the array using a loop? This way you could put [] around each word. (at least thats what I think your trying to do...)
PS. what does cin.ignore() do?
@Zephilinox

Then just do this:

in a for loop:

cout<< "["<< var1<< "]"<< endl;

It's really simple.
Either I am really stupid or you guys don't seem to get it.

the program works, when you input a phrase it then reads it back to you with each word in brackets, but you can't format your input with brackets.
so you want it to read the sentence you input with brackets around each word? If thats what you want, this will do it.

1
2
3
4
5
6
7
8
9
10
int main()
{
    int x=0;
    string text[100];
    while(1){
        cin >> text[x];
        cout << "[" << text[x] << "] ";
        x++;
    }
}


the while loop runs forever :)
Last edited on
Works fine too.
that doesn't work, because if you enter "hello" and press enter, you can see both the word "hello" and "[hello]" in the terminal, what he wants is to only see "[hello]" but that isn't possible (using the stl at least) that I know of.

try compiling his code before suggesting something that does the exact same thing, it works fine, just not the way he wants.
Hello,
I tried to run this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
using namespace std;
int main()
{
	
	int a;
	string text[100], b;
	cout << "Enter a search word: ";
	cin >> b;
	cout << "Searching for the word [" << b << "] ... "; 
	int count;
	cin.ignore();
	for(count = 0; text[100] != b; count++)
	{
		cin >> text[a];
		cout << "[" << text[a] << "]";
	}
	cout << "Found! There were " << count << " words between occurrences of " << b << ".";
}


but windows crashes the program.
It is probably crashing because the variable a isn't initialized. I think you might want to swap 'a' out with 'count'.
then just System("CLS"); before makeing it display it in backets.
Topic archived. No new replies allowed.