C艹重复输入小方法,for循环+while

 

#include <iostream>
#include <cctype>
#include <string>

const int Max = 5;

int main(int argc, char const *argv[]) {

  using namespace std;

  int golf[Max];
  int i;
  for (i = 0; i < Max; i++) {
    std::cout << "round #" << i+1 << '\n';
    while (!(cin >> golf[i])) {
      cin.clear();
      cin.get();
      // while (cin.get() != '\n') {
      //   continue;
      // }
      std::cout << "please enter a number:" << '\n';
    }
  }
  double total = 0.0;
  for (i = 0;i < Max; i++) {
    total += golf[i];
  }
  std::cout << total << '\n';


  return 0;
}

 

 

int vector_begin(){
    vector<string> words;
    string str;
    cout << "please input you content==>:" << '\n';
    while (cin >> str){
        words.push_back(str);
        if (cin.get() == '\n'){
            break;
        }
    }

    vector<string>::iterator iter;

    for (iter =words.begin(); iter != words.end(); iter++){
        cout << *iter << '\n';
    }

    return 0;
}


int main () {
//    assign();

    vector_begin();
    return 0;
}

 

posted @ 2017-09-04 16:25  我当道士那儿些年  阅读(379)  评论(0编辑  收藏  举报