- #1
Heisenberg.
- 68
- 0
So I have been teaching myself some c++, and as I tried to do an example my guide book showed me: that is where things went awry. The book I have been using is Programming:Principles and Practice Using C++ By Bjarne Stroustrup. I used the solution for the example where I got stuck on a certain part - I did not understand exactly what the book was doing at that part, but threw it in anyway. Even with the solution as a guide, my code still seems to produce errors. Here is the code:
//Write a program that prompts the user to enter two integer values.
//Store these values in int variables named val1 and val2. Write your program to determine the smallest,
//largest, sum, difference, product, and ratio of these values and report them to the user.
#include <iostream>
#include <conio.h>
using namespace std;
int main ()
{
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) << val1 << "is smallest. \n";
if (val2 < val1) << val2 << "is smallest. \n";
if (val1==val2) cout << val1 << "and" << val2 << "is equal.\n";
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";
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";
}
catch (runtime_error e) {
cout << e.what () << "\n";
getch ();
return 0;
}
The first error is this if (!cin) error ( "something went bad with the read"); - this is what the solutions say, I haven't seen this before and am not sure how to go about fixing this program.
Thank-You in advance!
//Write a program that prompts the user to enter two integer values.
//Store these values in int variables named val1 and val2. Write your program to determine the smallest,
//largest, sum, difference, product, and ratio of these values and report them to the user.
#include <iostream>
#include <conio.h>
using namespace std;
int main ()
{
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) << val1 << "is smallest. \n";
if (val2 < val1) << val2 << "is smallest. \n";
if (val1==val2) cout << val1 << "and" << val2 << "is equal.\n";
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";
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";
}
catch (runtime_error e) {
cout << e.what () << "\n";
getch ();
return 0;
}
The first error is this if (!cin) error ( "something went bad with the read"); - this is what the solutions say, I haven't seen this before and am not sure how to go about fixing this program.
Thank-You in advance!