array question

This is a discussion on array question within the C++ Programming forums, part of the General Programming Boards category; Can you tell me why it won't print my array to the screen? Compared to examples in my book, it ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    9

    array question

    Can you tell me why it won't print my array to the screen? Compared to examples in my book, it should work. I don't get it.

    if ((Prefix == "AA") && (Description == "MC"))
    {
    DocsBelongMCDR_Out << Prefix << DocAreaCode2 << Description << endl; // test output

    string arrayDocAreaCode2[3];
    int i;
    for (i=0; i<3; i++)
    {
    cout << arrayDocAreaCode2[i];
    }
    }

  2. #2
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    like always it would help if we would see more code...
    but i'll try to work with what we have....

    the first thing that comes to my mind might be that the "if" statement never gets executed perhaps BOTH of these conditions don't evalute as "true"

    the first couple lines don't mean anything to me...
    the way you want to display an array is fine, i am very certain that the problem exists somewhere else....perhaps prior to you entering the "if" statement

    ps. more code would be helpfull

    Regards,
    matheo917

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    9
    Here is the complete test file. Thanks in advance.

    #include <fstream> // For file i/o
    #include <iomanip> // For cout manipulation
    #include <iostream> // For cout
    #include <stdlib.h> // For exit
    #include <string> // For elements of type char
    #include <stdio.h>

    using namespace std;

    int main ()
    {
    ifstream Code_Detail;
    ofstream DocsBelongMCDR_Out;
    string line;
    string Prefix, DocAreaCode2, Description; // Strings for Code_Detail

    Code_Detail.open ("Code_Detail.txt");
    if (Code_Detail.fail ())
    {
    cout << "Cannot Open File: " << endl;
    return EXIT_FAILURE;
    }


    DocsBelongMCDR_Out.open ("DocsBelongMCDR_Out.txt");
    if (DocsBelongMCDR_Out.fail ())
    {
    cout << "Cannot Create File: " << endl;
    return EXIT_FAILURE;
    }

    while (Code_Detail.good ())
    {
    getline(Code_Detail, line);
    Prefix=line.substr(0,2);
    DocAreaCode2=line.substr(2,2);
    Description=line.substr(4,10);

    if ((Prefix == "AA") && (Description == "MC"))
    {
    DocsBelongMCDR_Out << Prefix << DocAreaCode2 << Description << endl; // test output

    string arrayDocAreaCode2[3];
    int i;
    for (i=0; i<3; i++)
    {
    cout << arrayDocAreaCode2[i];

    }
    }

    }

    Code_Detail.close ();
    DocsBelongMCDR_Out.close ();

    return EXIT_SUCCESS;

    }

  4. #4
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    as a matter of fact i just answered this post under another name.... maybe that will help you...

    and another thing......
    in your last "if" statement you are comparing (Description) if it equals to "MC" unfortunatelly "MC" contains two (2) characters and Description doesn't so they would never equal unless you just would get lucky.... line.substr(4,10); starts at the fourth character and takes ten (10)....

    try line.substr(4,2); ............

    hope that helps,

    Regards,
    matheo917

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    9
    You the Man!

  6. #6
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    anytime....

    good luck..

    Regards,
    matheo917

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic Mutli dimensional Array question.
    By fatdunky in forum C Programming
    Replies: 6
    Last Post: 02-22-2006, 06:07 PM
  2. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. array question?
    By correlcj in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 05:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21