Whats wrong with this?

This is a discussion on Whats wrong with this? within the C++ Programming forums, part of the General Programming Boards category; The following program LOOKS to be working fine but not sure why is displaying wrong, when I input either F ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    36

    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

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    we can't due to the lack of a primetest function

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    36
    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

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You also have the wrong main() function in your example.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    36
    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;
    }
    }
    }

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Why is fahren a float but celsius an int? With both directions possible, this doesn't make sense.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    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

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    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.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > 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.

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    36
    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;
    }
    }
    }

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    36
    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.

  12. #12
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    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;
    		}
    	}
    }

  13. #13
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Ah, remove the cout << end1; from the F section

  14. #14
    Registered User
    Join Date
    Mar 2006
    Posts
    36
    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!!!!!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21