Program 8

Jun 16, 2014 at 8:52am
Hello,

I decided to write a program like follows:

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 smaller, larger, sum, difference, product and ratio of these values and report them to the user.

Here is something I put together but I get plenty of error messages when trying to compile....



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<cmath>
using namespace std

int main()

{


int val1=0;
int val2=0;
cout<<"Enter two integer values:\n";
cin>>val1>>val2

if (val1>val2) { cout>>" "<<val2<<" is smaller than "<<val1<<" and "<<val1<<" is bigger than "<<val2<<" \n"; }

if (val1<val2) { cout>>" "<<val1<<" is smaller than "<<val2<<" and "<<val2<<" is bigger than "<<val1<<" \n"; }

cout>>"The sum of "<<val1<<" and "<<val2<<" is "<<val1<< + <<val2<<" \n"; 
cout>>"The difference of "<<val1<<" and "<<val2<<" is "<<val1<< - <<val2<<" \n"; 
cout>>"The product of "<<val1<<" and "<<val2<<" is "<<val1<< * <<val2<<" \n"; 
cout>>"ratio of "<<val1<<" and "<<val2<<" is "<<val1<< : <<val2<<" \n"; 


}
Jun 16, 2014 at 9:10am

Missing semicolon at the end of line 6 and 16.

cout uses << and not >>. You also have a lot of extra << in your cout statements especially where you attempt to perform the calculations - those are not needed.

For example, line 22 should look like this..

cout<<"The sum of "<<val1<<" and "<<val2<<" is "<< val1 + val2 <<" \n";

EDIT: cout's on lines 18 and 20 also need >> changed to <<
Last edited on Jun 16, 2014 at 9:12am
Jun 16, 2014 at 10:59am
Yep though now getting error message...

In function ‘int main()’:
Documents/program8.cpp:26:54: error: found ‘:’ in nested-name-specifier, expected ‘::’
cout<<"ratio of "<<val1<<" and "<<val2<<" is "<<val1 : val2<<" \n";
^
Documents/program8.cpp:26:49: error: ‘val1’ is not a class or namespace
cout<<"ratio of "<<val1<<" and "<<val2<<" is "<<val1 : val2<<" \n";
^

Last edited on Jun 16, 2014 at 11:00am
Jun 16, 2014 at 12:24pm

You cant use : on line 25, its not a operator like + / - * %
Jun 16, 2014 at 12:36pm
How should I implement ratio then? Is it meant to be something like the ratio of 4 and 3 is 4:3?

Jun 16, 2014 at 12:43pm
The division operator /

Since you're dividing integers, you'll get an integer division result, which will be an integer (any remainder is discarded).
Topic archived. No new replies allowed.