Start New Discussion within our Software Development Community

I am running into an issue trying to get the program to read in my data correctly. The program is supposed to take a data file and read in names, if the string is preceeded by "surname" or "lastname" then the string lastname is set to the next string, if it isn't preceeded by that then string firstname is set to the string, when I run the program all it does is set all the values to the last name and prints out first name with no data, any pointers?

#include <iostream>
 #include <string>
 #include <fstream>

 using namespace std;

 ifstream indata;
 string f[100];
 string l[100];
 void readindata(string first[], string last[]);
 void formatnames(string fname[], string lname[]);

 int main(int argc, char *argv[]){
    indata.open("namesLast.txt");
    if(!indata){
        cout << "ERROR!!! The file did not open.";
        return 1;
    }
    readindata(f, l);
 }


void readindata(string first[], string last[]){
    int i;
    string nametag;
    for(i = 0;!indata.eof(); i++){
        if(nametag == "lastname" || "surname"){
            indata >> last[i +1];
            cout << "Last name is: " << last[i] << endl;
        }
        else if(nametag != "lastname" || "surname"){
            indata >> first[i];
            cout << "First name is: " << first[i] << endl;
        }
    }
}

Lines 27 and 31 are incorrect. You have

if(nametag == "lastname" || "surname")
// which translates into
if(nametag == "lastname" || true)
// which translates into
if (true)

What you need to do is

if(nametag == "lastname" || nametag == "surname")
This article has been dead for over six months. Start a new discussion instead.