Skip to content

Instantly share code, notes, and snippets.

@derekmma
Created October 1, 2021 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save derekmma/273ef0c2027c60645016783c4770dbf6 to your computer and use it in GitHub Desktop.
Save derekmma/273ef0c2027c60645016783c4770dbf6 to your computer and use it in GitHub Desktop.
Demo program for Week 1, CS31 Fall 2021 Discussion 1C
/* Demo program for CS31 Discussion 1C Week 1
Oct 1, 2021, Mingyu Derek Ma
*/
#include <iostream>
using namespace std;
int main()
{
// Example 1: basic cout usage
int thisYear = 2021;
string thisMonth = "October";
cout << "hello" << endl;
cout << "This year is " << thisYear << ", and this month is " << thisMonth << ". \n";
// Example 2, precision control
double exampleFloat = 3.1415926; // define a double variable
cout << exampleFloat << endl;
cout.setf(ios::fixed);
cout << exampleFloat << endl;
cout.setf(ios::showpoint);
cout << exampleFloat << endl;
cout.precision(2);
cout << exampleFloat << endl;
cout.precision(7);
cout << exampleFloat << endl;
cout.precision(15);
cout << exampleFloat << endl;
// Example 3, receiving input from console
int dateNum = 0;
int hourNum = 0;
cout << "Please input date: ";
cin >> dateNum;
cout << "Please input date and then input hour: ";
cin >> dateNum >> hourNum;
cout << "The dateNum is " << dateNum << ", and the hourNum is " << hourNum << ". \n";
// Example 4, bit overflow example
int a = 2147483647;
a = a + 1;
cout << a;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment