int main() try { int val1 = 0; int val2 = 0; cout << "Please enter two integer values separated by a space: "; cin >> val1 >> val2; if (!cin) error("something went bad with the read"); cout << "values : " << val1 << " " << val2 << '\n'; if (val1<val2) cout << val1 << " is smallest\n"; if (val2<val1) cout << val2 << " is smallest\n"; if (val1==val2) cout << val1 << " and " << val2 << " are equal\n"; // I "forgot" to ask for that possibility cout << "sum : " << val1+val2 << '\n'; cout << "product : " << val1*val2 << '\n'; if (val2==0) cout << "good try! but I don't divide by zero\n"; else cout << "ratio (val1/val2): " << val1/val2 << '\n'; // note that this is integer division so 5/2 is 2 (not 2.5) // some more tests: if (val1<0) cout << val1 << " is negative\n"; cout << "difference (val1-val2) : " << val1-val2 << '\n'; if (val2!=0) cout << "remainder (val1%val2) : " << val1%val2 << '\n'; cout << "square(val1) : " << val1*val1 << '\n'; keep_window_open(); // For some Windows(tm) setups } catch (runtime_error e) { // this code is to produceerror messages; it will be described in Chapter 5 cout << e.what() << '\n'; keep_window_open("~"); // For some Windows(tm) setups }
//------------------------------------------------------------------------------- int main() try { define_name("pi",3.1415926535); define_name("e",2.7182818284); cout << "Welcome to our simple calculator.\n" "Please enter expressions using floating-point numbers.\n"; calculate(); keep_window_open(); return 0; } catch (runtime_error& e) { cerr << e.what() << '\n'; keep_window_open("~~"); return 1; } catch (...) { cerr << "Oops: unknown exception!\n"; keep_window_open("~~"); return 2; }
int main() try { vector<string> vs; vs.push_back("Technicalities"); vs.push_back("a"); vs.push_back("A"); vs.push_back("hellohellohell"); // same length as "Technicalities" vs.push_back("Hellohellohell"); // lexicographically 'H' < 'h' // vs.push_back(""); // the empty string vs.push_back("Technicalities"); // More technicalities vs.push_back("!"); // same length as "a" cout << "sizes: "; vector<int> lengths = get_sizes(vs); for (int i=0; i<lengths.size(); ++i) cout << lengths[i] << ' '; cout << '\n'; int i = longest(vs); cout << "longest(): index==" << i << "; string=='" << vs[i] << "'\n"; // note the quotes: I want to be able to see the empty string cout << "shortest(): '" << shortest(vs) << "'\n"; // note the quotes: I want to be able to see the empty string cout << "lex_first(): '" << lex_first(vs) << "'\n"; lex_last(vs,i); // pass 'i' for lex_last() to return into cout << "lex_last(): index==" << i << "; string=='" << vs[i] << "'\n"; // note the quotes: I want to be able to see the empty string // note: had vs been empty, this code would have accessed vs[-1]; Ouch! keep_window_open("~"); // For some Windows(tm) setups } catch (runtime_error e) { // this code is to produce error messages cout << e.what() << '\n'; keep_window_open("~"); // For some Windows(tm) setups }
int main() try { cout << "Welcome to our simple calculator.\n" << "Please enter expressions using floating-point numbers.\n"; double val = 0; while (cin) { Token t = ts.get(); if (t.kind == 'x') break; // 'x' for quit if (t.kind == '=') // '=' for "print now" cout << "=" << val << '\n'; else ts.putback(t); val = expression(); } keep_window_open(); } catch (exception& e) { cerr << "error: " << e.what() << '\n'; keep_window_open(); return 1; } catch (...) { cerr << "Oops: unknown exception!\n"; keep_window_open(); return 2; }
int main() try { int val1 = 0; int val2 = 0; int val3 = 0; cout << "Please enter three integer values separated by a space: "; cin >> val1 >> val2 >> val3; if (!cin) error("something went bad with the read"); cout << "values read : " << val1 << ", " << val2 << ", " << val3 <<'\n'; // idea for solution: // just test which value is the smallest and put it into "smallest" // then test which ofthe remaining two values is the smaller and put it into "middle" // then but the remaining variable int "largest" int smallest = 0; int middle = 0; int largest = 0; if (val1<=val2 && val1<=val3) { // && means and smallest = val1; if (val2<=val3) { middle = val2; largest = val3; } else { middle = val3; largest = val2; } } else if (val2<=val1 && val2<=val3) { smallest = val2; if (val1<=val3) { middle = val1; largest = val3; } else { middle = val3; largest = val1; } } else { // since neither val1 nor val2 was smaller than val3, val3 must be the smallest smallest = val3; if (val1<=val2) { middle = val1; largest = val2; } else { middle = val2; largest = val1; } } cout << "values sorted : " << smallest << ", " << middle << ", " << largest <<'\n'; keep_window_open(); // For some Windows(tm) setups } catch (runtime_error e) { // this code is to produceerror messages; it will be described in Chapter 5 cout << e.what() << '\n'; keep_window_open("~"); // For some Windows(tm) setups }
int main() { std::string enterToClose = "~~"; try { calculate(); keep_window_open(enterToClose); return 0; } catch (std::exception& error) { std::cerr << error.what() << '\n'; keep_window_open(enterToClose); return 1; } catch (...) { std::cerr << "Unknown exception!\n"; keep_window_open(enterToClose); return 2; } }
int main() try { //defining some variables: define_name("pi", 3.1415926535); define_name("e", 2.7182818284); calculate(); keep_window_open(); //for windows consoles return 0; } catch(exception& e) { cerr<< e.what() << endl; keep_window_open("~"); /* the above line is the same as: cout << "Please enter ~ to close the program"; char ch; while (cin>>ch) if (ch == '~') return 1; */ return 1; } catch(...) { cerr << "exception \n"; keep_window_open("~"); return 2; }
int main() try { vector<int> val; cout << "Please enter a sequence of integers ending with any non-digit character: "; int i; while (cin>>i) val.push_back(i); print("\nInput:\n",val); reverse1(val); print("\nReversed once:\n",val); reverse2(val); print("\nReversed again:\n",val); keep_window_open("~"); // For some Windows(tm) setups } catch (runtime_error e) { // this code is to produce error messages; it will be described in Chapter 5 cout << e.what() << '\n'; keep_window_open("~"); // For some Windows(tm) setups } catch (...) { // this code is to produce error messages; it will be described in Chapter 5 cout << "exiting\n"; keep_window_open("~"); // For some Windows(tm) setups }
int main() try { while (cin) { cout << "> "; Token t = ts.get(); while (t.kind == ';') t=ts.get(); // eat ';' if (t.kind == 'q') { keep_window_open(); return 0; } ts.putback(t); cout << "= " << expression() << endl; } keep_window_open(); return 0; } catch (exception& e) { cerr << e.what() << endl; keep_window_open("~~"); return 1; } catch (...) { cerr << "exception \n"; keep_window_open("~~"); return 2; }
int main(){ char op; int arg_a, arg_b; cout << "Enter operation (e.g. c 10 3 or p 5 2)\n"; cin >> op >> arg_a >> arg_b; if (arg_a < arg_b){ cerr << "Please make sure that your numbers are in descending order of size\n"; keep_window_open(); return 1; } long permutation, combination; if (op == 'p' || op == 'P'){ long permutation = factorial(arg_a, (arg_a - arg_b)); cout << "P(" << arg_a << ',' << arg_b << ") is " << permutation << endl; } else if (op == 'c' || op == 'C'){ long combination; if (arg_a - arg_b > arg_b) combination = factorial(arg_a, (arg_a - arg_b)) / factorial(arg_b); else combination = factorial(arg_a, arg_b) / factorial(arg_a - arg_b); cout << "C(" << arg_a << ',' << arg_b << ") is " << combination << endl; } else cout << "Bad input\n"; keep_window_open(); }
int main() try { cout << "Please enter an input file names: "; string name; cin>>name; ifstream ifs(name.c_str()); if (!ifs) error("bad input file name: ", name); cout << "Please enter a word to search for: "; string word; cin >> word; int line_no = 0; string line; while (getline(ifs,line)) { ++line_no; // we read another line for (int i=0; i<line.size(); ++i) if (match(line,i,word)) { cout << line_no << ": " << line << '\n'; break; // don't find a word twice } } keep_window_open("~"); // For some Windows(tm) setups } catch (runtime_error e) { // this code is to produce error messages; it will be described in Chapter 5 cout << e.what() << '\n'; keep_window_open("~"); // For some Windows(tm) setups } catch (...) { // this code is to produce error messages; it will be described in Chapter 5 cout << "exiting\n"; keep_window_open("~"); // For some Windows(tm) setups }
int main() { Name_pairs names; try { names.read_names(); names.read_ages(); std::cout << "Names and ages unordered:\n"; cout << names; names.sort(); std::cout << "Names and ages sorted by name:\n"; cout << names; } catch (exception &e) { cout << "error: " << e.what() << std::endl; keep_window_open(); return 1; } catch (...) { cout << "Unknown exeption!\n"; keep_window_open(); return 2; } keep_window_open(); return 0; }
int main() try { double val = 0; while (cin) { Token t = ts.get(); if (t.kind == 'q') break; // 'q' for quit if (t.kind == ';') // ';' for "print now" cout << "=" << val << '\n'; else { ts.putback(t); val = expression(); } } keep_window_open(); } catch (exception& e) { cerr << "error: " << e.what() << '\n'; keep_window_open(); return 1; } catch (...) { cerr << "Oops: unknown exception!\n"; keep_window_open(); return 2; }
int main() try { cout << "Welcome to our simple calculator.Please enter expressions using floating-point numbers.\n" << "Available operators are '+','-','/','*'. To print a result please use '='' at the end of your statement.\n" << "To exit the program input 'x'\n"; double val = 0; while (cin) { Token t = ts.get(); if (t.kind == 'x') break; // 'q' for quit if (t.kind == '=') // ';' for "print now" cout << "=" << val << '\n'; else ts.putback(t); val = expression(); } keep_window_open(); } catch (exception& e) { cerr << "error: " << e.what() << '\n'; keep_window_open(); return 1; } catch (...) { cerr << "Oops: unknown exception!\n"; keep_window_open(); return 2; }
int main(){ cout << "Please enter values, you can multiply, divide, add or subtract.\n"; try { while (cin) { Token t = ts.get(); double val; if (t.kind == 'x') break; // 'x' for quit if (t.kind == ';') // ';' for "print now" cout << "=" << val << '\n'; else ts.putback(t); val = expression(); } keep_window_open(); } catch (exception& e) { cerr << "error: " << e.what() << '\n'; keep_window_open(); return 1; } catch (...) { cerr << "Oops: unknown exception!\n"; keep_window_open(); return 2; } }
// メインループとエラーの処理 int main(){ try { double val = 0; while (cin){ Token t = ts.get(); if(t.kind == 'q') break; // 'q'は「終了」を表す if(t.kind == ';') // ';'は「今すぐ出力」を表す cout << "= " << val << '\n'; else ts.putback(t); val = expression(); } keep_window_open("~0"); } catch (exception& e) { cerr << e.what() << endl; keep_window_open ("~1"); return 1; } catch (...) { cerr << "exception \n"; keep_window_open ("~2"); return 2; } }
int main(){ /* Returns roots of quadratic If roots are identical, only returns one If determinant is less than 0, returns error message */ double a, b, c; cout << "Enter values a, b, and c of the polynomial ax^2 + bx + c = 0\n"; cin >> a >> b >> c; double discriminant = (b * b - 4 * a * c); if (discriminant < 0){ cerr << "Your values do not return real roots!\n"; keep_window_open(); return -1; } double x1 = (-b + sqrt(discriminant)) / (2 * a); double x2 = (-b - sqrt(discriminant)) / (2 * a); if(x1 == x2) cout << "The root is " << x1 << '\n'; else cout << "The two roots are " << x1 << " and " << x2 << '\n'; keep_window_open(); return 0; }
int main() try { vector<int> v; cout << "sizeof vector: " << sizeof(v) << "\n"; Mini_vec<int> mv; cout << "sizeof Mini_vec: " << sizeof(mv) << "\n"; Mini_vec<int> mv10 = Mini_vec<int>(10); cout << mv10[0] << "\n"; for (int i = 0; i<mv10.size(); ++i) mv10[i] = i; for (int i = 0; i<mv10.size(); ++i) cout << mv10[i] << ' '; cout << "\n"; Mini_vec<int> mv_cpctr = mv10; for (int i = 0; i<mv_cpctr.size(); ++i) cout << mv_cpctr[i] << ' '; cout << "\n"; Mini_vec<int> mv_cpasgn; mv_cpasgn = mv10; for (int i = 0; i<mv_cpasgn.size(); ++i) cout << mv_cpasgn[i] << ' '; cout << "\n"; mv_cpasgn.resize(5); for (int i = 0; i<mv_cpasgn.size(); ++i) cout << mv_cpasgn[i] << ' '; cout << "\n"; vector<vector<vector<int> > > v3(100); Mini_vec<Mini_vec<Mini_vec<int> > > mv3(100); cout << "sizeof vector<vector<vector<int> > >: " << sizeof(v3) << "\n"; cout << "sizeof Mini_vec<Mini_vec<Mini_vec<int> > >: " << sizeof(mv3) << "\n"; Mini_vec<int> mv4; for (int i = 0; i<10; ++i) mv4.push_back(i); for (int i = 0; i<mv4.size(); ++i) cout << mv4[i] << ' '; cout << "\n"; } catch (exception& e) { cerr << "exception: " << e.what() << endl; keep_window_open(); } catch (...) { cerr << "exception\n"; keep_window_open(); }
int main() try { const int xmax = 1200; const int ymax = 600; const int x_orig = 100; const int y_orig = ymax - 100; const Point orig(x_orig,y_orig); const int r_min = 0; const int r_max = 20; const int x_scale = 20; const int y_scale = 200; Point tl(300,50); Simple_window win(tl,xmax,ymax,""); const int xlength = xmax - 200; const int ylength = ymax - 200; Axis x(Axis::x,Point(100,y_orig),xlength,xlength/x_scale,"1 == 20 pixels"); x.set_color(Color::red); win.attach(x); Axis y(Axis::y,Point(x_orig,500),ylength,ylength/y_scale,"1 == 200 pixels"); y.set_color(Color::red); win.attach(y); Open_polyline opl; opl.add(Point(orig.x,orig.y-y_scale)); win.attach(opl); for (int i = 1; i<=50; ++i) { ostringstream ss; ss << "Leibniz series, element " << i; win.set_label(ss.str()); int x = opl.point(i-1).x + x_scale; int y = orig.y - leibniz(i) * y_scale; opl.add(Point(x,y)); win.wait_for_button(); } } catch (exception& e) { cerr << "exception: " << e.what() << endl; keep_window_open(); } catch (...) { cerr << "exception\n"; keep_window_open(); }
int main(){ double a;//created variables double b; double c; double x1; double x2; cout<<"please input values a b c to solve the function of the form ax^2+bx+c=0\na=";//prompt for user input of variables a b and c cin>>a; cout<<"b="; cin>>b; cout<<"c="; cin>>c; if (a==0&b==0&c==0){//if statement that outputs that every possible number is a root cout<<"every real and complex number is a root\n"; keep_window_open();//stops the program return 0;} if (a==0&b==0&c!=0){//if statement that outputs the equation is inconsistent in other words it is not of the form ax^2+bx+c and has no roots cout<<"the equation is inconsistent\n"; keep_window_open();//stops the program return 0;} if (discriminant(a,b,c)<0){//if statement that outputs the possible roots are complex cout<<"the roots are complex\n"; keep_window_open();//stops the program return 0;} if (a==0&b!=0){//if statement that says there is only one root cout<<"there is one root\n"; } if(discriminant(a,b,c)==0){//if statement that outputs there are two of the same roots cout<<"there is one double root\n"; } x1=(-b+sqrt((b*b)-(4*a*c)))/(2*a);//calcution of the roots x2=(-b-sqrt((b*b)-(4*a*c)))/(2*a); double r1=residual(a,b,c,x1);//use of the function to calculate the residual of roots x1 and x2 double r2=residual(a,b,c,x2); cout<<"root1:"<<x1<<"\nroot2:"<<x2<<"\nresidual1:"<<r1<<"\nresidual2:"<<r2<<"\n"; //output of the roots and residuals keep_window_open(); return 0; }
int main() try { cout << "Enter a temperature and unit of temperature (c = Celsius, f = Fahrenheit)\n"; double temp_to_convert = 0; //input temperature variable cin >> temp_to_convert; if (cin.fail()) { cin.clear(); //clear cin error flags cin.ignore(INT_MAX, '\n'); //clear cin buffer error("entered non-numeric temperature!\n"); } char entered_temp_unit = '?'; cin >> entered_temp_unit; entered_temp_unit = tolower(entered_temp_unit); double converted_temp = 0; char converted_temp_unit = '?'; switch (entered_temp_unit) { case 'f' : converted_temp = ftoc(temp_to_convert); converted_temp_unit = 'c'; break; case 'c' : converted_temp = ctof(temp_to_convert); converted_temp_unit = 'f'; break; default : error("entered wrong temperature unit of measure!\n"); } cout << "Converted temperature is " << converted_temp << " " << converted_temp_unit << '\n'; keep_window_open(); return 0; } catch (exception& e) { cerr << "error: " << e.what() << '\n'; keep_window_open(); return 1; } catch (...) { cerr << "Oops: unknown exception!\n"; keep_window_open(); return 2; }
int main() // C++ programs start by exe cuting the function main { cout << "Hello, programming!\n" << "Here we go!" << std::endl; // output “Hello, World!” keep_window_open(); // wait for a characte r to be e nte re d return 0; }
int main() try { calculate(); keep_window_open(); // cope with Windows console mode return 0; } catch (runtime_error& e) { cerr << e.what() << endl; keep_window_open("~~"); return 1; } catch (...) { cerr << "exception \n"; keep_window_open("~~"); return 2; }
int main(){ ifstream is; is.open ("number.txt"); int product, max; int a, b, c, d, e; char temp; is.get(temp); a = temp - '0'; is.get(temp); b = temp - '0'; is.get(temp); c = temp - '0'; is.get(temp); d = temp - '0'; is.get(temp); e = temp - '0'; product = a * b * c * d * e; max = product; cout << product << endl; while(is.get(temp)){ a = b; b = c; c = d; d = e; e = temp - '0'; product = (a * b * c * d * e); if(product > max) max = product; cout << product << endl; } cout << max << endl; keep_window_open(); }
int main() { int max = 100; vector<int> primes; // start i at 2 for (int i=2; i<max; ++i) { bool result = prime(i); if (result == true) { primes.push_back(i); } } for (int i=0; i < primes.size(); ++i) { cout << "Nr. " << i << " - Prime: " << primes[i] << endl; } keep_window_open(); }
int main(){ std::vector<int> cash={1,2,3,4,5,6,7};//created a vector of size 7 with integer types to store cash values cout<<"How many one dollar bills do you have?\n"; //query the user for input and input directly into vector cash cin>>cash[0]; cout<<"How many two dollar bills do you have?\n"; cin>>cash[1]; cout<<"How many five dollar bills do you have?\n"; cin>>cash[2]; cout<<"How many ten dollar bills do you have?\n"; cin>> cash[3]; cout<<"How many twenty dollar bills do you have?\n"; cin>>cash[4]; cout<<"How many fifty dollar bills do you have?\n"; cin>> cash[5]; cout<<"How many Hundred dollar bills do you have?\n"; cin>> cash[6]; int total;// find total value by adding the components of the vector multiplyed by their corresponding cash value total=cash[0]+cash[1]*2+cash[2]*5+cash[3]*10+cash[4]*20+cash[5]*50+cash[6]*100; cout<<"You have "<<cash[0]<<" dollar bills\n";//output in a list the number of bills cout<<"You have "<<cash[1]<<" two dollar bills\n"; cout<<"You have "<<cash[2]<<" five dollar bills\n"; cout<<"You have "<<cash[3]<<" ten dollar bills\n"; cout<<"You have "<<cash[4]<<" twenty dollar bills\n"; cout<<"You have "<<cash[5]<<" fifty dollar bills\n"; cout<<"You have "<<cash[6]<<" hundred dollar bills\n\n"; cout<<"the value of all of your bills is "<<total<<" dollars.\n";//output the total keep_window_open(); return 0; }
int main(){ vector<int> primes; primes.push_back(2); int i = 3; bool divisible = false; while(primes.size() < 10001){ divisible = false; for(int j = 0; j < primes.size(); ++j){ if((i % primes[j]) == 0){ divisible = true; break; } } if(!divisible){ primes.push_back(i); } ++i; } //for(int k = 0; k < primes.size(); ++k){ // cout << primes[k] << ' '; //} cout << primes[primes.size() - 1]; cout << endl; keep_window_open(); }
int main() // C++ programs always start by executing the function "main" { cout << "Hello, programming!\n"; cout << "Here we go!\n"; keep_window_open(); return 0; }
int primary() { Rom r = romstr.get(); switch (r.type) { case '(': // handle '(' expression ')' { int d = expression(); r = romstr.get(); if (r.type != ')') error("')' expected"); return d; } case '{': // handle '(' expression ')' { int d = expression(); r = romstr.get(); if (r.type != '}') error("'}' expected"); return d; } case '-': return -primary(); case '+': return primary(); case number: // we use '8' to represent a number return r.ival;// return the number's value default: error("primary expected"); keep_window_open(); } return 0; }
int main() { string iname; cout << "Enter file name for search:\n"; cin >> iname; ifstream ifs{ iname }; if (!ifs) error("Unable to open file ", iname); cout << "Enter search word\n"; string sword; cin >> sword; int str_no = 0; while (ifs) { ++str_no; string str; getline(ifs, str); int pos = 0; while (pos >= 0) { pos = str.find(sword, pos+1); if (pos >=0) cout << "line " << str_no << '\t' << "'" << sword << "'" << " found at " << pos << " position" << endl; }; } keep_window_open(); return 0; }