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 !! Reenter a positive, nonzero 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 !! Reenter a positive, nonzero 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; }
Multiplying Program
This program is supposed to read in two numbers and dividing the large
Page 1 of 14 Replies  2595 Views  Last Post: 28 November 2008  09:52 PM
#1
Multiplying Program
Posted 28 November 2008  06:07 PM
Replies To: Multiplying Program
#2
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 "==".
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
#3
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 !! Reenter a positive, nonzero number. "; cin >> num; } return num; }
#4
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:
edited for typos
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 !! Reenter a positive, nonzero 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
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.
edited for typos
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 !! Reenter a positive, nonzero 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
