Я новичок в C ++, в настоящее время учусь в колледже на курсах CS, и я застрял, потому что мой код не читает значения из моего входного файла "OH-in.dat". Более того, я не совсем знаю, что делать, когда дело доходит до значения часового значения (поскольку мне требуется цикл do while, я думаю, что мне просто нужно сделать значение sentinel первым, которое я получу, и просто остановить цикл. с тех пор.)
Вот код, который у меня есть:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
// Declare the variables to be used in the program.
ifstream infile;
string schoolname, location;
int tuition, enrollnum, i;
double average;
//Obtain the values for the variables from the file given.
infile.open("OH-in.dat"); //Accomplish task 1.
{
i = 0;
cout << "Schools in Cincinnati with tuition below $20,000." << endl;
cout << "-----------------------------------------------------" << endl;
do
{
// Read the values in the data file.
infile >> schoolname;
infile >> location;
infile >> enrollnum;
infile >> tuition;
// Separate the values that contain Cincinnati and if it's tuition is over 20000 dollars.
if (schoolname == "Cincinnati" && tuition < 20000)
{
cout << schoolname << " $" << tuition << endl;
i++;
}
// Display the number of schools that fit the criteria.
cout << "Number of schools: " << i << endl;
}
//While the 1st value read in is not ***, the loop will continue.
while (schoolname != "***");
//Close the file.
infile.close();
}
system("pause");
return 0;
}
Вот первые четыре значения, которые я буду вводить.
AntiochCollege
YellowSprings
330
27800
...
Наконец-то до этого дойдет.
'* * *'
Эти данные были получены с сайта petersons.com
10 и 11 октября. Некоторое название и местонахождение
данные были изменены.
Итак, две основные проблемы, с которыми я столкнулся в этом коде, заключались в том, чтобы просто заставить С ++ читать значения и, во-вторых, заставить код не повторяться бесконечно.