Unexpected Infinite Loop

Hey guys, I'm just trying to write a basic program for my first week in a uni course. It's a continuation course of learning about OOP but whereas last semester we were mainly just learning Java and some OOP ideas such as encapsulation, etc. The course this semester covers a little bit about C++ but is mostly about data structures.

The program compiles and run fine in the cygwin terminal but the problem I am having is that when the program enters the for loop, it enters an infinite loop displaying "Celsius: 10 Fahrenheit: 42" but I can't see why.

Sorry if the answer is blindingly obvious but any help is greatly appreciated, cheers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*
 * Cale Banfield c3132281 06/08/2014
 *
 * This program takes a lower limit and an upper limit temperature in celsius and then converts them to fahrenheit while displaying 
 * temperatures in between determined by a value entered by the user.
 *
 */


#include <iostream>
using namespace std;

int main(){
	
	double lowerLimit, upperLimit, 	/* Values input by the user for the lower and upper limit temperatures */ 	       	
	       celsius, fahrenheit;	/* Stores the value of both temperatures Celsius and Fahrenheit */
	int step;			/* This is the value in which the temperature will be incremented by when it is displayed */

	cout << "Enter the lower limit of temperature: ";
	cin >> lowerLimit;

	cout << "Enter the upper limit of temperature: ";
	cin >> upperLimit;

	cout << "Enter the step that the temperature will be incremented by: ";
	cin >> step;

	for (celsius = lowerLimit; celsius < upperLimit; celsius + step) { 	/* This loop starts from the lower limit and prints the celsius temperature and then    */
										/* the fahrenheit temperature, incremented by the step until it reaches the upper limit */
		
		fahrenheit = (9 / 5) * celsius + 32;
		
		cout << "Celsius: " << celsius << "   " << "Fahrenheit: " << fahrenheit << "\n";

	}
	
	return 0;
}
Last edited on
Morning,
This bit of your for loop:
celsius + step

Isn't doing anything. You need to assign this new value back to 'celcius'. Did you mean:

celsius += step
?
Last edited on
Ah, you are absolutely correct.

I'm still getting used to C++'s anything goes environment whereas Java would've flagged the problem before I was able to compile haha.

Thanks for that :)
my compiler did flag the problem :)

source.cpp(19): warning C4552: '+' : operator has no effect; expected operator with side-effect
Default settings?

Aceix.
Sorry, I've been really busy with work and uni to check back on here.

@mutexe ah, what compiler are you using? We have to use cygwin for this uni course.

@Aceix I'm sorry, what did you mean by default settings?
Last edited on
@mutexe ah, what compiler are you using? We have to use cygwin for this uni course.
It is probably a compiler flag set. I'm not sure exactly what one but I'd assume -Wextra or -Wall.
http://coliru.stacked-crooked.com/a/1bea333deb540623

main.cpp:28:59: warning: expression result unused [-Wunused-value]
        for (celsius = lowerLimit; celsius < upperLimit; celsius + step) {      /* This loop starts from the lower limit and prints the celsius temperature and then    */
                                                         ~~~~~~~ ^ ~~~~
Last edited on
The compiler that comes with VS 2013 and the default settings.
Topic archived. No new replies allowed.