catching up on entries

This commit is contained in:
left_adjoint 2024-02-05 10:42:57 -08:00
parent 170cd28bfd
commit abe44e96d1
8 changed files with 181 additions and 0 deletions

15
array1.cpp Normal file
View File

@ -0,0 +1,15 @@
#include <iostream>
using namespace std;
int main(){
int intArray[10];
for(int i = 0; i < 10; i++){
intArray[i] = i*i;
}
for(int i = 0; i < 10; i++){
cout << "The " << i << "th thing is: " << intArray[i] << endl;
}
return 0;
}

18
badarray.cpp Normal file
View File

@ -0,0 +1,18 @@
#include <iostream>
using namespace std;
int main(){
int myArray[10];
for(int i = 0; i < 10; i++){
myArray[i] = i*i + 1;
}
cout << myArray[0] << endl;
cout << myArray[1] << endl;
//...
myArray[200] = 12345;
cout << myArray[200] << endl;
cout << "What is the value of the array variable: " << myArray << endl;
return 0;
}

21
cinfail.cpp Normal file
View File

@ -0,0 +1,21 @@
#include <iostream>
using namespace std;
int main(){
int num1;
int num2;
string rest;
cout << "Enter two numbers, separated by a space: ";
cin >> num1;
cin >> num2;
if(cin.fail()){
cout << "oh, it failed" << endl;
cin.clear();
cin >> rest;
cout << "The rest was: " << rest << endl;
}
return 0;
}

18
cinfail2.cpp Normal file
View File

@ -0,0 +1,18 @@
#include <iostream>
using namespace std;
int main(){
int num;
cout << "Enter a whole number: ";
cin >> num;
while(cin.fail() || cin.peek() == '.'){
cout << "Whoops. Enter a whole number: ";
cin.clear();
cin.ignore(256,'\n');
//cout << "Next char is: " << cin.peek() << endl;
cin >> num;
}
cout << "You entered: " << num << endl;
return 0;
}

39
cinfail3.cpp Normal file
View File

@ -0,0 +1,39 @@
#include <iostream>
using namespace std;
int parseInt(){
int num;
cout << "Enter a whole number: ";
cin >> num;
while(cin.fail() || cin.peek() == '.'){
cout << "Whoops. Enter a whole number: ";
cin.clear();
cin.ignore(256,'\n');
//cout << "Next char is: " << cin.peek() << endl;
cin >> num;
}
return num;
}
double parseDouble(){
double num;
cout << "Enter a number: ";
cin >> num;
while(cin.fail()){
cout << "Whoops. Enter a number: ";
cin.clear();
cin.ignore(256,'\n');
//cout << "Next char is: " << cin.peek() << endl;
cin >> num;
}
return num;
}
int main(){
int num = parseInt();
cout << "You entered: " << num << endl;
double dubstep = parseDouble();
cout << "Here's your double: " << dubstep << endl;
return 0;
}

13
whileCin1.cpp Normal file
View File

@ -0,0 +1,13 @@
#include <iostream>
using namespace std;
int main(){
string input = "";
while(input != "chicken"){
cout << "Say the magic word: ";
cin >> input;
}
cout << "You said it!" << endl;
return 0;
}

37
whileCin2.cpp Normal file
View File

@ -0,0 +1,37 @@
#include <iostream>
using namespace std;
int main(){
string input="";
bool yOrN;
// && "and", returns true if left and right are true
// || "or", returns true if left or right are true
// ! "not", ! true = false, ! false = true
// ! (a || b) <--> !a && !b
// Haskell: or $ map (==input) ["Yes,"No","y","n"]
/* while(! (input == "Yes" || input == "y" || input == "No" || input == "n")){
cout << "Enter an option (Yes/y/No/n): ";
cin >> input;
}
*/
while(input != "Yes" && input != "y" && input != "No" && input != "n"){
cout << "Enter an option (Yes/y/No/n): ";
cin >> input;
}
/*
if(input == "Yes" || input == "y"){
yOrN = true;
}
else{
yOrN = false;
}
*/
yOrN = (input == "Yes" || input == "y");
// will print 1 if yOrN = true
// will print 0 if yOrN = false
cout << "Testing our boolean: " << yOrN << endl;
return 0;
}

20
whileCin3.cpp Normal file
View File

@ -0,0 +1,20 @@
#include <iostream>
using namespace std;
int main(){
int ourNum;
int ourNum2;
string ourString;
cout << "Enter numbers: ";
cin >> ourNum;
cin >> ourNum2;
// cin >> ourString;
cout << "You wrote: " << ourNum << " " << ourNum2 << endl;
return 0;
}