0

I'm working on a program that reads a line of code and stores each word into an index or a array of string and I'm stuck. Its a small part of a larger project I must used cin.getline:

#include <iostream>
#include <string>
#include <cstring>
#include <cctype>
#include <iomanip>
using namespace std;
int main ()
{
char anything;
sting word[3000];

while (!cin.eof())
{
cin.getline(anything);
line++;

(somehow I have to put each word from the line into its own index of the string array and I am so lost on how to do that)
}
}
4
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by griswolf
0

There is some problems with your code, right now I'm too lazy to point them out and try to explain it all. Instead I will give you a push in the right direction to achieve what you want to do. First you want to make use of std::string and std::vector. std::string are a more powerful char arrays, and std::vector is a more powerful generic arrays. Before I say more, how does the input look like?

0

We are not suppose to use vectors or any container classes. The output is suppose to look like this

21 58 348
Total 48 different characters, 5 most used characters:
No. 0:                  44
No. 1: \t               29
No. 2: e                24
No. 3: a                23
No. 4: \n               21

Total 46 different identifiers, 5 most used identifiers:
No. 0: the              2
No. 1: a                2
No. 2: of               2
No. 3: will             2
No. 4: on               1

Total 6 different numbers, 5 most used numbers:
No. 0: 1                2
No. 1: 5                2
No. 2: 2                1
No. 3: 3                1
No. 4: 83               1

its suppose to get some input and compute the amount of line, words, and characters. Then its to fine the top 5 most used characters, lines, and numbers. My my question is how can I go about accessing the each individual character within that line, because I have to make a count of which ones are numbers or not, so should I use cin.get()?

Edited by ana_1234: n/a

0

Just like firstPerson I suggest using the string type instead of char. To access each individual character you can use the following syntax:

string line;
char firstchar = line[0];

You can use a loop to get each char. Something like:

for(int i=0; i < line.size(); i++)
{
char = linepp[i];
}

Then you can store each letter, special char or number in differents arrays. Of course that's incomplete but I hope you get the point.

Also I'm a bit unsure on what you are trying to do with this while loop:

while (!cin.eof())

Are the line / sentences user inputs or you are reading from a file?

Anyway in both cases the method I suggested above should work.. but it's not really clear what you are trying to do.

1

Is std::string considered a 'container class' by your prof? If not, then the recommendation to use it is very good. If so, then I hope you are in an advanced C++ class, because beginners should be given full use of all the tools. Otherwise they should be taking a C class instead. It is very wrong-headed to try to teach C++ 'from the ground up' to folks without much programming experience. The whole point of C++ is to make powerful and useful constructs easily available.

If you really have to work with (const) char* lines Look in the C forum. For instance: http://www.daniweb.com/forums/thread77052.html

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.