Help reading multiple inputs...

Alright so I'm supposed to do something like this:
 
cout<<"Please enter some sentences below. A word '@@@' terminates the input.\n";

then input something like:
Blah Blah I'm a Banana @@@


My question is how do I read each word individually?

if I use cin, when I cout the input, there's no whitespaces
if I use getline, Everything is together including whitespace.
Last edited on
If you only want to read the words, then why do you need the whitespaces?
I need to save the words into my struct/class ...
and I thought if I used cin then it would just take the whole input as one word and I need to save each individual word ;

For example, if I wanted to save
my name is apple
using cin, wouldn't my output be
 mynameisapple
?I don't want that because that input is 4 words, not 1
Last edited on
Is this what you are looking for??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>
#include <vector>   //This is only for vector
using namespace std;

int main()
{
    string s;
    vector<string> v;
    cout<<"Please enter some sentences below. A word '@@@' terminates the input.\n";
    while (true)
    {
        cin >> s;
        if (s=="@@@") break;
        //Do whatever you want to do with string. In this case I'm adding it
        //in vector
        v.push_back(s);
    }
    cout << endl<<"The words you entered are"<<endl;
    //Now show all the strings
    vector<string>::iterator i;
    for (i=v.begin();i!=v.end();i++)
        cout << *i<<endl;
    return 0;
}



Please enter some sentences below. A word '@@@' terminates the input.
Blah Blah I'm a Banana @@@ 

The words you entered are
Blah
Blah
I'm
a
Banana
Yeah, it's something like that xD Except I can only use

 Classes
 Array of structures
 String class
 Sorting
and previous stuff I learned but I haven't learned vectors yet :P .

Do you know how classes work? Or where I can learn about classes ?

My book doesn't explain it clearly enough to me because I have no idea what classes do o_O
Hey, of course I know how classes work and if your book doesn't explain then you can always look for help. And that's what Google is there for. Isn't it?? But I think there are a lot of tutorials on this site alone. Here, take a look at it.

http://www.cplusplus.com/doc/tutorial/classes/

And there are more too. You just need to search.
But learning vectors is also a very good idea. I find them pretty useful you know.
As much as I'd love to learn about vectors, because my Prof hasn't covered it in class, I wouldn't be able to use it in my programming because my assignments have strict requirements as to what I can and cannot use. (Like, my last assignment I could use C-strings but this assignment I can only use strings)

So, learning about vectors would be useless until my prof covered them in class, if he even is going to --.

Oh and thanks for the site -- I'll look into it (:
Last edited on
Topic archived. No new replies allowed.