在c ++中使用if(cin>>)命令

时间:2017-04-20 09:57:37

标签: c++ string cin

我想计算一个正方形的周长或面积。 如果用户输入a=areacin>>a>>c;。 程序在#include<iostream> #include <string> using namespace std; int main() { int kenar; //kenar means side length string c,a; cout<<"karenin kenar uzunlugunu girin :"; cin>>kenar; cout<<"cevre icin c, alan icin a girin :";// type c for perimeter,a for area cin>>a>>c; if (cin>>c) { cout<<4*kenar<<endl; } else if ( cin>>a) { cout<<kenar*kenar<<endl; } else { cout<<"yanlis giris"<<endl; //cout<<"you type wrong"" } } 命令后跳转。我该怎么办?

    <template>
<div class="coutpage">
<div class="countheader">

</div>
  <ul class="countdowmlist" v-if="!countdowndata.over">
    <li v-text="countdowndata.days"></li>
    <li>天</li>
    <li v-text="countdowndata.hours"></li>
    <li>时</li>
    <li v-text="countdowndata.minus"></li>
    <li>分</li>
    <li v-text="countdowndata.second"></li>
    <li>秒</li>
  </ul>
  <div class="isover" v-if="countdowndata.over">
    时间到
  </div>
</div>
</template>

<script type="text/ecmascript-6">
export default {
  name: 'Countdown',
  props: [ 'endDate' ],
  data () {
    return {
      countdowndata: {
        days: '0',
        hours: '0',
        minus: '0',
        second: '0',
        realcount: '0',
        over: false
      },
      interval: {}
    }
  },
  created () {
    const counttime = new Date(this.endDate).getTime() - new Date().getTime()
    if (counttime < 0) {
      clearInterval(this.interval)
      return
    }
    this.countdowndata.realcount = Math.floor(counttime / 1000)
    const _this = this
    this.interval = setInterval(function () {
      _this.getCount()
    }, 1000)
  },
  methods: {
    getCount () {
      var time = this.countdowndata.realcount
      if (time <= 0) {
        clearInterval(this.interval)
        this.countdowndata.over = true
        return
      }
      var days = Math.floor(time / 60 / 60 / 24)
      var hours = Math.floor((time - days * 60 * 60 * 24) / 60 / 60)
      var minus = Math.floor((time - days * 60 * 60 * 24 - hours * 60 * 60) / 60)
      var second = Math.floor((time - days * 60 * 60 * 24 - hours * 60 * 60 - minus * 60))
      hours = hours < 10 ? ('0' + hours) : hours
      minus = minus < 10 ? ('0' + minus) : minus
      second = second < 10 ? ('0' + second) : second
      this.countdowndata.days = days
      this.countdowndata.hours = hours
      this.countdowndata.minus = minus
      this.countdowndata.second = second
      this.countdowndata.realcount--
    }
  }

}
</script>

2 个答案:

答案 0 :(得分:2)

基本上,你应该有一个变量,其值可以是&#39; c&#39;或者&#39; a&#39;根据用户输入。然后,您可以使用此变量检查用户输入的内容,如下所示:

 int main()
 {
     int kenar; 
     char option; //note I have changed it to char
     cout<<"karenin kenar uzunlugunu girin  :";
     cin>>kenar;
     cout<<"cevre icin c, alan icin a girin  :";
     cin>>option;

    if (option == 'c')
    {
        cout<<4*kenar<<endl;
    }
    else if (option == 'a')
    {
        cout<<kenar*kenar<<endl;
    }
    else
    {
        cout<<"yanlis giris"<<endl; //cout<<"you type wrong""
    }
}

答案 1 :(得分:0)

cin >>c尝试将单词读入字符串cif(cin>>c)检查您是否成功阅读了某个单词。

这与输入单词"c"的用户无关。您有一个名为c的变量。你可能想要的是std::string operation,所以你可以测试

if (cin>>operation) {
   if (operation=="c") {
     // ...
   }
   else (operaion=="a") {
     // ...
   }