GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Mar-2005, 10:11
krepunk krepunk is offline
New Member
 
Join Date: Mar 2005
Posts: 3
krepunk is on a distinguished road

help with arguments to main


CPP / C++ / C Code:
int main(int argc, char *argv[])
{
	char cFileName[] = "";
	char iFileName[] = "";
	char oFileName[] = "";

	bool correct = true;
	int test;
	int fileNames[3] = {-1,-1,-1};	

	if (argc != 7) {
		cout << "Not enough arguments\n";
		cout << "Program requires the following switches and arguments:\n";
		cout << "-c <constructionFileName> -i <initializationFileName> -o <operationsFileName>";
		return 1;
	}
	else {
		for (test=1;test<=6;test++){
			if (argv[test][0] == '-'){
				switch (argv[test][1]){
					case 'c':	if (fileNames[0]!=-1){
								correct = false;
								cout << "found " << argv[test] << " a second time." << endl;
							}
							else
								fileNames[0] = test + 1;
							//end-IF
							break;
					case 'i':	if (fileNames[1]!=-1){
								correct = false;
								cout << "found " << argv[test] << " a second time." << endl;
							}	
							else
								fileNames[1] = test + 1;
							//end-IF
							break;
					case 'o':	if (fileNames[2]!=-1){
								correct = false;
								cout << "found " << argv[test] << " a second time." << endl;
							}
							else
								fileNames[2] = test + 1;
							//end-IF
							break;
					default:	cout << "Invalid switch " << argv[test] << endl;
							correct = false;
				}//end-switch
			}
			else {
				correct = false;
				cout << "Invalid switch " << argv[test] << endl;
			}//end-IF
			test++;
		}//end-For
	}//end-IF

	if (!correct)
		return 1;
	else{
		strcpy(cFileName,argv[fileNames[0]]);
		cout << cFileName << " c" << endl;    // displays the correct filename
		strcpy(iFileName,argv[fileNames[1]]);
		cout << iFileName << " i" << endl;
		strcpy(oFileName,argv[fileNames[2]]);
		cout << oFileName << " o" << endl;
	}//end-IF

	ifstream cfile;
	cfile.open(cFileName);
        //cfile.close();

	if (!cfile) {
		cout << "Failed to open construction file\n";
		return 1;
	}

that is part of my code... and when i run the program, i get a "failed to open construction file", and when i replace the cFileName in cfile.open to "constFile.txt", i get an infinite loop elsewhere in the code.

but the problem must be in this section, because the program worked perfect before... with ifstream cfile("constFile.txt");
and then i added the argument stuff, and ive been getting all kinds of errors.

so yeah... please can you guys look at the code above, and tell me if you see any errors.

thanks!!!
  #2  
Old 16-Mar-2005, 13:44
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,200
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold
Quote:
Originally Posted by krepunk

but the problem must be in this section, because the program worked perfect before... with ifstream cfile("constFile.txt");
and then i added the argument stuff, and ive been getting all kinds of errors.

so yeah... please can you guys look at the code above, and tell me if you see any errors.

thanks!!!


Here's a suggestion:

When a program "works perfect before..." but doesn't work so good with the new stuff, break out the new stuff into a separate program and test it to see if you understand what's happening:

CPP / C++ / C Code:
#include <iostream>

using std::cout;
using std::endl;

int main(int argc, char *argv[])
{
  cout << "argc = " << argc << endl;
  
  for (int i = 0; i < argc; i++) {
    cout << "argv[" << i << "] : <" << argv[i] << ">" << endl;
  }

  return 0;
}

Once you get this under your cap, then you can put this loop at the beginning the main() of your big program to make sure it's getting the stuff you think.

Regards,

Dave
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Who Calls main and where does main returns (C/C++) Poolan C Programming Language 5 27-Oct-2004 11:00
main() arguments crystalattice C Programming Language 2 24-Oct-2004 01:05
Another problem dealing with main() and driver function tommy69 C Programming Language 4 20-Mar-2004 20:46
regarding main method jerry C++ Forum 18 09-Mar-2004 19:48

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 17:55.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.