hasdigit.cpp
#include <iostream>
#include <cctype>
using namespace std;
int main() {
bool hasDigit = false;
string passCode = "";
int valid = 0;
passCode = "abc";
hasDigit = isdigit(passCode.at(0))
|| isdigit(passCode.at(1))
|| isdigit(passCode.at(2));
if (hasDigit) {
cout << "Has a digit." << endl;
}
else {
cout << "Has no digit." << endl;
}
return 0;
}
spaces.cpp
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main() {
string passCode = "";
cout << "Enter pass code: ";
getline(cin, passCode);
//passCode = "1 "; // Replace spaces with _
int i = 0;
while(i < passCode.size()) {
if(isspace(passCode.at(i))) {
passCode.at(i) = '_';
}
i++;
}
cout << passCode << endl;
return 0;
}
climatebars.cpp
// climate bar graph
#include <iostream>
using namespace std;
int main()
{
cout << "Enter temperature: ";
int temp;
cin >> temp;
int num_stars = 0;
while(num_stars < temp) {
//cout << num_stars;
cout << "*" << endl;
num_stars += 5;
}
cout << endl;
return 0;
}
climatebars-for.cpp
// climate bar graph
#include <iostream>
using namespace std;
int main()
{
cout << "Enter temperature: ";
int temp;
cin >> temp;
for(int num_stars = 0; num_stars < temp; num_stars += 5)
{
//cout << num_stars;
cout << "*" << endl;
}
cout << endl;
return 0;
}
double-loop.cpp
// climate bar graph
#include <iostream>
using namespace std;
int main()
{
cout << "Enter size: ";
int size;
cin >> size;
for(int i = 1; i <= size; i++)
{
for(int j = 1; j <= size; j++)
{
if((i*j)%2 == 0) {
cout << "*";
}
else {
cout << " ";
}
}
cout << endl;
}
return 0;
}