...

View Full Version : C++ Code Help



Skeddels
10-18-2008, 12:00 AM
What's wrong with this code and how can I fix it?


#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[]){
//-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
//Farenheit = Celsius * (212 - 32)/100 + 32
int nCelsius;
cout << "Enter the temperature in Celsius:"; << endl;
cin >> nCelsius >> endl;
//-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
//Calculate convershion factor for Celsius to Farneheit
int nFactor;
nFactor = 212 - 32
//-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
//Calculate convershion factor for Farenheit to Celsius
int nFarenheit;
nFarenehit = nFactor * nCelsius/100 + 32;
//-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
//Output the results
cout << "Farenheit value is:" << endl;
cin >> nFarenheit >> endl;
//-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
//Keep the window open
system("PAUSE");
return 0;
}


The compiler says that there's a problem with line 10:

10 C:\Program Files\MyProjects\Test Project\main\temperature.cpp expected primary-expression before '<<' token

oesxyl
10-18-2008, 04:38 AM
try to correct the marked parts:

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[]){
//-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
//Farenheit = Celsius * (212 - 32)/100 + 32
int nCelsius;
cout << "Enter the temperature in Celsius:"; << endl; // remove that ';'
cin >> nCelsius >> endl;
//-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
//Calculate convershion factor for Celsius to Farneheit
int nFactor;
nFactor = 212 - 32 // <<< here is missing a ;
//-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
//Calculate convershion factor for Farenheit to Celsius
int nFarenheit; // 34.5 is not of type int
nFarenehit = nFactor * nCelsius/100 + 32;
//-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
//Output the results
cout << "Farenheit value is:" << endl;
cin >> nFarenheit >> endl;
//-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_
//Keep the window open
system("PAUSE");
return 0;
}


regards

Skeddels
10-18-2008, 02:48 PM
Oh I see. The book that I'm using says that it's for Visual C++, so I assumed that whatever it said would work for Dev C++. I typed it exactly how it was written in the book, and overlooked that integer error.

marek_mar
10-19-2008, 02:02 PM
You need a better book. This example won't work in any C++ compiler.


cin >> nCelsius >> endl;

cin >> endl will never compile. Same goes for:


//Output the results
cout << "Farenheit value is:" << endl;
cin >> nFarenheit >> endl;

which will definitely not output any result.


int nFarenheit; // 34.5 is not of type int
nFarenehit = nFactor * nCelsius/100 + 32;

Changing the type of nFarenheit to float/double is not enough if you want a floating point result.
In the division


nCelsius/100

you divide int by int which will return you an int, not a floating point number. It should be


nCelsius/double(100)
// or
nCelsius/float(100)

(you could also change the type of nCelsius to float or double)

I don't see how any book, which has such errors, can teach anything properly.

ghell
10-19-2008, 03:26 PM
Well, AFAIK the conversion goes like this:
f = c / (5.0/9.0) + 32;

If you invert that 5/9 and then multiply rather than divide, you get
f = c * 1.8 + 32


This means the code (including both conversions from C to F and from F to C) should look something like this:

#include <iostream>
using namespace std;

double ftoc(double f) { return (f - 32) / 1.8; }
double ctof(double c) { return c * 1.8 + 32; }

int main()
{
double c, f;

// Prompt for a temperature
cout << "Enter the temperature in Celsius: ";
cin >> c;

// Covert the temperature
f = ctof(c);

// Display the result
cout << c << " C converts to " << f << " F" << endl;

return 0;
}


And when I say it should look something like that, I also mean that you should get rid of all the crap that you put in there just to make it unreadable.

A sample output:
$ ctof
Enter the temperature in Celsius: 26
26 C converts to 78.8 F
$(you can confirm this with google calculator: http://www.google.com/search?q=26+celsius+in+fahrenheit )



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum