简体   繁体   中英

Input n space separated integer in c++

Generally we take input in string in such cases but How will we ensure that string will contain N integer only not more than that ?
Input format
Line 1 : Integer n .
Line 2 : Price for n days (separated by space). It can contain duplicate values .

#include <stdio.h>
 int main()
 {
  int n;
  char a;
  printf("How many digit you want to take???");
  scanf("%d", &n);
  scanf("%c", &a);
  char name[2*n];
  printf("Enter Numbers");
  fgets(name,2*n,stdin);
  printf("You Entered :: %s",name);
  return(0);
 }

Even if you enter more than n numbers , it array won't store . You can always use strlen(name) [from String.h] to get length of char array for special conditioning

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM