#include <iostream>
int main()
{
using namespace std;
int a, b;
cout << "Please enter the first integer:";
cin >> a;
cout << "Please enter the second integer:";
cin >> b;
int s = 0;
while (a <= b)
{
s += a;
++a;
}
cout << "The sum of them is " << s << endl;
return 0;
}
#include <iostream>
int main()
{
using namespace std;
float a;
cout << "Please enter the first number:";
cin >> a;
float s = 0;
while (a != 0)
{
s += a;
cout << "The sum of numbers until now: " << s << endl;
cout << "Please enter the next number:";
cin >> a;
}
cout << "The sum of numbers until now: " << s << endl;
return 0;
}
#include <iostream>
int main()
{
using namespace std;
const float a_D = 0.1;
const float a_C = 0.05;
float d = 100;
float c = 100;
int year = 0;
while (c <= d)
{
c = (1+a_C) * c;
d += (1+a_D) * 100;
++year;
}
cout << "The years are : " << year << endl;
return 0;
}
#include <iostream>
#include <string>
int main()
{
using namespace std;
const int Month = 12;
const string month[Month] =
{
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
int sale[Month] = {0};
int s = 0;
cout << "Please enter the sales of 12 months.\n";
for (int i=0; i < Month; ++i)
{
cout << month[i] << ":";
cin >> sale[i];
s += sale[i];
}
cout << "The sum of sales are : " << s << endl;
return 0;
}
#include <iostream>
#include <string>
int main()
{
using namespace std;
const int Month = 12;
const string month[Month] =
{
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"
};
int sale[3][Month] = {0};
int sum = 0;
for (int j = 0; j < 3; ++j) {
int s = 0;
cout << j << " Please enter the sales of 12 months.\n";
for (int i = 0; i < Month; ++i) {
cout << month[i] << ":";
cin >> sale[j][i];
s += sale[j][i];
}
cout << j << " The sum of sales are : " << s << endl;
sum += s;
}
cout << "The sum of sales of the 3 years are : " << sum << endl;
return 0;
}
- [1]
#include <iostream>
#include <string>
int main()
{
using namespace std;
struct Car
{
string brand;
int year;
};
int n = 0;
cout << "How many cars do you wish to catalog?";
cin >> n;
Car * ps = new Car[n];
int i = 0;
while (i < n)
{
cout << "Car #" << i+1 << ":\n";
cout << "Please enter the make:";
cin.get();
getline(cin, ps[i].brand); // [1]
cout << "Please enter the year made:";
cin >> ps[i].year;
++i;
}
cout << "Here is your collection:\n";
cout << ps[0].year << " " << ps[0].brand << endl;
cout << ps[1].year << " " << ps[1].brand << endl;
return 0;
}
7.
#include <iostream>
#include <cstring>
int main()
{
using namespace std;
const int ArSize = 100;
char str[ArSize];
cout << "Enter words (to stop, type the word done):\n";
cin >> str;
int i = 0;
while (strcmp(str, "done"))
{
cin >> str;
++i;
}
cout << "You entered a total of " << i << " words.\n";
return 0;
}
8.
#include <iostream>
#include <string>
int main()
{
using namespace std;
string str;
cout << "Enter words (to stop, type the word done):\n";
cin >> str;
int i = 0;
while (str != "done")
{
cin >> str;
++i;
}
cout << "You entered a total of " << i << " words.\n";
return 0;
}
#include <iostream>
int main()
{
using namespace std;
int num;
cout << "Enter number of rows:";
cin >> num;
int i = 0;
for (int i = 0; i < num; ++i)
{
for (int j = 0; j < num; ++j)
{
if ((num-j-1) > i)
cout << ".";
else
cout << "*";
}
cout << endl;
}
return 0;
}
参考资料:
[1] 4.3.3 string类I/O