**This is an old revision of the document!**


#include<iostream>
#include<iomanip>
#include<string>
 
using namespace std;
 
int main() {
 
	cout << "CIN:" << endl;
 
	// from last time:
	int favorite_number;
	/*cout << "Please give me your favorite number:";
	cin >> favorite_number;
 
	cout << "\nHey, I know you, your favorite number is " << favorite_number << endl;
 
	// try again but give "abc" as your favorite number.
	// I wish we had a way to check for bad input and do some thing about it!
 
	// we can do this with strings too
	int users_name;
	cout << "Please give me your first name: ";
	cin >> first_name;
 
	cout << first_name << " is a funny name!!\n";
 
	//But be careful with stings:
	cout << "Please give me your full name: ";
	cin >> full_name;
 
	cout << full_name << " really!!\n";
	cin >> last_name;
	cout << last_name << " is the rest of it.\n";
 
	// back to numbers
	// errors in cin
	int interesting_number;
	cout << "I am expecting a number, try something unexpected:";
	cin >> interesting_number;
	cout << "\nI saw: " << interesting_number << endl;
	cout << "What?\n\nMaybe this will help, give bad input here:\n";
	cout << "Please give me your favorite number:";
	cin >> favorite_number;
 
	cout << "\nHey, I know you, your favorite number is " << favorite_number << endl;
 
	// I sure wish I could tell when input failed and do something about it!!
 
 
	cout << "Please give me your favorite non-negative number: (give me a negative number to test me)";
	cin >> favorite_number;
 
	// WARNING STUPID CODE USED TO MAKE A POINT. DO NOT USE THIS!
	if (favorite_number < 0) cout << favorite_number << "is negative and therefore bad input" << endl;
	if (favorite_number < 0) system("pause");
	if (favorite_number < 0) return 0;
 
	cout << "\nYou said your favorite number is " << favorite_number << endl;
 
	// that was ugly, we need a way to group chucks of code. We have one:
 
	cout << "Please give me your favorite non-negative number: (give me a negative number to test me)";
	cin >> favorite_number;
 
	if (favorite_number < 0) {
		cout << favorite_number << "is negative and therefore bad input" << endl;
		system("pause");
		return 0;
	}
 
	// note {} style AND note that if-; is bad too!
 
	cout << "\nYou said your favorite number is " << favorite_number << endl;
 
	cout << "\nWe can now responde more gracefully to bad input:\n\n";
 
	cout << "Please give me your favorite non-negative number: (give me a negative number to test me)";
	cin >> favorite_number;
 
	if (favorite_number < 0) {
		cout << favorite_number << "is negative and therefore bad input" << endl;
		cout << "Please give me your favorite NON-NEGATIVE number: ";
		cin >> favorite_number;
	}
 
	cout << "\nYou said your favorite number is " << favorite_number << endl;
	*/
	cout << "\nWhat about the bad input the second time? We could:\n\n";
 
	cout << "Please give me your favorite non-negative number: (give me a negative number to test me)";
	cin >> favorite_number;
 
	if (favorite_number < 0) {
		cout << favorite_number << "is negative and therefore bad input" << endl;
		cout << "Please give me your favorite NON-NEGATIVE number: ";
		cin >> favorite_number;
		if (favorite_number < 0) {
			cout << favorite_number << "is negative and therefore bad input" << endl;
			cout << "Please give me your favorite NON-NEGATIVE number: ";
			cin >> favorite_number;
		}
	}
 
	cout << "\nYou said your favorite number is " << favorite_number << endl;
 
	cout << "\nThis is NOT the same as the following, but it has the same effect IN THIS CASE:\n\n";
 
	cout << "Please give me your favorite non-negative number: (give me a negative number to test me)";
	cin >> favorite_number;
 
	if (favorite_number < 0) {
		cout << favorite_number << "is negative and therefore bad input" << endl;
		cout << "Please give me your favorite NON-NEGATIVE number: ";
		cin >> favorite_number;
	}
	if (favorite_number < 0) {
		cout << favorite_number << "is negative and therefore bad input" << endl;
		cout << "Please give me your favorite NON-NEGATIVE number: ";
		cin >> favorite_number;
	}
 
	cout << "\nYou said your favorite number is " << favorite_number << endl;
 
	cout << "\nWhat about truely difficult:\n\n";
 
	cout << "Please give me your favorite non-negative number: (give me a negative number to test me)";
	cin >> favorite_number;
 
	// ALSO STUPID CODE:
	if (favorite_number < 0) {
		cout << favorite_number << "is negative and therefore bad input" << endl;
		cout << "Please give me your favorite NON-NEGATIVE number: ";
		cin >> favorite_number;
	}
	if (favorite_number < 0) {
		cout << favorite_number << "is negative and therefore bad input" << endl;
		cout << "Please give me your favorite NON-NEGATIVE number: ";
		cin >> favorite_number;
	}
	if (favorite_number < 0) {
		cout << favorite_number << "is negative and therefore bad input" << endl;
		cout << "Please give me your favorite NON-NEGATIVE number: ";
		cin >> favorite_number;
	}
	if (favorite_number < 0) {
		cout << favorite_number << "is negative and therefore bad input" << endl;
		cout << "Please give me your favorite NON-NEGATIVE number: ";
		cin >> favorite_number;
	}
 
	// ...
 
	// We will have better ways of dealing with this when we do loops next week.
 
	cout << "\nYou said your favorite number is " << favorite_number << endl;
 
	// compare strings, tax nesting
 
 
	std::system("pause");
	return 0;
}
cs-142/input-and-ifs.1473202910.txt.gz · Last modified: 2016/09/06 23:01 by kseppi
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0