c++循环的作业
1.使用while循环计算0~100之间偶数之和。2.使用do-while制作一个登录系统,
账户和密码错误时重新输入
正确时提示登陆成功
3使用for循环完成第一个作业.
#include <iostream> using namespace std; int main( void ) { unsigned sum = 0; unsigned i=0; while( i<=100 ) { if( i%2 == 0 ) sum += i; ++i; } cout << sum << endl; }
#include <iostream> #include <string> using namespace std; int main( void ) { const char* correct_user = "guess"; const char* correct_pswd = "password"; string user, pswd; do { cout << "用户: "; getline( cin, user ); cout << "密码: "; getline( cin, pswd ); } while( user!=correct_user || pswd!=correct_pswd ); cout << "登陆成功" << endl; }
#include <iostream> using namespace std; int main( void ) { unsigned sum = 0; for( unsigned i=0; i<=100; i+=2 ) sum += i; cout << sum << endl; }