我有一個非常簡單的程序。c + +中的scanf和字符串
#include <stdio.h>
#include <string>
using namespace std;
struct scholar{
int number;
string fam;
int ocen;
};
int main(){
int len;
printf("vvedite n");
scanf("%d", &len);
scholar *barr= new scholar[len];
for(int i=0; i<len; i++){
printf("Nomer studenta ");
scanf("%d", &barr[i].number);
printf("\nFamilia ");
scanf("%s", barr[i].fam);
printf("\nOcenka");
scanf("%d", &barr[i].ocen);
}
delete [] bar;
但上線,在這裏我想輸入的家庭,有一個警告:
warning: format specifies type 'char *' but the argument has type 'string'
(aka 'basic_string<char>') [-Wformat].
任何建議如何解決呢?
使用'std :: cin'和'std :: vector'而不是'scanf'和一個指針。 – chris