-
January 5th, 2009, 06:00 PM
#1
[RESOLVED] Question about infinite word/phrase input, terminated by a specific word
Hey everyone. I'm new here, and I'm kinda newbie with C++. So, here's my question.
I want to make a program in C++ which receives phrases by the user, strings or char vectors, not decided which one to use yet. So, I want it to use a pointer, but not sure exactly how to program it. My idea is that there'll be a cicle which creates a new vector or a string with the pointer for each next word.... And the cicle ends when I write END.
To picture it a little more explicit:
Program example input:
(enter is for when the user presses enter)
Start inputing words:
Word (enter)
This is a word (enter)
this is not a word (enter)
It's a phrase (enter)
END (enter)
So, I'm not really sure if this can be done in C++, I mean I can do it with an array, but the size of it is fixed, so I can't really input an infinite list of words of phrases.
Any ideas?
Thanks in advance
PS: It's a console program, not a graphic one, I'm not there yet
Last edited by gtbgmaniak; January 5th, 2009 at 06:01 PM.
Reason: Add PS
-
January 5th, 2009, 06:05 PM
#2
Re: Question about infinite word/phrase input, terminated by a specific word
Curious that you used the word "vector" in your description, since that's exactly what you need....a vector of strings.
-
January 5th, 2009, 06:14 PM
#3
Re: Question about infinite word/phrase input, terminated by a specific word
So, you're saying I should learn how to use the vector library?
What about the infinite cycle, terminated by "END"?
-
January 6th, 2009, 01:19 PM
#4
Re: Question about infinite word/phrase input, terminated by a specific word
So, what you basically mean is a vector in a cycle?
-
January 6th, 2009, 01:22 PM
#5
Re: Question about infinite word/phrase input, terminated by a specific word
I mean the idea of the program is:
The user starts inputting words or phrases, the word or phrase ends when the user presses enter. The program will keep asking for a word or a phrase until the user writes the word 'END'. So, the other thing is it should be dynamically assigned. That's my problem, can you help me?
-
January 6th, 2009, 01:32 PM
#6
Re: Question about infinite word/phrase input, terminated by a specific word
You seem to know what needs to be done; what precisely is your problem in just doing it?
-
January 6th, 2009, 02:49 PM
#7
Re: Question about infinite word/phrase input, terminated by a specific word
Could you put an example with C++ code, because I don't really know how to use vectors yet. I mean I did something similar with an array but it was finite. I have never used vector before, I more or less understand what it is, but never really used it.
-
January 6th, 2009, 03:07 PM
#8
Re: Question about infinite word/phrase input, terminated by a specific word
Why don't you post your array code, and I'll show you how to modify it to use a vector. (A vector is basically just a wrapper around a dynamic array.)
-
January 6th, 2009, 05:55 PM
#9
Re: Question about infinite word/phrase input, terminated by a specific word
Code:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <string>
const int MAX=255;
int main(){
char phrase[MAX];
char phrases[MAX][MAX];
cout<<"Please write all the phrases you want."<<endl;
cout<<"To finish write \"END\""<<endl;
cout<<endl;
int x=0;
char terminator[]="END";
char temp[MAX];
for(int i=0;i<MAX;i++){
++x;
cin.getline(phrase,MAX);
strcpy(temp,phrase);
strcpy(&phrases[i][0],phrase);
if (strcmp(temp,terminator)==0)
break;
}
return 0;
}
Here it is. It's only working for 255 phrases... I know they are pretty much but I need the infinite version of this
-
January 6th, 2009, 06:04 PM
#10
Re: Question about infinite word/phrase input, terminated by a specific word
Code:
#include <iostream>
#include <string>
#include <vector>
using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::vector;
using std::getline;
int main(){
string phrase;
vector<string> phrases;
cout<<"Please write all the phrases you want."<<endl;
cout<<"To finish write \"END\""<<endl;
cout<<endl;
char terminator[]="END";
while (phrase != terminator){
getline(cin,phrase);//note we are use the std::string version of getline, not istream::getline.
phrases.push_back(phrase);
}
return 0;
}
Here's the update. If you want to avoid adding "END" to phrases, you can just put a pop_back after the loop, or an if statement around the push_back.
The code will be more efficient if you can .reserve() a certain number of phrases you think you're likely to get beforehand.
Last edited by Lindley; January 6th, 2009 at 06:06 PM.
-
January 6th, 2009, 06:49 PM
#11
Re: Question about infinite word/phrase input, terminated by a specific word
That was just what I needed, put in a very easy to understand way.
Thanks a lot Lindley!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|