Hey all I am writing this program and I am trying to read in a group of names from a file example:

Peter
Mike
Juan
Valentino
Stephanie

I need to read each name one by one and look at for example the first one P then compare it to see if it is one of a group of suggested letters to perform manipulations on.

Suggested letters;
A,B,C,F,P,N,R,T,

Values of letters A=1,B=2,C=3, ...Z=26;

for example if A is in the word then the value of A is 1 and times it by 2, so new value is 2.

so in the first name Peter, P is one of the letters so perform a given task, and E is not so value is 0, and T is so do a certain manipulation, E is not so 0, and R is so perform a manipulation.

When ever i read in the strings i can get it to read in the whole name or just the first letter, but not each letter individually.

Any help with this would be great I think I can perform each manipulation to find the new value of the letter I just can't seem to get each name broken up into individual letters.

Here is what I have so far:

int main()
{
	time_t t;
	time(&t);

	yl;
	cls;
	
    string name;
    int len;
    
    if (!infile) 
    {
        cout << "An error has occurred while opening the file." << endl;

        frz;
        exit(1);
    }  
    
while (!infile.eof())   
  {
    int i=0;
    infile >> name;
                 
    if (i < name.length())
    {
        cout << name[i] << endl;
        i++;
    }
}

Thank you very much for your help.


Any posting help you can give me would be a great help

Salem commented: Yet another failure to use code tags I see... -2

>>while (!infile.eof())
This is wrong -- don't use eof() because it doesn't work the way you would think it should. Here is a better way of doing it

while( infile >> name)
{

}

>>if (i < name.length())
In your code the value of i (which you initialize to 0) will NEVER ever be less than the length of name, even when name is an empty string.

If you want to check the first character of name against any character is a specific string

std::string teststring = "ABCFPNRT"
if( teststring.find(name[0]) != std::npos)
{
   // found it!
}

1) use code tags when posting code to this board
2) You don't need these:

time_t t;
time(&t);

yl;
cls;

so I'd get them out of there to avoid polluting the landscape.

3) Don't use the return value of eof() to control loops. Sooner or later it's going to cause the loop to run one to many times, and that may crash your program if not corrupt your data.

4) Use a loop intead of an if statement to evaluate every letter of a given name. If only evaluates a single letter. A loop can evaluate them all, one at a time.

commented: It doesn't matter how many times you say code tags, the OP simply doesn't get it. IOW, a WOMBAT +9

I am still working on this program and have all of the calculations basicallly laid out.

I just keep having trouble, I read in the name in the file;

in file;
tom
mike
al
rob

I perform that calculations but it will only perform the given calcuations on the first letter such as T in the first name and then will automatically jump to next name in list.

My question is how do I get it to analyze a specific name by looking into the name and comparing every letter in that name to a list that I have for calculations to be made too.

I need to read in the names and seperate them into individual letters and look at one by one. Any help you can give would be greatly appreicated.

Thanks,

so lets say you have an array of names and you want to itterate through all of the names and each letter in the name

string allnames[20];  //lets say we have 20 names
 
for(int i = 0; i < 20; ++i)
{
  //get the length of the names
int namelength = allnames[i].size();
string currentname = allnames[i];
for(int n = 0; n < namelength; ++n)
{
   currentname[n]; //do your magic
   //this line actually grabs the character at the n'th position
}
}

Killer Typo

Thanks for the help. It seems to be working the way I input everything, and I had to make some changes but it is still giving me one error.

for this:

int len = allnames[ij].length();

and i tried what you had

int len = allnames[ij].size();

and it keeps saying

error 1> 'length' has not been declared
error 2> request for member of nonaggregate type before (


Thanks for the help

try this method instead. worked much better for me

#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
using namespace std;
int main ()
{
   vector<string> names(2);
   names[0] = "robert";
   names[1] = "Michael";
   for (int i = 0; i < int(names.size()); ++i)
   {
      for (int n = 0; n < int(names[i].size()); ++n)
      {
         cout << (names[i]).at(n) << endl;  //outputs each letter in each name on a new line just for testing purposes
      }
   }
}
Member Avatar for iamthwee

No containers (vectors) or the like are necessary.

No containers (vectors) or the like are necessary.

maybe not, but i happen to have a project already open where i was using vectors so i wrote this while i was in the middle of my project.

the same logic could easily be applied to standard arrays ;)

Member Avatar for iamthwee

standard arrays are not needed either.

standard arrays are not needed either.

yay, good for you now post something productive or stop harping.

Member Avatar for iamthwee

But it's true. :(

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.