-
Whats wrong with this?
The following program LOOKS to be working fine but not sure why is displaying wrong, when I input either F or C, it displays:
Degree Degree
celsius fahrenehit
------- ------------
nnn nnnn
Code:
#include <iostream>
#include <iomanip>
using namespace std;
void primetest(int);
int main ()
{
int number;
cout << "You have selected the prime option."
<< "\nEnter any number you wish to test.";
cin >> number;
primetest(number);
return 0;
}
void convert (int tempmin, int tempmax, char choice)
{
if (choice == 'C' || choice == 'c') {
const int step = 1;
double fahren;
int celsius;
cout << "Degree Degrees\n"
<< "Celsius Fahrenheit\n"
<< "------- ----------\n";
celsius = tempmin;
cout << setiosflags (ios :: showpoint) << setiosflags (ios :: fixed)
<< setprecision (2);
while (celsius <=tempmax)
{
fahren = (9.0/5.0) * celsius + 32.0; cout << setw (4) << celsius
<< setw (13) << fahren << endl; celsius = celsius + step;
}
}
else if (choice == 'f' || choice == 'F')
{
const int increase = 1;
int fahren;
double celsius;
cout << "Degree Degrees\n"
<< "Fahrenheit Celsius\n"
<< "------ -------\n";
fahren = tempmin;
cout << setiosflags (ios :: showpoint) << setiosflags (ios :: fixed) << setprecision (2);
while (fahren <=tempmax)
{
celsius = (fahren-32) *(5.0/9.0);
cout << setw (4) << fahren
<< setw (13) << celsius << endl;
fahren = fahren + increase;
cout << endl;
}
}
}
sorry for the mess^^^. You should be able to just copy/paste and it should run for ya
-
we can't due to the lack of a primetest function
-
Oh, im sorry, use this instead of primetest, completely get rid of that and past this in its place:
void convert (int, int, char);
sorry again
-
You also have the wrong main() function in your example.
-
ok, sorry for all the confusion, I figured it out on my own, but now I ran into a new problem:
when i use C, everything is line to line back to back
but if I use F, a line is skipped b/t the lines
If you copy past this, it WILL work now.
Code:
#include <iostream>
#include <iomanip>
using namespace std;
void convert (int tempmin, int tempmax, char ForC);
int main ()
{
int tempmin, tempmax;
char ForC;
cout << "You have selected the conversion option.\n"
<< "Enter the starting range.\n";
cin >> tempmin;
cout << "Enter the ending range.\n";
cin >> tempmax;
cout << "Enter C to convert from celsius to fahrenheit or,\n"
<< "Enter F to convert from fahrenheit to celsius.\n";
cin >> ForC;
convert (tempmin, tempmax, ForC);
return 0;
}
void convert (int tempmin, int tempmax, char ForC)
{
if (ForC == 'C' || ForC == 'c') {
const int step = 1;
double fahren;
int celsius;
cout << "Degree Degrees\n"
<< "Celsius Fahrenheit\n"
<< "------- ----------\n";
celsius = tempmin;
cout << setiosflags (ios :: showpoint) << setiosflags (ios :: fixed)
<< setprecision (2);
while (celsius <=tempmax)
{
fahren = (9.0/5.0) * celsius + 32.0; cout << setw (4) << celsius
<< setw (13) << fahren << endl; celsius = celsius + step;
}
}
else if (ForC == 'f' || ForC == 'F')
{
const int increase = 1;
int fahren;
double celsius;
cout << "Degree Degrees\n"
<< "Fahrenheit Celsius\n"
<< "------ -------\n";
fahren = tempmin;
cout << setiosflags (ios :: showpoint) << setiosflags (ios :: fixed) << setprecision (2);
while (fahren <=tempmax)
{
celsius = (fahren-32) *(5.0/9.0);
cout << setw (4) << fahren
<< setw (13) << celsius << endl;
fahren = fahren + increase;
cout << endl;
}
}
}
-
Why is fahren a float but celsius an int? With both directions possible, this doesn't make sense.
-
Your indentation needs a little tweaking - its a little off
Just a guess, but if the lines are not matching use setw defined in #iomanip
-
EDIT use double if you are working with temp as I find it more relaible than float.
If you have two variables differenent types, like cornbee said, this could be the problem.
I ran it on MSVC++2003 and got a simular prob.
-
> sorry for the mess^^^. You should be able to just copy/paste and it should run for ya
Actually, it's your problem, not mine.
Because neatness and consistency of indentation in itself will fix so many problems for you from the outset, it's simply a waste of time even trying to understand poorly formatted code. Most people have got better things to do than wade through such horrid code.
Even something as basic as compiling what you posted, or posting what you compiled would be a start.
-
Sorry for everything guys, i formatted it much neater and tested it. Like I said, it'll be easier to see the problem I am getting if you copy / paste and test it out.
Code:
#include <iostream>
#include <iomanip>
using namespace std;
void convert (int tempmin, int tempmax, char ForC);
int main ()
{
int tempmin, tempmax;
char ForC;
cout << "You have selected the conversion option.\n"
<< "Enter the starting range.\n";
cin >> tempmin;
cout << "Enter the ending range.\n";
cin >> tempmax;
cout << "Enter C to convert from celsius to fahrenheit or,\n"
<< "Enter F to convert from fahrenheit to celsius.\n";
cin >> ForC;
convert (tempmin, tempmax, ForC);
return 0;
}
void convert (int tempmin, int tempmax, char ForC)
{
if (ForC == 'C' || ForC == 'c')
{
const int step = 1;
double fahren;
int celsius;
cout << "Degree Degrees\n"
<< "Celsius Fahrenheit\n"
<< "------- ----------\n";
celsius = tempmin;
cout << setiosflags (ios :: showpoint) << setiosflags (ios :: fixed)
<< setprecision (2);
while (celsius <=tempmax)
{
fahren = (9.0/5.0) * celsius + 32.0;
cout << setw (4) << celsius
<< setw (13) << fahren << endl;
celsius = celsius + step;
}
}
else if (ForC == 'f' || ForC == 'F')
{
const int increase = 1;
int fahren;
double celsius;
cout << "Degree Degrees\n"
<< "Fahrenheit Celsius\n"
<< "------ -------\n";
fahren = tempmin;
cout << setiosflags (ios :: showpoint) << setiosflags (ios :: fixed)<< setprecision (2);
while (fahren <=tempmax)
{
celsius = (fahren-32) *(5.0/9.0);
cout << setw (4) << fahren
<< setw (13) << celsius << endl;
fahren = fahren + increase;
cout << endl;
}
}
}
-
Can anyone out there help me??
once again, problem:
when I put either F, there is a gap b/t the lines but w/ C, everything is perfect.
Any help would be much appreciated.
-
here's the program formatted
Code:
#include <iostream>
#include <iomanip>
using namespace std;
void convert (int tempmin, int tempmax, char ForC);
int main ()
{
int tempmin, tempmax;
char ForC;
cout << "You have selected the conversion option.\n"
<< "Enter the starting range.\n";
cin >> tempmin;
cout << "Enter the ending range.\n";
cin >> tempmax;
cout << "Enter C to convert from celsius to fahrenheit or,\n"
<< "Enter F to convert from fahrenheit to celsius.\n";
cin >> ForC;
convert (tempmin, tempmax, ForC);
return 0;
}
void convert (int tempmin, int tempmax, char ForC)
{
if (ForC == 'C' || ForC == 'c')
{
const int step = 1;
double fahren;
int celsius;
cout << "Degree Degrees\n"
<< "Celsius Fahrenheit\n"
<< "------- ----------\n";
celsius = tempmin;
cout << setiosflags (ios :: showpoint) << setiosflags (ios :: fixed)
<< setprecision (2);
while (celsius <=tempmax)
{
fahren = (9.0/5.0) * celsius + 32.0;
cout << setw (4) << celsius
<< setw (13) << fahren << endl;
celsius = celsius + step;
}
}
else if (ForC == 'f' || ForC == 'F')
{
const int increase = 1;
int fahren;
double celsius;
cout << "Degree Degrees\n"
<< "Fahrenheit Celsius\n"
<< "------ -------\n";
fahren = tempmin;
cout << setiosflags (ios :: showpoint) << setiosflags (ios :: fixed)<< setprecision (2);
while (fahren <=tempmax)
{
celsius = (fahren-32) *(5.0/9.0);
cout << setw (4) << fahren
<< setw (13) << celsius << endl;
fahren = fahren + increase;
cout << endl;
}
}
}
-
Ah, remove the cout << end1; from the F section
-
Oh!!!! thanks so much madcow, i salute you 100 %! Glad you arent one of those people who follow the crowd who continue the hateage on me, im just a newb lookin for help :P
thanks so much again!!!!!! :D