2016-04-16 10 views
-4

こんにちは私はこのプログラムでいくつかの問題を抱えています。 forループの結果として1つの値しか表示されず、プログラムのdelete関数が機能しません。私はこれを別のやり方で考えていくのが難しいです。forループの1つの値を持つベクトルの内容を表示

// dvd title library 



#include <iostream> 
#include <vector> 
#include <string> 
#include <algorithm> 

using namespace std; 

int main() 
{ 
int adqv, ete, ti; 
vector<string> dvdtitle(101); 
vector<string>::const_iterator place; 
string title, second, third ; 


do{cout<< "welcome to your Dvd library!" 
&& cout<< "  To add titles enter 1.        To      delete titles enter 2.   Type 3 to quit" 

&& cin>>adqv; 


switch (adqv) 
{case 1 : cout<<"Enter the title name" 
&& cin >>title 
&& cout<<"Enter another title" 
&& cin>>second 
&& cout <<"Enter a third title" 
&&cin>>third; 
    break; 
case 2: 
cout<<"enter the number of the title to be deleted. starting with 0 then  1" 
&& cin>>ete; 

break; 
case 3: 
    {return 0;} 
default: cout<<"invalid choice";} 

dvdtitle.push_back(title); 
dvdtitle.push_back(second); 
dvdtitle.push_back(third); 
if (adqv=2) 
dvdtitle.erase(dvdtitle.begin()+ete); 

sort(dvdtitle.begin(), dvdtitle.end()); 

    for (place = dvdtitle.begin(); place< dvdtitle.end() ; ++place); 

    { cout<< "    These are your titles   "; 
    cout<< *place; 



    } 


}while (adqv !=3); 

} 
+0

_「問題があります」_はかなり曖昧なエラーの説明です。投稿[MCVE]してください。 –

+0

そのフォーマットで何を期待しましたか?それはあなたのチャンスを維持しています。 – LogicStuff

+0

*ベクトル dvdtitle(101); *はベクトルに101個の要素を作成し、* dvdtitle.push_back(title); *は102要素というように追加し、次に* dvdtitle.erase(dvdtitle.begin ()+ ete); *あなたが考えると思われる要素は消去されません。 –

答えて

1

ラインfor (place = dvdtitle.begin(); place< dvdtitle.end() ; ++place);

セミコロンを含めるべきではありません ';'最後に。これはループの空の本体として機能します。ループが終了すると、placedvdtitle.end()となります。これは、後で印刷しようとすると、ベクトルの境界から外れます。

+0

ああ、それを説明してくれてありがとう! – Lars

+0

delete関数を使用すると、削除する代わりに削除するタイトルのコピーを作成します – Lars

+0

あなたは常に 'dvdtitle.push_back(title);というコードを実行します。 dvdtitle.push_back(秒); dvdtitle.push_back(3番目); 'どのような選択が行われたかにかかわらず。したがって、ユーザーがアイテムを削除することを選択した場合でも、3つのアイテムを追加します。 – Unimportant

関連する問題