• 0

c++ .net help


Question

ok i'm writing some code for my cs class but i'm getting errors i think shouldn't be there. it builds, but when i debug i get "distkilo is used without being defined". will someone compile the code below and let me know if it works? using vc++ .net if that helps. thanks.

<pre>

#include <iostream.h>



int CalcDistance(int, int);

int CalcKilometers(int);



int main(void)



{

    // Declarations of 4 variables with comments for each

	int dist; // integer variable for distance

	int rate; // integer variable for rate (speed)

	int time; // integer variable for time

	int distkilo; //integer variable for distance in kilometers



	// Initialization of speed and time elapsed

	cout << "Enter the speed of travel (in mph): ";

	cin >> rate;

	cout << "Enter the time traveled: ";

	cin >> time;



	dist = CalcDistance(rate, time); // find the distance

	distkilo = CalcKilometers(dist); // convert to kilometers



	// Print values of 4 variables

	cout << "The values found were:nn";

	cout << "Distance (in miles) = " << dist << "n";

	cout << "         (in kilometers) = " << CalcKilometers(dist) << "n";

	cout << "nSpeed = " << rate;

	cout << "nTime = " << time;

	cout << "n";

   

	cin.get(); cin.get();

	return 0;



}



/*

function to calculate distance

pre: rate and time are integers

post: distance is returned

*/



int CalcDistance(int rate, int time)

{



	return (rate * time);



}



/*

function to convert miles to kilometers

pre: dist is an integer

post: distance in kilometers is returned

*/



int CalcKilometers(int dist)

{



	return (dist * 1609 / 1000);



}

</pre>

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

don't know if it will help your error problem or not, but i don't think .net likes iostream.h 'coz it's an old library. i think you just get a warning though, but nonetheless, try using:

#include

using namespace std;

Link to comment
Share on other sites

  • 0

figured out why that one *didn't* have a problem. in my attempts to find out what was the matter i just called CalcKilometers() directly.

line 27

"CalcKilometers(dist)" should be "distkilo" (no quotes)

getting the error w/ iostream.h and iostream

no build errors, just runtime. 'distkilo' is being used without being defined. am i doing something wrong or what? thx for the help

Link to comment
Share on other sites

  • 0

update: in my ongoing attempts to get this thing to work, i opened up the .cpp file in notepad. there were lots of squares (special characters) for breaks - i removed all those and reopened in .net. started debugger and it works fine. is this strange? maybe its just me. if you can make sense of it let me know cause its very irritating when you're trying to get something to work for class and it should but it doesn't. thx

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.