ユーザー入力文字列を解析して配列に変換しようとしています。 Example: user inputs "hello to you" array[0]="hello" array[1]="to" array[2]="you'
いくつかの単語を入力するようにユーザーに指示した後、プログラムは無限ループするようです。私もベクトルを使ってみましたので、別の領域で私のロジックかもしれません。私はC言語になると非常に錆びているので、私の無知を許してください。どんな助けでも大歓迎です!文字列配列無限ループへのC++ユーザー入力
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
struct FATNode;
struct FileNode;
class FileList;
class FAT
{
FATNode* head;
};
class FileList
{
FileNode* head;
//methods
public:
int createfile()
{
string word[2];
cout << "Please input file name, followed by file size: ";
int i = 0;
for (string input; cin >> input; i++)
word[i] = input;
for(i=0; i<2; i++)
cout << word[i] << ' ';
return 0;
}
};
struct FileNode
{
string filename;
int filesize;
FAT t1;
FileNode* next;
};
struct FATNode
{
int sectornumber;
FATNode* next;
};
main()
{
FileList myFileSystem;
char start;
int cmd;
bool cont = true;
while(cont == true)
{
cout << "Initializing disk.\n" << "To access menu, type 'Y'. To exit, type 'N': ";
cin >> start;
if(start == 'y' || start == 'Y')
{
cout << "What command would you like to execute on the disk?" << endl;
cout << "1. Format disk\n2. Create file\n3. Delete file\n";
cout << "4. List\n5. Read file\n6. Overwrite file\n7. Append to file\n8. Disk status\nSelection: ";
cin >> cmd;
switch(cmd)
{
case 1 :
break;
case 2 :
myFileSystem.createfile();
break;
default :
cout << "Invalid command" << endl;
}
}
else if(start == 'n' || start == 'N')
{
cont = false;
}
else
{
cout << "Invalid input." << endl;
}
}
}
あなたが入力したテキストの行をしたい場合は、 'のstd :: getline'を使用しています。あるいは、ループを終了させる(そして配列をオーバーフローさせない)ように、 'i <2'を追加します。 'for(文字列入力; i < 2 && cin >>入力; i ++)' – paddy
デバッガを使用して、プログラムの動作を問題の予想される位置の周囲で調べる必要があります。 –
質問に一致する投稿コードに何も見つかりませんでした。あなたが求めていることは私には分かりません。 –