在单个scanf中获取字符串和整数会在获取字符串后跳过其余整数,为什么呢?仅在单个scanf中怎么做?

时间:2019-04-13 14:38:49

标签: c scanf

我需要从单个scanf中获取整数和字符串。但是,除非我用两个scanf来做,否则代码不会采用剩余的整数。如何获得单个scanf函数来接受我输入的所有内容?

struct student {
    int r;
    char a[50];
    int c1, c2, c3;
    float total, per;
} s[100];

main() {
    int i, n;
    printf("Enter total number of students\n");
    scanf("%d", &n);
    for (i = 0; i < n; i++) {
        printf("Enter details of student\nRollNo Name c1 c1 c3 marks\n");
        scanf("%d %[^\n]*%s %d %d %d", &s[i].r, s[i].a, &s[i].c1, &s[i].c2, &s[i].c3);
       // scanf("%d %d %d", &s[i].c1, &s[i].c2, &s[i].c3);  this works 
    }
    for (i = 0; i < n; i++) {
        printf("%d %s %d %d %d\n", s[i].r, s[i].a, s[i].c1, s[i].c2, s[i].c3);
    }
}

1 个答案:

答案 0 :(得分:1)

读取与换行符不同的字符串的格式为$(function() { function initMap() { var location = new google.maps.LatLng(50.0875726, 14.4189987); var mapCanvas = document.getElementById("map"); var mapOptions = { center: location, zoom: 16, panControl: false, scrollwheel: false, mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(mapCanvas, mapOptions); var markerImage = "marker.png"; var marker = new google.maps.Marker({ position: location, map: map, icon: markerImage }); var contentString = '<div class="info-window">' + "<h3>Info Window Content</h3>" + '<div class="info-content">' + "<p>Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.</p>" + "</div>" + "</div>"; var infowindow = new google.maps.InfoWindow({ content: contentString, maxWidth: 400 }); marker.addListener("click", function() { infowindow.open(map, marker); }); var styles = [ { featureType: "landscape", stylers: [{ saturation: -100 }, { lightness: 65 }, { visibility: "on" }] }, { featureType: "poi", stylers: [ { saturation: -100 }, { lightness: 51 }, { visibility: "simplified" } ] }, { featureType: "road.highway", stylers: [{ saturation: -100 }, { visibility: "simplified" }] }, { featureType: "road.arterial", stylers: [{ saturation: -100 }, { lightness: 30 }, { visibility: "on" }] }, { featureType: "road.local", stylers: [{ saturation: -100 }, { lightness: 40 }, { visibility: "on" }] }, { featureType: "transit", stylers: [{ saturation: -100 }, { visibility: "simplified" }] }, { featureType: "administrative.province", stylers: [{ visibility: "off" }] }, { featureType: "water", elementType: "labels", stylers: [ { visibility: "on" }, { lightness: -25 }, { saturation: -100 } ] }, { featureType: "water", elementType: "geometry", stylers: [{ hue: "#ffff00" }, { lightness: -25 }, { saturation: -97 }] } ]; map.set("styles", styles); } google.maps.event.addDomListener(window, "load", initMap); }); ,其中有一个可选的但强烈建议的宽度前缀,用于存储要存储到目标数组中的最大字符数。尾随%[^\n]毫无意义。 但是请注意,此*%s转换说明符将接受标记号作为名称的一部分:

scanf在找到数字时应停止读取该名称。这样一来,用户就可以在每个学生的一行上进行输入,无论提示如何,他/她都可以这样做。

其格式为scanf,如果用户键入的名称超过49个字符,则要防止未定义的行为,请使用%[^0-9\n]指定此限制。

使用%49[^0-9\n]解析输入仍然很困难:从无效输入中恢复很乏味。

这是更正的版本:

scanf()