Using a Do While Loop to pull Data from a file and then run the data through a switch.

Guys I'm so confused on this I'm trying to read the following data from a .txt file.
(file data)
This file contains the hights, radius, and the calculation that will be preformed.
1.0 1.0 C
1.0 1.0 V
1.0 1.0 S
1.0 1.0 D
1010.0 250.0 C
12.3 3.25 V
123.0 43.0 S
12.3 3.25 K
1.12 2.13 C
999
(end of file data)

I want the data to be read in by using a do while loop and in the loop I want three switch statements with the options 'C' 'V' and 'D'. These letter correspond with the type of calculation I want to do with the data. Here is what I currently have.

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
63

#include <iostream>
#include <string>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;


int main()
{
	cout << " Kaitlin Stevers " << endl;
	cout << " February 12, 2017 " << endl;
	cout << " Program 4 " << endl;
	cout << " Geometries " << endl << endl;
	cout << " Height \tRadius \t\tCalculation Type \t Calculated Value " << endl;
	cout << "---------------------------------------------------------------------------------- "<< endl;
	


	float h, r;
	char caltype;
	char filename[200] = "Program4Data.txt";
	string fileinfo;
	cout << setprecision(2) << fixed;
	do
	{
		ifstream infile;
		infile.open(filename);
		getline(infile, fileinfo);
		cout << fileinfo << endl;
		infile >> h >> r >> caltype;
		switch(caltype)
		{
			case 'C' :
			case 'c' : 
				caltype = 'C';
				float answerc;
				answerc = 0; //circumfrence formula here//
				cout << h << "\t\t" << r << "\t\t" << "Circumference" << "\t" << answerc << endl;
				break;
			case 'V' :
			case 'v' :
				caltype = 'V';
				float answerv;
				answerv = 0; //volume formula here//
				cout << h << "\t\t" << r << "\t\t" << "Volume" << "\t\t" << answerv << endl;
				break;
			case 'S' :
			case 's' :
				caltype = 'S';
				float answersa;
				answersa = 0; //surface area formula here//
				cout << h << "\t\t" << r << "\t\t" << "Surface Area" << "\t" << answersa << endl;
				break;
			default:
				cout << " The calculation can not be completed because the data is invalid for 'calculation type' in the file. " << endl;
				break;
		} 
	}
		while( h != 999);
}


The compiles and builds. When I run it I get the default statement over and over. What have I done wrong? Also, where should I put [code] return 0; [\code]? Lastly the 999 at the end of the data is supposed to force the loop to stop because the height equals 999 at that point.
Last edited on
I've changed the code a little. Now it says error: switch quantity not an integer.

I know it isn't... It is a string letter intentionally.... Does it HAVE to be a char? Is that right?

Here is my code now..

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
#include <iostream>
#include <string>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;


int main()
{
	cout << " Kaitlin Stevers " << endl;
	cout << " February 12, 2017 " << endl;
	cout << " Program 4 " << endl;
	cout << " Geometries " << endl << endl;
	cout << " Height \tRadius \t\tCalculation Type \t Calculated Value " << endl;
	cout << "---------------------------------------------------------------------------------- "<< endl;
	


	float h, r;
	string type;
	char filename[200] = "Program4Data.txt";
	string fileinfo;
	cout << setprecision(2) << fixed;
	do
	{
		ifstream infile;
		infile.open(filename);
		getline(infile, fileinfo);
		cout << fileinfo << endl;
		infile >> h >> r >> type;
		switch(type)
		{
			case 'C' :
			case 'c' : 
				type = 'C';
				float answerc;
				answerc = 0; //circumfrence formula here//
				cout << h << "\t\t" << r << "\t\t" << "Circumference" << "\t" << answerc << endl;
				break;
			case 'V' :
			case 'v' :
				type = 'V';
				float answerv;
				answerv = 0; //volume formula here//
				cout << h << "\t\t" << r << "\t\t" << "Volume" << "\t\t" << answerv << endl;
				break;
			case 'S' :
			case 's' :
				type = 'S';
				float answersa;
				answersa = 0; //surface area formula here//
				cout << h << "\t\t" << r << "\t\t" << "Surface Area" << "\t" << answersa << endl;
				break;
		} 
	}
		while( h != 999);
}
@katiestevers

Try changing line 22 from a string, to a char. Also, you could remove lines 37, 44 and 51, since type already equals those values.

EDIT:
I moved lines 29 and 30 outside the for loop, and placed a infile.close(); after the while command.
Also placed the switch statement in an if statement, so that things get printed ONLY if h != 999.
I see you also don't have a case check for 'D' and 'K', yet.

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
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;


