declaring three double variables

Sep 16, 2010 at 4:12pm
Hello! I need help writing a program for a class, im having a lot of trouble comprehending with this because this is all new to me, the two main objectives are these:

It first declare three double variables: a, b, and c.
Then it asks the user for three numbers, and assign them to a, b and c respectively. Then it computes the sum and product of the three numbers.

Before printing out each of the sum and product, it should announce what is being calculated. Below is a sample input and output of the program, where the user entered the numbers 3.4, 5.6 and 4.3. There, user inputs are indicated with underlines for a presentation purpose.

Any help would be great, thanks in advance!
Sep 16, 2010 at 4:15pm
Why don't you have a go and show us what you come up with? Then we can advise you where you are going wrong.
Sep 16, 2010 at 4:24pm
This is what I have so far.

#include <iostream>
using namespace std;

int main()
{
double x = 5.4;
double y = 4.3;
double z;

z = x / (double) y;

cout << x << " + " << y << " = " << z << endl;



system("pause");
return 0;
}
Sep 16, 2010 at 7:23pm
Okay, well now you need to ask the user to supply the values of your doubles. You can do that using cin >> d; where d is a double.
Sep 17, 2010 at 7:30pm
im still very confused on how to go on from there, can you possibly help code the first part of it please :/ ?
Sep 17, 2010 at 7:42pm
How about starting at the beginning?

1. It first declare three double variables: a, b, and c.
2. Then it asks the user for three numbers, and assign them to a, b and c respectively.
3. Then it computes the sum and product of the three numbers.

If you have problems with any of these points, ask again.
If not, repeat this for the remaining points.
Sep 17, 2010 at 7:57pm
by the way, you can also declare these doubles by using:

double x=5.4, y=4.3, z;

and instead of system("pause") use cin.ignore(numeric_limits<streamsize>::max(), '\n');
This might seem intimidating, but it's a better way to do the same (and faster).. ( see: http://www.cplusplus.com/forum/articles/11153/ )

Also, in the line with z=x/(double) y; you can remove the (double)

Anyway, first try to do what Athar said, and if you need any extra help, feel free to ask :)

Hope it helps!

Cheers!
Sep 17, 2010 at 9:02pm
Hey guys, thanks for all the help, you've pushed me in the right direction. I just have two simple (noob) questions, how to I make the users input become underlined for the presentational purpose? Also, is there a way I can space out the lines in the debug screen so it doesn't look cluttered? thank you :)

Here is what I have:

#include <iostream>
using namespace std;

int main()
{
//Declare 3 double variables for input and another 2 for the sum and product
double gab1, gab2, gab3, sum, prod;

//ask for value 1
cout<<"Enter value 1: ";

//cin assigns the input to the variable
cin>>gab1;

cout<<"Enter value 2: ";
cin>>gab2;

cout<<"Enter value 3: ";
cin>>gab3;

// Sum of the three double variables

sum = gab1 + gab2 + gab3;

// Show the sum of the three double variables

cout<<"The sum of "<< gab1 << " and " <<gab2<<" and "<<gab3<<" = "<<sum<<"\n";

// Calculate and show the product
prod = gab1 * gab2 * gab3;

cout<<"The product of "<< gab1 << " and " <<gab2<<" and "<<gab3<<" = "<<prod<<"\n";

system("pause");
return 0;

}
Sep 17, 2010 at 9:15pm
I don't think you can underline, bold or make text italic with standard ASCII..
Sep 17, 2010 at 9:26pm
Try '\b' at the end of your cout statement right before grabbing input.
Each time \b is placed a blank line is inserted that gets overridden which each character entered.

ex: cout<<"Enter value 1: \b\b\b";

Will have 3 spaces of line in the console.

Edit: It might also be something like this:
ex: cout<<"Enter value 1: ___\b\b\b";

\b back spaces the cursor on each call.
Last edited on Sep 17, 2010 at 9:28pm
Topic archived. No new replies allowed.