Infinite Loop

Why is this infinitely looping?
This is my input.txt verbatim:
sin 45 cos 45 tan 45

Aren't sin, cos, and tan supposed to be stored in the oper string but I have a feeling like they aren't or at least only sine is?

This is basically supposed to be a simple taking of the angle and outputting the answer. The inputs are contained in input.txt and the answer is outputted in an organized output.txt.

This is my header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using std::string;
using std::istream;
using std::ostream;
using std::setprecision;
using std::fixed;
using std::showpoint;
using std::setw;

void calculate( string inputfilename, string outputfilename );

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "calculate.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>
#define _USE_MATH_DEFINES
#include <cmath>
#define PI       3.14159265

using std::string;
using std::cout;
using std::ifstream;
using std::ofstream;
using std::setprecision;
using std::fixed;
using std::showpoint;
using std::setw;

int main()
{
    cout << "Please enter a number of trig commands followed by an angle in degrees.  For example: sin 45 cos 45 tan 45: \n";
    calculate( "input.txt", "output.txt");
    return 0;

}

void calculate( string inputfilename, string outputfilename )
{
    ifstream in(inputfilename);
    ofstream out(outputfilename);
    string oper;
    int degrees;
	double cosine;
	double sine;
	double tangent;
    in >> oper;
	in >> degrees;
		while ( !in.fail() )
    {
        if ( oper == "sin" )
        { 
        sine = sin ( degrees * PI / 180.0 ); 
        }
        else if ( oper == "cos" )
        {
        cosine = cos ( degrees * PI / 180.0 );
        }
        else if ( oper == "tan" )
        {
        tangent = tan ( degrees * PI / 180.0 );
		}
		
		out << "Operation \t" << "Angle \t\t" << "Answer \n";
		out << oper << "\t\t" << degrees << "\t" << showpoint << setprecision(10) << sine << '\n';
		out << oper << "\t\t" << degrees << "\t" << showpoint << setprecision(10) << cosine << '\n';
		out << oper << "\t\t" << degrees << "\t" << showpoint << setprecision(10) << tangent << '\n';
		
		}   
	
    in.close();
    out.close();
}
Last edited on
Why is this infinitely looping?


When does the condition of your loop ever become false?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
while ( !in.fail() )
{
        if ( oper == "sin" )
        { 
        sine = sin ( degrees * PI / 180.0 ); 
        }
        else if ( oper == "cos" )
        {
        cosine = cos ( degrees * PI / 180.0 );
        }
        else if ( oper == "tan" )
        {
        tangent = tan ( degrees * PI / 180.0 );
	}
		
	out << "Operation \t" << "Angle \t\t" << "Answer \n";
	out << oper << "\t\t" << degrees << "\t" << showpoint << setprecision(10) << sine << '\n';
	out << oper << "\t\t" << degrees << "\t" << showpoint << setprecision(10) << cosine << '\n';
	out << oper << "\t\t" << degrees << "\t" << showpoint << setprecision(10) << tangent << '\n';	
} 
Line 38: If the last in >> operation was successful, you're going to loop forever since you do not access cin inside the loop, therefore the status of cin never changes.

If you intended the program to loop, move the while statement to after line 35.
If you did not intend the program to loop, change the while to if.
Topic archived. No new replies allowed.