Hi ive made a program that does 3 things.
1. lets you enter 2 words to form one word ( red + cat = redcat ).
2. opens a separate txt file.
3. checks what you entered is the same as one of the words in the separate txt file.
here is my code:

#include <iostream>
#include <fstream>
using namespace std;

class animals
{
      public:
      string name;         
};

int main(){

    string x;
    string y;
    string z;
    cout<<"firstword"<<endl;
    cin>>x;
    cout<<"secondword"<<endl;
    cin>>y;
    z=x+y;
    cout<<z<<endl;

    animals array[10];
    ifstream infile;
    infile.open("C:\\animals.txt");
    while(infile.peek()!=EOF){
       infile
       >>array[0].name; 
    }
    infile.close();

    if(z==array[0].name)
    cout<<"correct"<<endl;
    else
    cout<<"wrong"<<endl;

    cout<<array[0].name;
    system("pause");
    return 0;    
}

what it does however, is only checks for the array 0 ( so in this case only one word out of three ).
how would i go about changing or fixing it to make it check so that ANY of the three words that matches up with what the user types down is correct.(not just the array 0 word).

here is the txt file

redcat
bluedog
greenbird

as you can see the user must match a colour with the animal to be correct. however the only code that reads out to be correct is at current, the green bird. I know doing array 0 is wrong but i dont know how to fix it.
any help is greatly appreciated.

nevermind figured it out. had to change while loop into for loop. then i used the for loop to first read all strings, then check if they match until there are none left.

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.