954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Problems With My First Program

It is supposed to take a ceral box measurement in ounces and convert it to metric tons, as well as tell how many boxes it would take to equal a metric ton. Here it is.

#include <iostream>
using namespace std;

double ounces;

cout << "Enter the box weight in ounces and press enter";
cin >> ounces;

double weightInMetricTons, numberOfCerealBoxes;

weightInMetricTons = ounces/35273.92;
numberOfCerealBoxes = 35273.92/ounces;

cout << "The weight in metric tons is/n" 
     << weightInMetricTons << "and it takes/n"
	 << numberOfCerealBoxes << "to equal a metric ton";


For some reason when I hold the mouse over the line that says that weightInMetricTons and numberOfCerealBoxes are doubles, it says that they are both integers, (they are supposed to be doubles). When I hold the mouse over the variable ounces though, it says that it is a double. A double / a double should be a double, right?

The program fails to build. I tried starting at the end, and cutting sections out to see where the problem started, but I got all the way to the beginning so the only lines were:

#include
using namespace std;

and it still failed to build. Is this normal, or does that have to be some content in the program for it to actually build. Any answers to these questions would be greatly appreciated.

wolfkrug
Newbie Poster
15 posts since Jan 2010
Reputation Points: 17
Solved Threads: 1
 

The program fails to build most likely because you have no main() function

#include <iostream>
using namespace std;

int main (int argc, char** argv)
{
  // your program here
  return 0; // successful execution
}
ShawnCplus
Code Monkey
Team Colleague
1,583 posts since Apr 2005
Reputation Points: 526
Solved Threads: 268
 

I'm not seeing a main function anywhere. do you have this code in a main function?

NathanOliver
Veteran Poster
1,084 posts since Apr 2009
Reputation Points: 215
Solved Threads: 189
 

No that is the whole code. I should probably go learn what a main function is.

wolfkrug
Newbie Poster
15 posts since Jan 2010
Reputation Points: 17
Solved Threads: 1
 

http://www.cplusplus.com/doc/tutorial/program_structure/

From this link:

int main ()

This line corresponds to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. It does not matter whether there are other functions with other names defined before or after it - the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function.

Stick your code in a main function and try compiling and running it again.

#include <iostream>
using namespace std;


int main ()
{
	double ounces;

	cout << "Enter the box weight in ounces and press enter";
	cin >> ounces;

	double weightInMetricTons, numberOfCerealBoxes;

	weightInMetricTons = ounces/35273.92;
	numberOfCerealBoxes = 35273.92/ounces;

	cout << "The weight in metric tons is/n" 
		 << weightInMetricTons << "and it takes/n"
		 << numberOfCerealBoxes << "to equal a metric ton";
		 
    return 0;
}
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

Thanks I actually think I was at that exact link a few minutes ago. It made my program run and also made my doubles show up as doubles.

wolfkrug
Newbie Poster
15 posts since Jan 2010
Reputation Points: 17
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You