0

シンボルが含まれている場合、1 行のユーザー入力から複数の変数を割り当てる方法を考えていました。たとえば、ユーザー入力が 5-25-1995 の場合、5、25、および 1995 を別の変数に割り当てて、「-」を無視することは可能ですか? 私は cin.ignore() を使用しようとしていますが、まだ運がありません。

ありがとう。

短縮版:

ユーザー入力「1995 年 3 月 24 日」

望ましい結果

int 月は 3、int 日は 24、int 年は 25、

4

2 に答える 2

4
char dummy;
int month, day, year;
cin >> month >> dummy >> day >> dummy >> year;
于 2013-03-11T02:34:11.033 に答える
0

あなたの特定の要件は、入力が「3-24-1995」の形式であるということだったので、ラインに沿った何かがあなたのニーズに準拠し、あなたが望むものを生み出すかもしれません.

/* Code */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main ()
{
  char str[] ="3-24-1995"; // Your input that you will have some way of getting
  char * month, *day, *year;

  month=strtok (str,"-");

  day = strtok (NULL,"-");
  year = strtok (NULL,"-");

  // Here, converting to int, just because you were looking to convert it into 
  // int otherwise you could just leave it un converted too.

  printf("month: %d day: %d year: %d\n",atoi(month), atoi(day), atoi(year));
}
于 2013-03-11T03:45:07.027 に答える