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.
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.
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.
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.
// Kailin Stevers
// February 12, 2017
// Program 4
// Geometries
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
usingnamespace 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();
}