簡體   English   中英

c / c ++ scanf / cin從多行讀取int

[英]c/c++ scanf/cin reads int from multiple lines

我正在c / c ++中進行一些基本的輸入解析。

格式:值的數量,后跟空格分隔的值:

3
5 2 4

這里的問題是第一行后沒有空格。 這將導致cin和scanf將35讀入第一個變量,而不是3。

int num;

scanf("%d", &num);

int array[num];
for (int i = 1; i <= num; i++) {
    scanf("%d", &array[i]);
}

如何獲取cin或scanf來停止在換行符處進行解析?

編輯:即使在讀取之前將變量寫入以后,也不要初始化變量,這很不好嗎? int num

如果我輸入輸入內容,它將起作用,但是如果我粘貼輸入,它將不起作用。 有什么線索嗎?

std::cin將換行符解釋為空格,因此您正在使用的文件可能包含換行符以外的內容。 您還使用了非標准擴展來聲明該數組。 這不是可移植的,不能保證所有編譯器都支持。 我建議您改用std::vector

您的for循環也不正確。 數組使用基於零的索引來訪問其元素。 因此,您最終無法訪問數組,這是未定義的行為。 這意味着您的程序可能會崩潰,可能會覆蓋其他變量,或者您根本不會注意到任何症狀。 如果它覆蓋其他變量,也可能會導致您遇到的症狀。

下面的示例使用C ++輸入流而不是scanf來提供更好的錯誤檢查。

#include <istream>
#include <vector>

std::vector<int> load(std::istream& in)
{
    std::size_t count;
    std::vector<int> data;

    // If the user does not enter a number "in >> count" will fail.
    if (in >> count)
    {
        int value;
        while (count-- && in >> value)
            data.push_back(value);
    }

    return data;
}


#include <iostream>

int main()
{
    auto data = load(std::cin);
    for (auto i : data)
        std::cout << i << std::endl;
}

您可以通過使用std::stringstream作為輸入來測試而不用讀取文件。

#include <iostream>
#include <sstream>
int main()
{
    std::stringstream text("3\n5 2 4");
    auto data = load(text);

    for (auto i : data)
        std::cout << i << std::endl;
}

在for循環中,您將數組從位置1而不是0開始。這將導致超出范圍,因為您要寫入數組的元素2。 如果分配2個元素組成的數組,則有效元素將為0和1。此代碼有效:

int num;

scanf("%d", &num);

int array[num];

for (int i = 0; i < num; i++)
{
    scanf( "%d", &array[i] );
}
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num;
    scanf("%d", &num);

    int *array= malloc(sizeof(int) *num);    // num is known at runtime
    int i;
    for (i = 0; i < num; i++) {    //starts at 0, ends at num - 1 
        scanf("%d", &array[i]);
    }
    for (i = 0;i< num; i++) {
        printf("%d", array[i]);
    }
    free(array);
}

0開始array ,因為數組索引從0開始-像:

for (int i = 0; i < num; i++) 

您將從1開始創建第一個元素,這使其變得未定義。 此外,您應該制作動態數組。

[編輯:Answere特定於C ++,因為Question也具有C ++標簽]

好第一件事。

您的數組定義錯誤。

int array[num];    // Super wrong way

您不應在定義數組時將變量作為索引傳遞,這是不允許的。 否則,將導致“鼻惡魔”。

int * array = new int[num]    //correct way

該代碼現在可能正常工作,但是您給定的數組定義在UB類別下。

我喜歡Lidong Guo的代碼,並對其進行了修改以與Microsoft的C編譯器一起運行。 唯一的變化是將所有數據定義移到任何可執行代碼的前面,而且我在打印數字之間添加了一個空格。

  #include <stdio.h>
  #include <stdlib.h>
  main()
  {
       int num;

       int *array; //[num];
       int i;

       scanf("%d\n", &num);//here deal with the newlinw
       array= malloc(sizeof(int) *num);//[num];

       for (i = 0; i < num; i++)
       {//the loop .start 0 end num -1
          scanf("%d", &array[i]);
       }

       for (i = 0; i < num; i++)
       {
          printf("%d ", array[i]);
       }

       free(array);
 }

0開始循環,並以num-1結束循環,即

for (int i = 0; i < num - 1; i++) 
    scanf("%d", &array[i]);   

粘貼輸入不起作用的原因是它在兩行之間不包含換行符

改變

掃描

聲明

scanf(“%d”,&array [i]);

數組索引也從0開始,以num-1結尾

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM