Skip to content

Instantly share code, notes, and snippets.

@brenttaylor
Created February 28, 2012 01:32
Show Gist options
  • Save brenttaylor/1928386 to your computer and use it in GitHub Desktop.
Save brenttaylor/1928386 to your computer and use it in GitHub Desktop.
Student Assignment
#include <iostream>
#include <limits>
#include <cmath> //Has our Absolute value function: abs()
namespace BTH { //Brent Taylor Helper namespace. ;)
template<typename T>
bool InputFromStream(std::istream &InputStream, T &Result) {
InputStream >> Result;
if (!InputStream.good()) {
InputStream.clear();
InputStream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return false;
}
return true;
}
void ClearInputBuffer(std::istream &InputStream) {
InputStream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
}
void PrintCharacter(int Iterations, char Character) {
//Might be a bad idea to give a negative number here. ;)
//Let's get the absolute value and cache the result.
const int AbsIterationCache = std::abs(Iterations);
for (int i = 0; i < AbsIterationCache; i++) {
std::cout << Character;
}
std::cout << std::endl << std::endl;
}
int main()
{
int numChars;
while (numChars != 100)
{
std::cout << "Enter number of stars: ";
if (!BTH::InputFromStream<int>(std::cin, numChars)) {
std::cout << "Please enter a number!" << std::endl;
continue;
}
std::cout << std::endl;
if (numChars < 0)
{
PrintCharacter(numChars, '-');
}
else if (numChars >= 100000)
{
std::cout << "Bad idea, trust me." << std::endl;
}
else
{
PrintCharacter(numChars, '*');
}
BTH::ClearInputBuffer(std::cin);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment