# Multiplying Program

Page 1 of 1

## 4 Replies - 3058 Views - Last Post: 28 November 2008 - 09:52 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=74023&amp;s=762ee3369c4ff4c6122bffdca831d84b&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 cball1321

Reputation: 0
• Posts: 33
• Joined: 27-September 08

# Multiplying Program

Posted 28 November 2008 - 06:07 PM

```  The user defined funtion must be called twice. Once to read in and error check each number.  I dont know how to get the function to read the prompt for the second number.  The other problem I'm having is that no matter what number I type in I get either 288 or 2983560.

# include <iostream> // for input/ output
using namespace std;

int numbers ( int num, int num1 )
{
string str1 = "Enter the first number to be multiplied. ";
cout << str1;
cin >> num;

while ( num <= 0 )
{
cout << "Error !! Re-enter a positive, non-zero number. ";
cin >> num;
cout << endl;
}

return (num);

string str2 = "Enter the second number to be multiplied. ";

if (num == num1)
{
cout << str2;
cin >> num;
}

while ( num <= 0 )
{
cout << "Error !! Re-enter a positive, non-zero number. ";
cin >> num;
cout << endl;
}

return (num);
}

int main ()
{
// variables
int num1, num2, num;
int smaller;
int larger;

cout << "This program will multiply two numbers. " << endl << endl;

num1 = numbers ( num, num1 );

num2 = numbers ( num, num1 );

cout << num1 << " " << num2 << endl << endl;

if ( num1 > num2 )
{
larger == num1;
cout << num1 << endl;
}
else
{
larger == num2;
cout << num2 << endl;
}

if ( num1 < num2 )
{
smaller == num1;
cout << num1 << endl;
}
else
{
smaller == num2;
cout << num2 << endl;
}

for ( larger == num; larger >= 1; larger /= 2 )
cout << larger << endl;

cout << endl << endl;
system ("pause");
return 0;
}

```

Is This A Good Question/Topic? 0

## Replies To: Multiplying Program

### #2 janotte

• code > sword

Reputation: 991
• Posts: 5,141
• Joined: 28-September 06

## Re: Multiplying Program

Posted 28 November 2008 - 06:21 PM

First step is to use the compiler warnings to point you to problems:
dic.cpp: In function ‘int main()’:
dic.cpp:57: warning: statement has no effect
dic.cpp:62: warning: statement has no effect
dic.cpp:68: warning: statement has no effect
dic.cpp:73: warning: statement has no effect
dic.cpp:77: warning: statement has no effect

Remember there is a fundamental difference between "=" and "==".

This post has been edited by janotte: 28 November 2008 - 06:21 PM

• Saucy!

Reputation: 6246
• Posts: 24,014
• Joined: 23-August 08

## Re: Multiplying Program

Posted 28 November 2008 - 06:26 PM

After heeding janotte's advice, call this function twice and assign its return value to different variables.
```int get_number_to_multiply(void)
{
int num = 0;
cin >> num;
while (num <= 0)
{
cout << "Error !! Re-enter a positive, non-zero number. ";
cin >> num;
}

return num;
}
```

### #4 KYA

• Wubba lubba dub dub!

Reputation: 3202
• Posts: 19,232
• Joined: 14-September 07

## Re: Multiplying Program

Posted 28 November 2008 - 06:30 PM

Your question got cut off in the title. In the future please put in the post itself.

You'll have to explain some of your intent as it is unclear, however here is code that multiplies two numbers and determines which is larger, then divides the larger amount by 2 until it equals one:

```//The user defined funtion must be called twice. Once to read in and error check each number.
//I dont know how to get the function to read the prompt for the second number.
//The other problem I'm having is that no matter what number I type in I get either 288 or 2983560.

# include <iostream> // for input/ output
using namespace std;

int numbers ()
{
int num = 0;
string str1 = "Enter number to be multiplied. ";
cout << str1;
cin >> num;

while ( num <= 0 )
{
cout << "Error !! Re-enter a positive, non-zero number. ";
cin >> num;
cout << endl;
}
//function can only have one return value per iteration, you had multiple returns earlier
return (num);
}

int main ()
{
// variables
int num1, num2, num;
int smaller;
int larger;

cout << "This program will multiply two numbers. " << endl << endl;

num1 = numbers (); //obtain first num
num2 = numbers (); //obtain second

cout << num1 << " " << num2 << endl << endl;

//multiply??? did not see it anywhere
num = num1 * num2;

cout << "Multiplied value: " << num << endl;

//don't need multiple ifs for the same check
if ( num1 > num2 )
{
larger = num1;
smaller = num2;
cout << "larger: " << num1 << endl;
}
else
{
larger = num2;
smaller = num1;
cout << "larger: " << num2 << endl;
}
cout << endl;

//don't really need an initializing condition here, divide by 2 until larger is 1
//Don't we want to divide by the multiplied value????
//you'll need to explain that a bit
for (; larger >= 1; larger /= 2 )
cout << larger << endl;

cout << endl << endl;
system ("pause");
return 0;
}

```

edited for typos

This post has been edited by KYA: 28 November 2008 - 10:15 PM

### #5 cball1321

Reputation: 0
• Posts: 33
• Joined: 27-September 08

## Re: Multiplying Program

Posted 28 November 2008 - 09:52 PM

KYA, on 28 Nov, 2008 - 05:30 PM, said:

Your question got cut off in the title. In the future please put in the post itself.

You'll to explain some of your intent as it is unclear, however here is code that multiplies two numbers and determines which is larger, then divides the larger amount by 2 until it equals one:

The user defined function must prompt and error check the two numbers entered. I have no problem with the prompt for the first number. It's the second thats giving me trouble. The multiplication, which I havent quite gotten to yet, goes as follows: The larger and smaller numbers are determined then the larger is divided by 2 until it is one. The smaller number is doubled until the larger is down to one.

```//The user defined funtion must be called twice. Once to read in and error check each number.
//I dont know how to get the function to read the prompt for the second number.
//The other problem I'm having is that no matter what number I type in I get either 288 or 2983560.

# include <iostream> // for input/ output
using namespace std;

int numbers ()
{
int num = 0;
string str1 = "Enter number to be multiplied. ";
cout << str1;
cin >> num;

while ( num <= 0 )
{
cout << "Error !! Re-enter a positive, non-zero number. ";
cin >> num;
cout << endl;
}
//function can only have one return value per iteration, you had multiple returns earlier
return (num);
}

int main ()
{
// variables
int num1, num2, num;
int smaller;
int larger;

cout << "This program will multiply two numbers. " << endl << endl;

num1 = numbers (); //obtain first num
num2 = numbers (); //obtain second

cout << num1 << " " << num2 << endl << endl;

//multiply??? did not see it anywhere
num = num1 * num2;

cout << "Multiplied value: " << num << endl;

//don't need multiple ifs for the same check
if ( num1 > num2 )
{
larger = num1;
smaller = num2;
cout << "larger: " << num1 << endl;
}
else
{
larger = num2;
smaller = num1;
cout << "larger: " << num2 << endl;
}
cout << endl;

//don't really need an initializing condition here, divide by 2 until larger is 1
//Don't we want to divide by the multiplied value????
//you'll need to explain that a bit
for (; larger >= 1; larger /= 2 )
cout << larger << endl;

cout << endl << endl;
system ("pause");
return 0;
}

```

edited for typos

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }