2012-05-07 13 views
0

コマンドライン引数を使って実行するプログラムを実行しました。私は今、 "改善"の一環としてメニュードライブプログラムをしようとしています。コマンドライン引数のプログラムをメニュー駆動型プログラムに変換する

char * startCity = argv[1]; 
char * endCity = argv[2]; 
in.open(argv[3],ios::in); //<----file name went here 

ここで私は今何をしたかと私はそれが間違っている知っている:私のプログラムのすべて以来

int main(int argc, char * argv[]){ 

int menuChoice; 
string startCity; 
string endCity; 
string fileName; 
ifstream in; 

cout<<"Welcome to J.A.C. P2\n" 
    "\n" 
    "This program will find the shortest path\n" 
    "from One city to all other cities if there\n" 
    "is a connecting node, find the shortest path\n" 
    "between two cities or find the shortest\n" 
    "between three or more cities.\n"<<endl; 

cout<<"Please make a choice of what you would like to do:\n"<<endl; 

cout<<" 1------> Shortest Path between 2 cities.\n" 
     " 2------> Shortest Path between 3 or more cities.\n" 
     " 3------> Shortest Path from 1 city to all.\n" 
     " 9------> Take your ball and go home!\n"<<endl; 
cout<<"Waiting on you: "; cin>>menuChoice; 

switch (menuChoice) { 
    case 1: 
     cout<<"Enter the starting city: "; 
     cin>>StartCity; 
     cout<<"\nEnter the ending city: "; 
     cin>>EndCity; 
     cout<<"\nEnter the name of the file: "; 
     cin>> fileName; 

    break; 

私はarguementsがいたところ、元の中

int main(int argc, char * argv[]) 

は、使用しましたchar * argv []どのように文字列に変換できますか?またはそれらを読み込むためにどのように変数を代入することができますか?

私はすべての答えに感謝しますが、私は離れようとしている方向に向かっているようです。 OLDプログラムはコマンドライン引数を使用していました。どうすればいいですか:

string StartCity = char * argv[1]; 
string EndCity = char * agrv[2]; 
string filename = in.open(argv[3],ios::in); 

これは私がやろうとしていることです。私が自分自身を明確にしなかった場合、私は残念です。

+0

'string s = argv [0];'これは引数の変換の意味ですか? – chris

+0

私はOPが他の方法を望んでいると思います。 'char * = sting s' – gardian06

+0

@chrisいいえ、元の質問を編集しましたが、実際には私はやってみたいと思っています。 –

答えて

4

これは役に立ちます。

int main (int argc, char ** argv) 
{ 
      std::vector<std::string> params(argv, argv + argc);  
      //Now you can use the command line arguments params[0], params[1] ... 

} 
+2

ベクトルに変換するためのショートカットが好きです:) – matiu

+0

他の方法で投稿を編集する必要があります。 –

0

文字列にコマンドライン引数を変換する:

std::vector<std::string> args(argc); 
for (int i=1; i<argc; ++i) 
    args[i] = argv[i]; 

'0' プログラム名ですので、私は '1' で開始します。


VARSにそれらを取得するには、おそらく:

// Make sure we're not accessing past the end of the array. 
if (argc != 4) { 
    std::cout << "Please enter three command line arguments" << std::endl; 
    return 1; 
} 

string startCity = argv[1]; 
string endCity = argv[2]; 
string fileName = argv[3];  
0

からのは、std::string::c_str()を使用します。

fstream file; 
string s = "path\\file.txt"; 
file.open (s.c_str(), ios::in); 
+0

それ以外の方法が必要です。元の投稿を編集しました。 –

+0

あなたが何を意味するのか分かりません。他の唯一の方法は、コメントで言及した 'string s = some_char_array;'のようなものです。 – chris

+0

私の投稿編集済みの投稿がより明確になることを願っています。 –

関連する問題