[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

How can I loop versus hardcode?

Posted on 2003-11-18
5
Medium Priority
?
520 Views
Last Modified: 2008-02-01
I found and modified a simple C++ program to connect to and create a recordset from a database.  I then write out each record to a text file.  I use the fprintf command and it works fine but I currently have hard-coded in the fprintf line 36 times (see code below) because I don't know how to use a for loop (or something like it).  It would make the code nicer and more professional (at least that's my impression).  Can anyone help?

current code:

#include <stdio.h>
#include <afxdisp.h>

#import "c:\program files\common files\system\ado\msado15.dll" rename ("EOF","adoEOF") no_namespace

#define CREATEiNSTANCE(sp,riid) { HRESULT _hr =sp .CreateInstance( __uuidof( riid ) ); \
                                  if (FAILED(_hr)) _com_issue_error(_hr); }

#define RsITEM(rs,x) rs->Fields->Item[_variant_t(x)]->Value
#define UC (char *)
struct      InitOle {
            InitOle()  { ::CoInitialize(NULL); }
            ~InitOle() { ::CoUninitialize();   }
} _init_InitOle_;       // Global Instance to force load/unload of OLE

void main(){

    _RecordsetPtr   spRS;
    _ConnectionPtr  spCON;
      FILE *fout;
      char csvfile[17] = "pdd_criteria.csv";

      if((fout = fopen(csvfile,"w")) == NULL)
            printf("cannot open out file !\n");
      
    try{
        CREATEiNSTANCE(spCON,Connection);
                  
                  // connect to local hmi computer
                  
                  spCON->ConnectionString =L"DRIVER={Microsoft Access Driver (*.mdb)};"
                                                       L"DBQ=ppg_2000.MDB;DefaultDir=C:\\test;";            
                  
                  // connect to process computer
                  spCON->ConnectionString = L"DSN=tpd;"
                                              L"UID=tpd; PWD=tpdtpd;";
                                
        spCON->Open( "", "", "", -1 );
        CREATEiNSTANCE(spRS,Recordset)
        spRS->PutRefActiveConnection( spCON );

                        
            spRS->Open("select * from products where bf_max_qty > 0 order by 1 desc", vtMissing, adOpenKeyset, adLockBatchOptimistic, -1);
       
        while(spRS->adoEOF == false){

            printf("productcode = %s  proddesc = %s \n", UC _bstr_t(RsITEM(spRS,0L)),
                                                 UC _bstr_t(RsITEM(spRS,"bobbintype")));

                        
                        // write string to out file
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,0L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,1L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,2L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,3L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,4L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,5L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,6L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,7L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,8L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,9L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,10L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,11L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,12L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,13L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,14L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,15L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,16L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,17L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,18L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,19L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,20L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,21L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,22L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,23L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,24L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,25L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,26L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,27L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,28L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,29L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,30L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,31L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,32L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,33L)));
                        fprintf(fout,"%s,", UC _bstr_t(RsITEM(spRS,34L)));
                        fprintf(fout,"%s, \n", UC _bstr_t(RsITEM(spRS,35L)));

                        

            spRS->MoveNext();
        }
        spRS->Close();
        spCON->Close();
       
    }
    catch( _com_error &e){
        _bstr_t bstrSource(e.Source());
        _bstr_t bs =  _bstr_t(" Error: ") + _bstr_t(e.Error()) + _bstr_t(" Msg: ")
            + _bstr_t(e.ErrorMessage()) + _bstr_t(" Description: ")
            + _bstr_t(e.Description());
       
        MessageBox(0,bs,bstrSource, MB_OK);
    }          
}
#undef UC
0
Comment
Question by:fgs3124
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 9772108
What about

for ( long l = 0; l < 36; ++l) {

    fprintf(fout,"%s, \n", UC _bstr_t(RsITEM(spRS,l)));
}

?
0
 

Author Comment

by:fgs3124
ID: 9772705
Sheever-me-timbers . . . it worked!  I tried using the for loop but I must have been doing something wrong.

Thank you.
0
 
LVL 86

Expert Comment

by:jkr
ID: 9772729
You are most welcome :o)
0
 

Expert Comment

by:FatNewbie
ID: 9777150
you can't just make people rewrite code for u.
or write some new stuff
0
 

Author Comment

by:fgs3124
ID: 9779173
What is that suppose to mean?  This is a help board and I needed help.  I wrote most of the code but got hung up on a piece of it.  That's the very reason this website exists!  I don't suppose the 'Fat' in your userid refers to what resides between your ears . . . ?
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question