사용자 입력 문자열을 배열로 구문 분석하려고합니다. Example: user inputs "hello to you" array[0]="hello" array[1]="to" array[2]="you'
사용자에게 몇 가지 단어를 입력하라는 메시지가 표시되면 프로그램이 무한 반복되는 것처럼 보입니다. 나는 또한 벡터를 사용해 보았으므로 다른 영역의 내 논리 일 수 있습니다. 나는 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;
}
}
}
루프 조건은 cin >> input
입니다. 이 표현식은를 반환 cin
하며 이는 암시 적으로 부울 ( http://en.cppreference.com/w/cpp/io/basic_ios/operator_bool ) 로 변환 될 수 있습니다 . 그러나 오류가 있거나 파일 ( )의 끝에 도달 한 경우 cin
에만이됩니다 . Ctrl + D를 눌러 신호 를 보낼 수 있습니다 .false
EOF
EOF
또한 사용자로부터 얻은 정보를 저장하기 위해 고정 크기 배열을 사용하고 있습니다. 사용자가 2 개 이상의 단어를 입력하면 배열 word
이 오버플 로되기 때문 입니다. 그것은 정의되지 않은 행동입니다.
파일 이름과 파일 크기 만 원하는 경우 다음을 사용하는 것이 좋습니다.
std::string filename;
std::size_t filesize;
std::cin >> filename >> filesize;
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다