我可以输入int还是终止scanf函数吗?

时间:2018-10-08 15:58:30

标签: c scanf

我们上课的任务是输入整数。我们不知道会有多少。

我想知道是否可以格式化scanf函数,该函数存储整数或通过按Enter终止自身。

我能以某种方式将仅存储整数的scanf("%d")和终止scanf("%[^\n])函数的scanf放在一起吗?

我所知道的是我不能使用scanf("%d%[^\n]),因为scanf正在等待那个整数,我不想输入,因为我已经存储了所有必须的整数。 / p>

我真的不喜欢将所有这些整数的字符串存储到数组中,然后将其转换为具有精确数字的另一个数组的元素的可能性。

2 个答案:

答案 0 :(得分:1)

扫描一个字符。跳过空间和制表符。在换行符上退出。
删除最近的字符,然后尝试扫描整数。如果无法扫描整数,请扫描并丢弃非数字,空格键和换行符除外。

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">

<style>
     .dropbtn{background-color:white;color:black;padding:4px;font-size:18px;border:none;cursor:pointer;border-radius: 7px;border: 2px solid #008CBA;-webkit-transition-duration: 0.8s; /* Safari */
    transition-duration: 0.8s;
}.dropbtn:focus,.dropbtn:hover{background-color:#4db8ff}#myInput{border-box:box-sizing;background-image:url(searchicon.png);background-position:14px 12px;background-repeat:no-repeat;font-size:16px;padding:14px 20px 12px 45px;border:none;border-bottom:1px solid #ddd}#myInput:focus{outline:#ddd solid 3px}.Country{position:relative;display:inline-block}.Country-content{display:none;position:absolute;background-color:#f6f6f6;min-width:230px;overflow:auto;border:1px solid #ddd;z-index:1}.Country-content a{color:#000;padding:12px 16px;text-decoration:none;display:block}.Country a:hover{background-color:#ddd}.show{display:block}
}


#myInput {
    border-box: box-sizing;
    background-image: url('searchicon.png');
    background-position: 14px 12px;
    background-repeat: no-repeat;
    font-size: 16px;
    padding: 14px 20px 12px 45px;
    border: none;
    border-bottom: 1px solid #ddd;
}

#myInput:focus {outline: 3px solid #ddd;}

.Country {
    position: relative;
    display: inline-block;
}

.Country-content {
    display: none;
    position: absolute;
    background-color: #f6f6f6;
    min-width: 230px;
    overflow: auto;
    border: 1px solid #ddd;
    z-index: 1;
}

.Country-content a {
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
}

.Country a:hover {background-color: #ddd;}

.show {display: block;}
</style>
</head>
<body>


<div class="Country">
<button onclick="myFunction()" class="dropbtn">Country</button>
  <div id="myCountry" class="Country-content">
    <input type="text" placeholder="Search here..." id="myInput" onkeyup="filterFunction()">
<select size="20">
     <option><a href="/professional-network/asia/Afghanistan">[flag=af]  <span>Afghanistan</span></a>   
<option><a href="/professional-network/asia/Armenia">[flag=am]   <span>Armenia</span></a>  
<option><a href="/professional-network/asia/Azerbaijan">[flag=az]   <span>Azerbaijan</span></a>  
<option><a href="/professional-network/asia/Bahrain">[flag=bh]   <span>Bahrain</span></a>  
<option><a href="/professional-network/asia/Bangladesh">[flag=bd]   <span>Bangladesh</span></a>  
<option><a href="/professional-network/asia/Bhutan">[flag=bt]   <span>Bhutan</span></a>  
<option><a href="/professional-network/asia/Brunei">[flag=bn]   <span>Brunei</span></a>  
<option><a href="/professional-network/asia/Cambodia">[flag=kh]   <span>Cambodia</span></a></option>
<option><a href="/professional-network/asia/Cyprus">[flag=cy]   <span>Cyprus</span></a></option>
<option><a href="/professional-network/asia/East Timor">[flag=tl]   <span>East Timor</span></a></option>
<option><a href="/professional-network/asia//Georgia">[flag=ge]   <span>Georgia</span></a></option>
<option><a href="/professional-network/asia/India">[flag=in]   <span>India</span></a></option>
<option><a href="/professional-network/asia/Indonesia">[flag=id]   <span>Indonesia</span></a></option>
<option><a href="/professional-network/asia/Iran">[flag=ir]   <span>Iran</span></a></option>
<option><a href="/professional-network/asia/Iraq">[flag=iq]   <span>Iraq</span></a></option>
<option><a href="/professional-network/asia/Israel">[flag=il]   <span>Israel</span></a></option>
<option><a href="/professional-network/asia/Japan">[flag=jp]   <span>Japan</span></a></option>
<option><a href="/professional-network/asia/Jordan">[flag=jo]   <span>Jordan</span></a></option>
<option><a href="/professional-network/asia/Kazakhstan">[flag=kz]   <span>Kazakhstan</span></a></option>
<option><a href="/professional-network/asia/Kuwait">[flag=kw]   <span>Kuwait</span></a></option>
<option><a href="/professional-network/asia/Kyrgyzstan">[flag=kg]   <span>Kyrgyzstan</span></a></option>
<option><a href="/professional-network/asia/Laos">[flag=la]   <span>Laos</span></a></option>
<option><a href="/professional-network/asia/Lebanon">[flag=lb]   <span>Lebanon</span></a></option>
<option><a href="/professional-network/asia/Malaysia">[flag=my]   <span>Malaysia</span></a></option>
<option><a href="/professional-network/asia/Maldives">[flag=mv]   <span>Maldives</span></a></option>
<option><a href="/professional-network/asia/Mongolia">[flag=mn]   <span>Mongolia</span></a></option>
<option><a href="/professional-network/asia/Myanmar">[flag=mm]   <span>Myanmar</span></a></option>
<option><a href="/professional-network/asia/Nepal">[flag=np]   <span>Nepal</span></a></option>
<option><a href="/professional-network/asia/North Korea">[flag=kp]   <span>North Korea</span></a></option>
<option><a href="/professional-network/asia/Oman">[flag=om]   <span>Oman</span></a></option>
<option><a href="/professional-network/asia/Pakistan">[flag=pk]   <span>Pakistan</span></a></option>
<option><a href="/professional-network/asia/China">[flag=cn]   <span>China</span></a></option>
<option><option><a href="/professional-network/asia/The Philippines">[flag=ph]   <span>The Philippines</span></a></option>
<option><a href="/professional-network/asia/Qatar">[flag=qa]   <span>Qatar</span></a></option>
<option><a href="/professional-network/asia/Taiwan">[flag=tw]   <span>Taiwan</span></a></option>
<option><a href="/professional-network/asia/Russia">[flag=ru]   <span>Russia</span></a></option>
<option><a href="/professional-network/asia/Saudi Arabia">[flag=sa]   <span>Saudi Arabia</span></a></option>
<option><a href="/professional-network/asia/Singapore">[flag=sg]   <span>Singapore</span></a></option>
<option><a href="/professional-network/asia/China">[flag=sg]   <span>China</span></a></option>
<option><a href="/professional-network/asia/South Korea">[flag=kr]   <span>South Korea</span></a></option>
<option><a href="/professional-network/asia/Sri Lanka">[flag=lk]   <span>Sri Lanka</span></a></option>
<option><a href="/professional-network/asia/Syria">[flag=sy]   <span>Syria</span></a></option>
<option><a href="/professional-network/asia/Tajikistan">[flag=tj]   <span>Tajikistan</span></a></option>
<option><a href="/professional-network/asia/Thailand">[flag=th]   <span>Thailand</span></a></option>
<option><a href="/professional-network/asia/The United Arab Emirates">[flag=ae]   <span>The United Arab Emirates</span></a></option>
<option><a href="/professional-network/asiaTurkey">[flag=tr]   <span>Turkey</span></a></option>
<option><a href="/professional-network/asia/Turkmenistan">[flag=tm]   <span>Turkmenistan</span></a></option>
<option><a href="/professional-network/asia/Uzbekistan">[flag=uz]   <span>Uzbekistan</span></a></option>
<option><a href="/professional-network/asia/Vietnam">[flag=vn]   <span>Vietnam</span></a></option>
<option><a href="/professional-network/asia/Yemen">[flag=ye]   <span>Yemen</span></a>  
  </div>
</div>

<script>
function myFunction() {
    document.getElementById("myCountry").classList.toggle("show");
}

function filterFunction() {
    var input, filter, ul, li, a, i;
    input = document.getElementById("myInput");
    filter = input.value.toUpperCase();
    div = document.getElementById("myCountry");
    a = div.getElementsByTagName("a");
    
    for (i = 0; i < a.length; i++) {
        var label = a[i].querySelector('span');
        if (label.innerText.toUpperCase().indexOf(filter) > -1) {
            a[i].style.display = "";
        } else {
            a[i].style.display = "none";
        }
    }
}

</script>

</body>
</html>

答案 1 :(得分:1)

scanf函数难以正确使用。

相反,使用fgets一次读取一行。如果输入的字符串只是换行符,则退出循环。如果不是,请使用strtol解析值。您将知道,如果结束指针指向输入末尾的换行符,那么是否仅输入了整数。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

int main()
{
    char line[100], *p;
    long val;

    while (fgets(line, sizeof(line), stdin)) {
        // if enter was pressed by itself, quit loop
        if (!strcmp(line, "\n")) {
            break;
        }

        errno = 0;
        val = strtol(line, &p, 10);
        if (errno) {
            perror("error reading value");
        } else if ((p != line) && (*p == '\n')) {
            // a valid integer was read
            printf("read value %ld\n", val);
        } else {
            // a non-integer was read or extra characters were entered
            printf("not a valid integer: %s", line);
        }
    }
    return 0;
}