I am so sorry that i have been asking to many questions these days...
After all the input, here's the last thing to do on my project that i got lost completely.
I have to produce a program that use the function call technique to combine two things using if else statement. The program consist of:
1) English to metric system conversion
2) metric to English system conversion
The user will input symbol 1 or 2 to determine the program to execute either program 1) or program 2).
How do i do that?
I plug all things that i did so far, but, still can't pass through the compiler with the if-else statement. Can someone help me to point out my mistake and give me some better ideas on how to do/improve this please?
NB: Please ignore the repetition usage of the using namespace std; please. The book told me to do so! (it's Savitch c++).
#include <iostream>
#include <cmath>
using namespace std;
double do_math(int meter);
double conversion(int feet, int inches);
void print_results(int output1, int output2);
void show_results(int results1, double results2);
int main(){
int m, f, i;
double final_pounds, final_inches, pounds_convert, final_meter, final_cm, end_meter;
const int inch = 12, meter = 100;
char symbol;
cout<<"Welcome!Enter 1 to proceed to a metric system to English system conversion or enter 2 to proceed to an English system to metric system conversion."<<endl;
cin>>symbol;
void print_results(int output1, int output2)
{
using namespace std;
cout<<"The value in metric systems are "<<output1<< " lb and "<<output2<< " inch "<<endl;
return;
}
void introduction()
{
using namespace std;
cout<<"Welcome! This program converts the English sytems units to metric system units."<<endl;
}
void get_numbers(int& input1, int& input2)
{
using namespace std;
cout<<"Enter the number of feet and inches integers separately: "<<endl;
cin>>input1;
cin>>input2;
}
double conversion(int feet, int inches)
{
using namespace std;
There is no "do" statement in C++. There is a "do-while" statement, but I don't think that's what you want. Try your program without the "do" statements; I don't think you need to replace them with anything.
Well, ..... actually, i need to place to repeat request somewhere in the loop.
I've tried place it everywhere up in the main, but it didn't work. Now, i'm placing under the showresults function call, but it cannot execute for a new calculation when i enter Y. Why is that? where should i place it?
To the right of the box you put your reply in, there's a bunch of formatting icons. One looks like "<>" Press that, and put your code in the middle of what appears.
Here's my final code that has a problem with the REPEAT LOOP.
It doesn't work. I need it to execute back to the very beginning (where i have to choose to input symbol 1 or 2) for every time it ask for a repeat calculation.
Please have a look, on where i should have use it.
Oh, i used toupper, so it doesn't matter for either upper case or lower case letter.
#include <iostream>
#include <cmath>
usingnamespace std;
void intro();
void introduction();
void get_value(int& value1);
void get_numbers(int& input1, int& input2);
double do_math(int meter);
double conversion(int feet, int inches);
void print_results(int output1, int output2);
void show_results(int results1, double results2);
int main(){
int m;
double final_pounds, final_inches, pounds_convert;
constint inch = 12;
int choice;
do{
cout<<"Welcome!Enter 1 to proceed to a metric system to English system conversion or enter 2 to proceed to an English system to metric system conversion."<<endl;
cin>>choice;
if (choice == 1){
intro();
get_value(m);
pounds_convert = do_math(m);
final_pounds =(pounds_convert);
final_inches =(pounds_convert - (floor(final_pounds)))*inch;
print_results(final_pounds, final_inches);
}
else (choice == 2);
{
int f, i;
double final_meter, final_cm, end_meter;
constint meter = 100;
introduction();
get_numbers(f,i);
end_meter = conversion(f,i);
final_meter =(end_meter);
final_cm =(end_meter - (floor(final_meter)))*meter;
show_results(final_meter, final_cm);
}
} while (choice != 3);
return (0);
}
void intro()
{
usingnamespace std;
cout<<"Welcome! This program converts the metric sytems units to English system units."<<endl;
}
void get_value(int& value1 )
{
usingnamespace std;
cout<<"Enter the number of meter in integers: "<<endl;
cin>>value1;
}
double do_math(int meter)
{
usingnamespace std;
double pounds_convert;
doubleconst foot = 0.3048;
pounds_convert = meter/foot;
return pounds_convert;
}
void print_results(int output1, int output2)
{
usingnamespace std;
char action;
cout<<"The value in metric systems are "<<output1<< " lb and "<<output2<< " inch "<<endl;
do{
cout<<"Do you want to repeat a new calculation? [Y/y/N/n]"<<endl;
action = toupper (action);
cin>>action;
} while (action == 'n');
return;
}
void introduction()
{
usingnamespace std;
cout<<"Welcome! This program converts the English sytems units to metric system units."<<endl;
}
void get_numbers(int& input1, int& input2)
{
usingnamespace std;
cout<<"Enter the number of feet and inches integers separately: "<<endl;
cin>>input1;
cin>>input2;
}
double conversion(int feet, int inches)
{
usingnamespace std;
double total_meter, end_meter;
doubleconst foot = 0.3048, inch = 12;
total_meter = feet+(inches/inch);
end_meter = total_meter*foot;
return end_meter;
}
void show_results(int results1, double results2)
{
usingnamespace std;
char action;
cout<<"The value in metric systems are "<<results1<< " m and "<<results2<< "cm"<<endl;
do{
cout<<"Do you want to repeat a new calculation? [Y/y/N/n]"<<endl;
action = toupper (action);
cin>>action;
} while (action == 'n');
return;
}
OK, I have to leave now, but...you have some uninitialized variables, and your processing of the choice logic isn't comprehensive. (What if choice = 4?) And you're still not testing your y/n correctly.
I'll check in later to see how you're doing with this.
Not in the code you posted above, you didn't. Besides, do you really want to continue only if choice is less than 2? That will only work for a value of 1 or less.
I also noticed that you're casting your user's answer to uppercase BEFORE you read it. That's not going to do much for you.
#include <iostream>
#include <cmath>
usingnamespace std;
void intro();
void introduction();
void get_value(int& value1);
void get_numbers(int& input1, int& input2);
double do_math(int meter);
double conversion(int feet, int inches);
void print_results(int output1, int output2);
void show_results(int results1, double results2);
int main(){
int m;
double final_pounds, final_inches, pounds_convert;
constint inch = 12;
int choice = 2;
do{
cout<<"Welcome! Enter 1 to proceed to a metric system to English system conversion or enter 2 to proceed to an English system to metric system conversion."<<endl;
cin>>choice;
if (choice == 1){
intro();
get_value(m);
pounds_convert = do_math(m);
final_pounds =(pounds_convert);
final_inches =(pounds_convert - (floor(final_pounds)))*inch;
print_results(final_pounds, final_inches);
}
else (choice == 2);
{
int f, i;
double final_meter, final_cm, end_meter;
constint meter = 100;
introduction();
get_numbers(f,i);
end_meter = conversion(f,i);
final_meter =(end_meter);
final_cm =(end_meter - (floor(final_meter)))*meter;
show_results(final_meter, final_cm);
}
} while (choice <= 2);
return (0);
}
void intro()
{
usingnamespace std;
cout<<"Welcome! This program converts the metric sytems units to English system units."<<endl;
}
void get_value(int& value1 )
{
usingnamespace std;
cout<<"Enter the number of meter in integers: "<<endl;
cin>>value1;
}
double do_math(int meter)
{
usingnamespace std;
double pounds_convert;
doubleconst foot = 0.3048;
pounds_convert = meter/foot;
return pounds_convert;
}
void print_results(int output1, int output2)
{
usingnamespace std;
char action;
cout<<"The value in metric systems are "<<output1<< " lb and "<<output2<< " inch "<<endl;
do{
cout<<"Do you want to repeat a new calculation? [Y/y/N/n]"<<endl;
cin>>action;
action = toupper (action);
} while (action == 'y' || action != 'n');
return;
}
void introduction()
{
usingnamespace std;
cout<<"Welcome! This program converts the English sytems units to metric system units."<<endl;
}
void get_numbers(int& input1, int& input2)
{
usingnamespace std;
cout<<"Enter the number of feet and inches integers separately: "<<endl;
cin>>input1;
cin>>input2;
}
double conversion(int feet, int inches)
{
usingnamespace std;
double total_meter, end_meter;
doubleconst foot = 0.3048, inch = 12;
total_meter = feet+(inches/inch);
end_meter = total_meter*foot;
return end_meter;
}
void show_results(int results1, double results2)
{
usingnamespace std;
char action;
cout<<"The value in metric systems are "<<results1<< " m and "<<results2<< "cm"<<endl;
do{
cout<<"Do you want to repeat a new calculation? [Y/y/N/n]"<<endl;
cin>>action;
action = toupper (action);
} while (action == 'y' || action != 'n');
return;
}
Seems to work for me. What is the exact input you're giving the program that's not working for you?
And, your y/n test still isn't right. Besides the uppercase issue (which you still haven't solved, you're not doing anything with action after you test it in the do-while loop. You exit the routine, and the value in action is lost.
Here is the problem, this is how it executes my repeat loop:
1 2 3 4 5 6 7 8 9 10 11 12 13
Welcome! Enter 1 to proceed to a metric system to English system conversion or enter 2 to proceed to an English system to metric system conversion.
1
Welcome! This program converts the metric sytems units to English system units.
Enter the number of meter in integers:
10
The value in metric systems are 32 lb and 9 inch
Do you want to repeat a new calculation? [Y/y/N/n]
y
Do you want to repeat a new calculation? [Y/y/N/n]
y
Do you want to repeat a new calculation? [Y/y/N/n]
n
Do you want to repeat a new calculation? [Y/y/N/n]
that isn't the problem. As i said. I have used toupper. So, it will handle that problem. My ONLY problem is where am i supposed to place my REPEAT loop in my program. That's all. why...