Assignment problem

Hello everyone. I am a new student in C++ and I am having trouble with an assignment. I need to enter a starting temperature, an ending temperature and an increment value that I want the conversion to show. The problem that I am having is that I cannot get the program to display the results for Fahrenheit to Celsius in the proper increment. It works fine for the Celsius to Fahrenheit however.

void FtoC(int start,int end,int increment,int &count)
{
double y;
int i;
for(i=start;i<=end;i=i++)
{
count=count+1;
}
temp=new double[count];
int x=0;
for(i=start;i<=end;i=i++) [u]// I have tried both i+increment and what you see here. When I use: for(i=start;i<=end;i=i+increment) I get nothing but garbage and not the proper calculations. As you can see from my code I used this for Celsius to Fahrenheit with no problems at all.
{
y= (5.0 *(i - 32.0)) / 9.0;
temp[x]=y;
x=x+1;
}
}

void CtoF(int start,int end,int increment,int &count)
{
double y;
int i; // start temp
for(i=start;i<=end;i=i++)//increment
{
count=count+1;
}
temp=new double[count];
int x=0;
for(i=start;i<=end;i=i+increment)
{
y =((9.0 * i)/5.0) + 32.0;

temp[x]=y;
x=x+1;
}
}


Any help or suggestions would be greatly appreciated!!
1)

i++ increments i. You don't need to assign it to i after that... the ++ operator changes i on its own.

i = i++ is undefined behavior. it assignes i to itself, then increments it. Or it increments it, then assigns it to the original value (leaving i unchanged).

Any of the following are fine:
1
2
3
4
for(i = start; i <= end; ++i)
for(i = start; i <= end; i++)
for(i = start; i <= end; i = i + 1)
for(i = start; i <= end; i += 1)


But what is increment? Is it always 1?


2)
What is the point of this loop:
1
2
3
4
for(i=start;i<=end;i=i++)
{
  count=count+1;
}


If the goal here is to increment 'count' by the number of items... wouldn't something like count += end-start+1; work? You don't even need a loop for that.


3)
What purpose does temp serve? You don't seem to do anything with it after you fill it. Your functions might as well not do anything. I don't know what you're using for output, but from the code you posted, you're certainly not outputting temp... which is probably why your output doesn't make sense.


4)
You should remember to delete[] whatever you new[]. You're never delete[]ing temp, so it's leaking memory with every call.
Last edited on
Disch,

Thank you very much for the reply!! I just scanned through your reply this morning but I will read it in great detail when I get home from work. The increment is chosen by the program user. They will enter a starting temp, end temp, and the increment that they want the results displayed in. IE start temp of 100, end temp of 500 degrees, and display the conversion increment 50 degrees. So the result that I should see is

100
150
200
250
300
350
400
450
500

and of course these numbers would have a converted temp eiither from C to F, or F to C.
Topic archived. No new replies allowed.