Multiplying Program

This program is supposed to read in two numbers and dividing the large

Page 1 of 1

4 Replies - 2563 Views - Last Post: 28 November 2008 - 09:52 PM Rate Topic: -----

#1 cball1321  Icon User is offline

  • New D.I.C Head

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

Multiplying Program

Post icon  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  Icon User is offline

  • code > sword
  • member icon

Reputation: 990
  • View blog
  • 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

Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6053
  • View blog
  • Posts: 23,488
  • 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;
}

Was This Post Helpful? 1
  • +
  • -

#4 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,140
  • 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

Was This Post Helpful? 1
  • +
  • -

#5 cball1321  Icon User is offline

  • New D.I.C Head

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

Re: Multiplying Program

Posted 28 November 2008 - 09:52 PM

View PostKYA, 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

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1