I am a beginner in c coding and i have an assignment for my class. I am trying to read the first line of integers separated by spaces 2 spaces in the file (cannot use arrays or vectors). I have seen many tutorials telling me to use getline () and simply reading each and every integer and storing it into its own variable, but neither method has been working for me. Does anybody have a way to read in the first line with a while loop and have it so that I can then find the maximum and minimum values from the line, as well as calculate the average EXCLUDING the maximum and minimum values?
sample input i was instructed to analyze is as follows
5 7 9 8 7
30032
51111
52000
42000
9 8 6 3 7
70000
23765
24000
41004
Here is what I have so far.
{
void PrintIntro (); {
cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-" <<endl;
cout << "Welcome to Tallahassee Idol! Where Stars are Born!!!" <<endl;
cout << "*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-" <<endl<<endl;
}
/*******************************************************************************/
void OpenFile (); {
ifstream myFile; // filestream for input file
string fileName; // string for user to input filename
cout <<"Enter a valid filename (no blanks please): ";
cin >> fileName;
myFile.open(fileName);
while (!myFile) {
cout << "Please re-enter a valid filename: "<<endl<<endl;
cin >> fileName;
myFile.open(fileName);
}
}
/*******************************************************************************/
void GetJudgeScore1 (); {
ifstream myFile; // filestream for input file
string fileName; // string for user to input filename
int player1Total = 0; // total score for player 1
int player1Average = 0; // average for player 1
int highScore1 = 0; // highest score to be excluded
int lowScore1 = 100; // lowest score to be excluded
const int judgeAmt = 5;
cout << "Processing Judge Data for Contestant 1" <<endl;
cout << "=====================================" <<endl;
cout << "Judge scores are: ";
if (myFile.is_open()){ // if the file is open, read data in
// here is where i ran into problems
}
}
return 0;
}
CodePudding user response:
You can use a combination of std::string
and std::istringstream
and std::getline
to iterate through the different integers in the line as shown below. The explanation is given in the comments.
#include <iostream>
#include<string>
#include <sstream>
#include <fstream>
#include <climits>
int main()
{
std::string line;//for storing a single line
int max = INT_MIN, min = INT_MAX, num = 0, count =0;
std::ifstream inFile("input.txt");
if(inFile)
{
while(std::getline(inFile, line))//go line by line
{
std::istringstream ss(line);
while(ss >> num)//go number by number
{
if(num > max)
{
max = num;//update the maximum
}
if(num < min)
{
min = num; //update the minimum
}
count;
}
//you can add an if statement here to print the result only if count > 1
std::cout<<"max is: "<<max<<std::endl;
std::cout<<"min is: "<<min<<std::endl;
count = 0; //make the count 0 for next iteration
max = INT_MIN; //reset max
min = INT_MAX; //reset min
std::cout<<"----------------------"<<std::endl;
}
}
else
{
std::cout<<"input file cannot be opened"<<std::endl;
}
return 0;
}
The output of the above program can be seen here:
max is: 9
min is: 5
----------------------
max is: 30032
min is: 30032
----------------------
max is: 51111
min is: 51111
----------------------
max is: 52000
min is: 52000
----------------------
max is: 42000
min is: 42000
----------------------
max is: 9
min is: 3
----------------------
max is: 70000
min is: 70000
----------------------
max is: 23765
min is: 23765
----------------------
max is: 24000
min is: 24000
----------------------
max is: 41004
min is: 41004
----------------------
Since this is a homework problem, i am skipping finding out the average of the numbers so that you can modify the above program accordingly and so that i don't give the whole solution myself. Note that since your requirement is that not to use std::vector
or arrays, i haven't used them in my program.
Hint:(for finding average) Add a variable called sum
or average
and add value of variable num
to it inside the while
loop.
CodePudding user response:
I am unsure as to what you mean by "cannot use arrays or vectors." The following code will read each line and save it to a string, convert it, and add it to an integer array, for easier storage. You could use multiple variables instead.
#include <fstream>
#include <string>
void getData() {
std::string output;
int i = 0, data[10]; // Pre-defined array size, looking at your data set.
std::ifstream myFile;
myFile.open("Path\\To\\Your\\File.txt", ios::in);
while(getline(myFile, output))
{
data[i] = std::stoi(output);
i ;
std::cout << data[i] << "\n";
}
myFile.close();
}
Output:
57987
30032
51111
52000
42000
98637
70000
23765
24000
41004