Hello,

I've got a simple program where i'm trying to a Char array with a string, but even though the program in debug mode shows that the string Phone = Phone, it is not adding a new line when the string starts with Phone. Can anyone tell me why the comparison is not working?

Thank you,
Art

This is the text file, i'm using.
2010#139648 AMERICAS BEST HOUSE PLANS INC MARK HALL 11/11/10
AMERICAS BEST HOUSE PLANS INC P O BOX 671274 SIC 874102
Phone - 770-479-5941 MARIETTA, GA 30066
2010#139609 ASKARI ENTERPRISES LLC STEVE ASKARI 11/08/10
ASKARI ENTERPRISES LLC 4880 LOWER ROSWELL RD 165 SIC 874101
Phone - 404-444-8995 MARIETTA, GA 30068
2010#139644 C0ZZZY COMFY MICHAEL R TYLDESLEY 11/10/10
1914 LEACROFT CT SIC 596312
Phone - 770-856-6452 MARIETTA, GA 30062
2010#139614 CHASE TOWING & RECOVERY BROOK JOHNSON 11/08/10
5025 AUSTELL RD SIC 874101

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

using namespace std;

int main(int argc, char *argv[])
{
    
    const int SIZE = 81;
    char input[SIZE];
    int strLen;
    string theString ="Phone";
    char str1[]= "To be or not to be";
    char phone[5];
   

    
    fstream nameFile;
    fstream outFile("Clearparsed2.txt",ios::out | ios::binary);
    
    //open file in input mode
    nameFile.open("clear.txt",ios::in);
    if (!nameFile)
    {
                  cout << "Error: Cannot open file. \n";
                  return 0;
}
//Read the file contents.
nameFile.getline(input, SIZE); 
while (!nameFile.eof())
{
      cout << input << endl;
      outFile << input;
      strncpy(phone,input,5);
      if (strcmp(phone,"Phone") == 0)
     // if (phone == "Ph")
      
        outFile << "\n";
      else
        outFile << "\t";
         
      //outFile << "\t";
      nameFile.getline(input, SIZE);
      }
      //Close the file
      nameFile.close();
      return 0;
      
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
;

line 16: variable phone is too small. strncpy() must have enough room for NULL terminator. Must be at least 6 characters, not 5.

Since you are wriing a c++ program why don't you replace C character arrays with std::string? std::string is a lot easier to work with than character arrays, and you can use == operator to compare std::string with char* (string literal).

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.