코드를 실행하려고하면 첫 번째 입력을 요청하지만 다음에는 코어 덤프가 표시됩니다.
그리고 나는 약간의 의심이 있습니다. 경고를 fget하는 것을 어떻게 고칠 수 있습니까?
그리고이 코드가 최적화 될 수 있다면 저에게 알려주세요, 효율적인 코드를 만들려고 노력하고 있습니다 : D
다음과 같이 컴파일하십시오.
g ++ -O2-벽 Proy2.cpp -o Proy2
암호:
#include < cstdio >
#include < iostream >
using namespace std;
int main(){
typedef struct PC{
char Brand[20];
char Model[20];
char Serial[20];
char Processor[10];
};
PC PC1[5],*machine;
unsigned int i;
for(i = 0; i < 4; i++){
cout <<"Insert PC brand: ";
fgets(machine->Brand, 20, stdin); fflush(stdin);
cout <<"Insert PC model: ";
fgets(machine->Model, 20, stdin); fflush(stdin);
cout <<"Insert PC serial: ";
fgets(machine->Serial, 20, stdin); fflush(stdin);
cout <<"Insert PC processor: ";
fgets(machine->Processor, 10, stdin); fflush(stdin);
printf("PC Brand : %s", PC1[i].Brand);
printf("PC Model : %s", PC1[i].Model);
printf("PC Serial : %s", PC1[i].Serial);
printf("PC Processor: %s", PC1[i].Processor);
PC1[i] = *machine;
}
return 0;
}
cout
C ++이고 C printf
입니다. fgets
또한 C이며 getline
(C ++) 이어야합니다 . 둘 다 섞지 말고 언어를 선택해야합니다. Char*
C이고 string
C ++입니다.
클래스 또는 구조체가 메인에 있으면 안됩니다.
#include <cstdio>
#include <iostream>
using namespace std;
typedef struct PC{
string Brand;
string Model;
string Serial;
string Processor;
};
int main(){
PC *PC1[5]; // pointer of pointers
PC *machine=new PC[4];
unsigned int i;
for(i = 0; i < 4; i++){
cout <<"Insert PC brand: ";
cin >> machine->Brand;
cout <<"Insert PC model: ";
cin >> machine->Model;
cout <<"Insert PC serial: ";
cin >> machine->Serial;
cout <<"Insert PC processor: ";
cin >> machine->Processor;
PC1[i] = machine; // you store the machine in PC1
cout << "PC Brand : " << PC1[i]->Brand << endl;
cout << "PC Model : " <<PC1[i]->Model << endl;
cout << "PC Serial : " <<PC1[i]->Serial << endl;
cout << "PC Processor: " <<PC1[i]->Processor << endl;
}
return 0;
}
당신은 사용 PC1[5]
하고 루프에서 4 개의 요소 만 제공합니다 (0에서 3은 4 요소입니다). 나는 당신이 당신의 루프를 바꾸고 싶은 것
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다