int main()
{
	cout << " Kaitlin Stevers " << endl;
	cout << " February 12, 2017 " << endl;
	cout << " Program 4 " << endl;
	cout << " Geometries " << endl << endl;
	cout << " Height \tRadius \t\tCalculation Type \t\t Calculated Value " << endl;
	cout << "---------------------------------------------------------------------------------- "<< endl;



	float h, r;
	char type;
	char filename[200] = "Program4Data.txt";
	string fileinfo;
	ifstream infile;
	infile.open(filename);
	cout << setprecision(2) << fixed;
	do
	{

		//getline(infile, fileinfo); // Not needed
		//cout << fileinfo << endl; // Not needed. Prints line BUT nothing done with it
		infile >> h >> r >> type;
		if(h !=999)
		{
			switch(type)
			{
			case 'C' :
			case 'c' : 
				float answerc;
				answerc = 0; //circumfrence formula here//
				cout << h << "\t\t" << r << "\t\t" << "Circumference" << "\t\t" << answerc << endl;
				break;
			case 'V' :
			case 'v' :
				float answerv;
				answerv = 0; //volume formula here//
				cout << h << "\t\t" << r << "\t\t" << "Volume" << "\t\t\t" << answerv << endl;
				break;
			case 'S' :
			case 's' :
				float answersa;
				answersa = 0; //surface area formula here//
				cout << h << "\t\t" << r << "\t\t" << "Surface Area" << "\t\t" << answersa << endl;
				break;
			} 
		}
	}while( h != 999);
	infile.close();
}
Last edited on
Thanks. I am going to make a default statement for those. Those are invalid types.


When I run my code I get an output of blank lines that never end. I dont understand. :(
Last edited on
@katiestevers

Paste your new code here, and I'll look it over.
I used the exact code you posted. I've been busy so I haven't looked to see what is going wrong. When I run it I get the first few lines and then it prints blank lines.
Your program looks relatively simple, but multiple issues.

First thing :
1
2
3
getline(infile, fileinfo);
cout << fileinfo << endl;
infile >> h >> r >> caltype;


What is the purpose of getline?? Since you already read h and r and caltype what is the need to read a whole line for nothing?

while( h != 999);

The condition may not be met and your program loops forever.
> I want the data to be read in by using a do while loop and in the loop I want three switch statements

Consider using a canonical while loop instead of a do-while loop; it is simpler.

1
2
3
4
5
6
7
8
9
10
11
12
std::ifstream infile(filename) ;

double height ;
double radius ;
char calc_type ;

// ideally, something like std::abs( height - 999.0 ) < 1.e-6 instead of height != 999
while( infile >> height && height != 999 && infile >> radius >> calc_type )
{
   // do what ever with height, radius and calc_type
   // eg. switch( calc_type ) etc. 
}
This worked perfectly! Thanks guys!
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
63
64
65
66
// Kailin Stevers
// February 12, 2017
// Program 4
// Geometries

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;


int main()
{
	cout << " Kaitlin Stevers " << endl;
	cout << " February 12, 2017 " << endl;
	cout << " Program 4 " << endl;
	cout << " Geometries " << endl << endl;
	cout << " Height \tRadius \t\tCalculation Type \t\t Calculated Value " << endl;
	cout << "---------------------------------------------------------------------------------- "<< endl;

	float h, r;
	char type;
	float pi;
	pi = 3.14159265359;
	char filename[200] = "Program4KaitlinSteversEGR126.txt";
	string fileinfo;
	ifstream infile;
	infile.open(filename); //Opens the file
	infile >> fileinfo; //Reads the first line.
	getline(infile, fileinfo); //Gets the line to display next.
	cout << fileinfo << endl; //Displays the line read in. 
	cout << setprecision(2) << fixed; //Sets ALL numbers to only two decimal places.
	do //Tells the program to keep reading in data till it hits the 999.
	{
		infile >> h >> r >> type; //This reads in the height, radius, and calculation type.
		if(h !=999)
		{
			switch(type) //This switches based on the type of calculation listed. 
			{
			case 'C' : //This case is chosen when the 'type' is C. 
			case 'c' : 
				float answerc;
				answerc = ( 2 * r) * pi; //This is the equation to calculate Circumfrence. 
				cout << h << "\t\t" << r << "\t\t" << "Circumference" << "\t\t" << answerc << endl;
				break;
			case 'V' : //This case is chose when the 'type' is V.
			case 'v' : 
				float answerv;
				answerv = pi * ( r * r ) * h; //This is the equation to find the Volume. 
				cout << h << "\t\t" << r << "\t\t" << "Volume" << "\t\t\t" << answerv << endl;
				break;
			case 'S' : //This case is chosen when the 'type' is S.
			case 's' :
				float answersa;
				answersa = (2 * pi * r * h) + (2 * pi * (r * r)); //This is the equation to find the surface area.
				cout << h << "\t\t" << r << "\t\t" << "Surface Area" << "\t\t" << answersa << endl;
				break;
			default:
				cout << h << "\t\t" << r << "\t\t" << "Invalid Character." << "\t" << "N/A" << endl; //The program defaults to this when the 'type' does not match C, V, or S.
			} 
		}
	}while( h != 999); // This tells the program to keep going till it reads in the height of 999 that is listed at the end. 
	infile.close();
}
Topic archived. No new replies allowed.