Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
i need help creating 7 functions to break this code into. By using pass by reference. i can't figure out how to pass the multidimensional arrays between functions.
#include <fstream>
#include <string>
#include <iomanip>
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
const int courseSize = 3;
const int examSize = 6;
int n=0;
int strLength =0;
double scoreTotal =0;
char lettergrade;
string header, name, address, phone, social, course, studentID;
const int letterGrade_A = 90;
const int letterGrade_B = 80;
const int letterGrade_C = 70;
const int letterGrade_D = 60;
int age, yearsAtTXST, numstudents;
double testscore;
double numGrade = 0;
const int minStrLength = 1;
const int maxStrLength = 100;
const int minint = 1;
const int maxint = 100;
const int minCourse = 1;
const int maxCourse = 3;
const int minTest = 1;
const int maxTest = 5;
const double test1 = 0.10;
const double test2 = 0.15;
const double test3 =0.15;
const double test4 = 0.20;
const double final_test = 0.40;
const double maxGrade = 100.0, minGrade = 1.0;
//initialize arrays
string nonNumerical[studsize][strSize];
int numeric1[studsize][numSize];
double numeric2[studsize][courseSize][examSize];
char lettergrades[studsize][courseSize];
cout << fixed << showpoint << setprecision(2);
//opening files
ifstream inputFile;
inputFile.open("Project4_A04314548_Input.txt");
ofstream fout;
fout.open ("Project4_A04314548_Output.txt");
if( !( inputFile && fout))
{
cout << "Error opening file.\n";
}
cout << " Enter a number of students: ";
cin >> numstudents;
while( !(numstudents ==3))
{
cout << " error number of students must be 3. Enter again: ";
cin >> numstudents;
}
//take in string data from input file
for(int x =0; x< studsize; x++)
{
for(int y=0; y < strSize; y++)
{
if(y >= strSize)
break;
getline(inputFile, nonNumerical[x][y]);
strLength = nonNumerical[x][y].length();
if(strLength < minStrLength || strLength > maxStrLength)
{
fout << " The given string for nonNumerical [" << x << "][" << y <<
"] is not within the proper range of 1-100 characters." << endl;
fout << " Please fix the issue and rerun the program for a correct
output." << endl << endl;
continue;
}
}
}
//above put into input data function
//take in data from input file
for(int x=0; x<studsize;x++)
{
for(int y=0; y < numSize; y++)
{
if(y>=numSize)
break;
inputFile >> numeric1[x][y];
if( numeric1[x][y] < minint || numeric1[x][y] > maxint)
{
fout << "The given integer for numerical1[" << x << "][" << y <<
"]is not within the proper range of 1-100." << endl;
fout << " please fix the problem and rerun the program for a correct
output." << endl << endl;
break;
}
}
}
//above put into data function
// take in double data from input file
for( int x1=0; x1<studsize; x1++)
{
for(int x2=0; x2 < courseSize; x2++)
{
for(int x3=0; x3<(examSize-1);x3++)
{
if(x3 >=(examSize-1))
break;
inputFile >> numeric2[x1][x2][x3];
if( numeric2[x1][x2][x3] < minGrade || numeric2[x1][x2][x3] >
maxGrade)
{
cout << "The given value fro numeric2[" << x1 << "][" << x2
<< "][" << x3 << "] is not within the proper range of 1-100" << endl
<< "Please fix the issue and rerun the program for a correct
output." << endl;
break;
}
else if( x3 == 0)
scoreTotal += (numeric2[x1][x2][x3]* test1);
else if( x3== 1)
scoreTotal += (numeric2[x1][x2][x3]* test2);
else if( x3 == 2)
scoreTotal += (numeric2[x1][x2][x3]* test3);
else if( x3 == 3)
scoreTotal += (numeric2[x1][x2][x3]* test4);
else
scoreTotal += (numeric2[x1][x2][x3]* final_test);
}
numeric2[x1][x2][5] = scoreTotal; // final numeric grade
scoreTotal = 0;
if(x2 >=courseSize)
break;
if((numeric2[x1][x2][5] > maxGrade) || (numeric2[x1][x2][5] <
minGrade) )
{
cout << "Error in calculating grade. Please fix the issue
then rerun the program. ignore broken program!";
cout << endl << endl;
continue;
}
else if(numeric2[x1][x2][5] >= letterGrade_A)
lettergrades[x1][x2] = 'A';
else if(numeric2[x1][x2][5] >= letterGrade_B)
lettergrades[x1][x2] = 'B';
else if(numeric2[x1][x2][5] >= letterGrade_C)
lettergrades[x1][x2] = 'C';
else if(numeric2[x1][x2][5] >= letterGrade_D)
lettergrades[x1][x2] = 'D';
else
lettergrades[x1][x2] = 'F';
}
}
for(int a1=0; a1< studsize; a1++)
{ fout << nonNumerical[a1][0]<< endl;
fout << right << setw(35) << "Name of Student:\t";
fout << nonNumerical[a1][1] << endl;
fout << right << setw(35) << "Student ID:\t";
fout << nonNumerical[a1][2]<< endl;
fout << right << setw(35) << "Address:\t" ;
fout << nonNumerical[a1][3] << endl;
fout << right << setw(35) << "Telephone Number:\t";
fout << nonNumerical[a1][4] << endl;
fout << right << setw(35) << "Student Soc. Security:\t";
fout << nonNumerical[a1][5] << endl;
fout << right << setw(35) << "Age:\t";
fout << numeric1[a1][0] << endl;
fout << right << setw(35) << "Number of years at Texas State:\t";
fout << numeric1 [a1][1] << endl << endl;
for(int b1=0; b1 <courseSize; b1++)
{
fout << right << setw(35) << "Course number:\t";
fout << nonNumerical[a1][(b1+6)] << endl;
for(int c1= 0; c1 <(examSize-1); c1++)
{
fout << right << setw(32) << "Exam #" << (a1 +1) << ":\t";
fout << numeric2[a1][b1][c1] << endl;
}
fout << right << setw(35) << "Numerical grade:\t";
fout << numeric2[a1][b1][5] << endl;
fout << right << setw(35) << "Letter grade:\t";
fout << lettergrades[a1][b1] << endl;
if( numeric2[a1][b1][5] < 70)
{
fout << right << setw(14) << " Warning Note: Your grade is too low
and needs improvements!" << endl << endl;
}
else if ( numeric2[a1][b1][5] >= 95)
{
fout << right << setw(14) << " Appreciation Note: Congratulations,
Your performance is Excellent!" << endl << endl;
}
else
fout << endl;
}
fout << endl;
}
inputFile.close();
fout.close();
return 0;
}
Instead of using array
std::string nonNumerical[studsize][strSize];
use std::vector.
std::vector<std::vector<std::string>> nonNumerical(studsize, std::vector<std::string(strSize));
Then, you can divide your code into as many functions as you need and pass the vectors to the functions.
void function1(std::vector<std::vector<std::string>>& nonNumerical)
{
...
}
and use it from main as:
int main()
{
...
std::vector<std::vector<std::string>> nonNumerical(studsize, std::vector<std::string(strSize));
...
function1(nonNumerical);
...
}
first of all c++ send arrays by reference .
your question is not clear ... but if I have get it right, you should send two dimensional arrays like this :
void f1(int a[][20]) ;
just the point is that , you must specify second dimension .
You can pass any size and dimension of array with pointers like this:
myfunction1(string *nonNumerical)
{
string something=nonNumerical[0][0];
}
But don't forget: You can't ask the array's size directly. You must pass the size with the array somehow.
Related
This question already has answers here:
What is array to pointer decay?
(11 answers)
Array Size Member Function Compile Error
(3 answers)
Closed 15 days ago.
I get an error saying "'find' in ‘sentence’, which is of non-class type ‘char*’".
So the instruction was to enter a string and identify which in the string is a noun, pronoun, adjectives and a linking verb. Everything seems to work except the ".find" part. I really need help.
(btw, nvm the other unecessary things. I made the code shorter to put focus more on the problem)
#include <iostream>
#include <cstring>
#include <cctype>
#include <string>
using namespace std;
string student_id, first_name, last_name, middle_name, suffix;
char sentence[100][100];
char history1[100][100];
int h = 0, s= 0;
char noun[20][20] = {"Ervin", "Rafi", "Peco", "luis", "Edgar", "Benj", "Rias", "Aki", "Naruto", "Jhay", "Josh", "Jopay", "Sasuke", "Joshua", "Trump", "Benjo", "Alice", "Janelle", "Samantha", "Jairah"};
char pronoun[20][20] = {"he", "she", "i", "it", "you"};
char verb[20][20] = {"is", "was", "are", "as", "am"};
char adj[20][20] = {"big", "small", "racist", "fat", "Funny", "gay", "black", "white", "rainbow", "tiny", "smart", "bi", "pan", "stupid", "idiot", "buang", "retard", "gaymer", "special", "talented"};
void display_dictionary();
void open_checker();
bool hasElement(char sentence[], char elements[], int size);
int main()
{
int option;
cout << endl << "Account Menu" <<endl;
cout << "[1] Open checker" << endl;
cout << "[2] Display Dictionary" << endl;
cout << "Option: ";
cin >> option;
switch (option){
case 1: open_checker(); break;
case 2: display_dictionary(); break;
}
return 0;
}
void display_dictionary(){
cout << "List of Nouns: ( ";
for (int n = 0; n < 20; n++){
cout << noun[n] << " " ;
}
cout << ")" << endl << endl;
cout << "List of Pronouns: ( ";
for (int n = 0; n < 5; n++){
cout << pronoun[n] << " " ;
}
cout << ")" << endl << endl;
cout << "List of Linking Verbs: ( ";
for (int n = 0; n < 5; n++){
cout << verb[n] << " " ;
}
cout << ")" << endl << endl;
cout << "List of Adjectives: ( ";
for (int n = 0; n < 20; n++){
cout << adj[n] << " " ;
}
cout << ")" << endl << endl;
}
void open_checker(){
int option;
cout << "Enter Sentence: ";
cin.ignore();
cin.get(sentence[s], 100);
if (hasElement(sentence[s], noun[s], 20)){
cout << " Is a noun." << endl;
}
if (hasElement(sentence[s], adj[s], 20)){
cout << " Is an Adjectives." << endl;
}
if (hasElement(sentence[s], verb[s], 5)){
cout << " Is a Linking Verb." << endl;
}
if (hasElement(sentence[s], pronoun[s], 5)) {
cout << " Is a Linking Verb." << endl;
}
else {
cout << "Sentence does not have all required elements." << endl;
}
strcpy(history1[h], sentence[s]);
history1[h][h] = sentence[s][s];
h++;
s++;
cout << "Type 1 to go back: ";
cin >> option;
if (option == 1){
main();
}
}
bool hasElement(char sentence[], char elements[], int size){
for (int i = 0; i < size; i++) {
if (sentence.find(elements[i]) != string::npos) {
cout << elements[i];
return true;
}
}
return false;
}```
This question already has answers here:
Getting Input from a Function - c++ [duplicate]
(2 answers)
When I change a parameter inside a function, does it change for the caller, too?
(4 answers)
Closed 2 years ago.
I attempting to make a program that allows the user to customize the U.S flag, though I've hit a snag. I want to return the flagHeight, flagWidth, and starArea variables back to the main() function, so they can be used in the output for statement.
My goal is to have two functions; the main() function where that output will be displayed, and a second function that takes the user input.
Any thoughts on how I could accomplish this?
#include <iostream>
using namespace std;
double input_Flag();
double input_Flag()
{
double flagWidth;
double flagHeight;
double starArea;
int restriction_1 = 5;
int restriciton_2 = 70;
do
{
cout << "Enter the flag width you would like (between 5 <--> 70): ";
cin >> flagWidth;
cout << endl;
if (flagWidth >= restriction_1 and flagWidth <= restriciton_2)
{
cout << "Your width is |" << flagWidth << "|";
cout << endl;
continue;
}
} while (flagWidth < restriction_1 or flagWidth > restriciton_2);
do
{
cout << "Enter the flag height (between 5 <--> " << flagWidth << "): ";
cin >> flagHeight;
cout << endl;
if (flagHeight >= restriction_1 and flagHeight <= flagWidth)
{
cout << "Your height is |" << flagHeight << "|";
cout << endl;
continue;
}
} while (restriction_1 > flagHeight or flagWidth < flagHeight);
do
{
cout<<"Enter the star area (area < " << flagHeight << "):";
cin>>starArea;
cout << endl;
if (starArea < flagHeight)
{
cout << "Your star area of the flag is |" << starArea << "|";
cout << endl;
continue;
}
} while (1 > starArea or starArea >= flagHeight);
}
void myPause()
{
cout << "[Press ENTER to see your flag]";
cin.clear();
cin.sync();
cin.get();
}
int main()
{
cout << "Welcome! Customize Your Flag below!" << endl;
cout << endl;
input_Flag();
cout << endl;
myPause();
cout << endl;
for(int x = 1; x <= flagHeight; x++)
{
for(int y = 1; y <= flagWidth; y++)
{
if(x <= starArea and y <= starArea)
cout << "*";
else
cout << "=";
}
cout << endl;
}
cout << "Awesome flag!" << endl;
return 0;
}
I am new to C++ and am trying to build a simple program that with the users input to proceed will generate a random left or right. I had the program working correctly until I added in the array to try and store each item as I have to output them as soon and the user would like to exit the loop. The program seems to compile fine but at run time I receive "Unhandled exception at 0x012B1CA9" Any help would be greatly appreciated.
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int userSelection = 1;
const int MAX = '100';
int randNum(0);
int one (0);
int two (0);
int total(0);
int sel[MAX];
do
{
cout << "Press 1 to pick a side or 0 to quit: ";
cin >> userSelection;
for (int i = 1; i < MAX; i++)
{
srand(time(NULL));
sel[i] = 1 + (rand() % 2);
if (sel[i] == 1)
{
cout << "<<<--- Left" << endl;
one++;
total++;
}
else
{
cout << "Right --->>>" << endl;
two++;
total++;
}
}
} while (userSelection == 1);
cout << "Replaying Selections" << endl;
for (int j = 0; j < MAX; j++)
{
cout << sel[j] << endl;
}
cout << "Printing Statistics" << endl;
double total1 = ((one / total)*100);
double total2 = ((two / total)*100);
cout << "Left: " << one << "-" << "(" << total1 << "%)" << endl;
cout << "Right: " << two << "-" << "(" << total2 << "%)" << endl;
system("pause");
return 0;
};
You have a multi-character constant here... and the behavior doesn't go as expected...
Change this line
const int MAX = '100';
to
const int MAX = 100;
Note the removed single quotes.
And secondly, I will advice you to remove the Seed of the C random generator from the for loop because, you'll likely get the same values from the rand() if you always call it immediately after seeding...
But preferable use the algorithm from C++'s random header
Here is a corrected version of your original code....
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int userSelection = 1;
const int MAX = 100; // <---changed
int randNum(0);
int one (0);
int two (0);
int total(0);
int sel[MAX];
do
{
cout << "Press 1 to pick a side or 0 to quit: ";
cin >> userSelection;
srand(time(NULL)); //< moved to here
for (int i = 0; i < MAX; i++) // <-- modified starting index
{
sel[i] = 1 + (rand() % 2);
if (sel[i] == 1)
{
cout << "<<<--- Left" << endl;
one++;
total++;
}
else
{
cout << "Right --->>>" << endl;
two++;
total++;
}
}
} while (userSelection == 1);
cout << "Replaying Selections" << endl;
for (int j = 0; j < MAX; j++)
{
cout << sel[j] << endl;
}
cout << "Printing Statistics" << endl;
double total1 = ((one / total)*100);
double total2 = ((two / total)*100);
cout << "Left: " << one << "-" << "(" << total1 << "%)" << endl;
cout << "Right: " << two << "-" << "(" << total2 << "%)" << endl;
system("pause");
return 0;
};
I think that it is basically good idea to read more about C data types and declaration. Your error:
const int MAX = '100' should be const int MAX = 100 without any quotes. C++ does implicit conversion from character literals to int.
the 7 functions i need to make
this file is using arrays to retrieve data from an input file and display the data onto an output file. I'm having trouble with the void input data function. The call function inputdata(inputFile, nonNumerical, numeric1); is getting an error saying error: cannot convert 'int ()[2]' to 'double ()[2]' for argument '3' to 'void inputdata(std::ifstream&, std::string()[9], double()[2])'
#include <fstream>
#include <string>
#include <iomanip>
#include <iostream>
#include <cmath>
using namespace std;
const int courseSize = 3;
const int examSize = 6;
const int studsize =3;
const int strSize =9;
const int numSize = 2;
void inputdata(ifstream &, string [][strSize],double [][numSize]);
int main()
{
int n=0, x, y;
int strLength =0;
double scoreTotal =0;
char lettergrade;
string header, name, address, phone, social, course, studentID;
const int letterGrade_A = 90;
const int letterGrade_B = 80;
const int letterGrade_C = 70;
const int letterGrade_D = 60;
int age, yearsAtTXST, numstudents;
double testscore;
double numGrade = 0;
const int minStrLength = 1;
const int maxStrLength = 100;
const int minint = 1;
const int maxint = 100;
const int minCourse = 1;
const int maxCourse = 3;
const int minTest = 1;
const int maxTest = 5;
const double test1 = 0.10;
const double test2 = 0.15;
const double test3 =0.15;
const double test4 = 0.20;
const double final_test = 0.40;
const double maxGrade = 100.0, minGrade = 1.0;
// initialize arrays
string nonNumerical[studsize][strSize];
int numeric1[studsize][numSize];
double numeric2[studsize][courseSize][examSize];
char lettergrades[studsize][courseSize];
cout << fixed << showpoint << setprecision(2);
// opening files
ifstream inputFile;
inputFile.open("Project5_A04314548_Input.txt");
ofstream fout;
fout.open ("Project5_A04314548_Output.txt");
if( !( inputFile && fout))
{
cout << "Error opening file.\n";
}
cout << " Enter a number of students: ";
cin >> numstudents;
while( !(numstudents ==3))
{
cout << " error number of students must be 3. Enter again: ";
cin >> numstudents;
}
inputdata(inputFile, nonNumerical, numeric1);
// take in string data from input file
for(int x =0; x< studsize; x++)
{
for(int y=0; y < strSize; y++)
{
if(y >= strSize)
break;
getline(inputFile, nonNumerical[x][y]);
strLength = nonNumerical[x][y].length();
if(strLength < minStrLength || strLength > maxStrLength)
{
fout << " The given string for nonNumerical [" << x << "][" << y <<
"] is not within the proper range of 1-100 characters." << endl;
fout << " Please fix the issue and rerun the program for a correct
output." << endl << endl;
continue;
}
}
}
//above put into input data function
// take in data from input file
for(int x=0; x<studsize;x++)
{
for(int y=0; y < numSize; y++)
{
if(y>=numSize)
break;
inputFile >> numeric1[x][y];
if( numeric1[x][y] < minint || numeric1[x][y] > maxint)
{
fout << "The given integer for numerical1[" << x << "][" << y <<
"]is not within the proper range of 1-100." << endl;
fout << " please fix the problem and rerun the program for a correct
output." << endl << endl;
break;
}
}
}
// above put into data function
// take in double data from input file
for( int x1=0; x1<studsize; x1++)
{
for(int x2=0; x2 < courseSize; x2++)
{
for(int x3=0; x3<(examSize-1);x3++)
{
if(x3 >=(examSize-1))
break;
inputFile >> numeric2[x1][x2][x3];
if( numeric2[x1][x2][x3] < minGrade || numeric2[x1][x2][x3] >
maxGrade)
{
cout << "The given value fro numeric2[" << x1 << "][" << x2
<< "][" << x3 << "] is not within the proper range of 1-100" << endl
<< "Please fix the issue and rerun the program for a correct
output." << endl;
break;
}
else if( x3 == 0)
scoreTotal += (numeric2[x1][x2][x3]* test1);
else if( x3== 1)
scoreTotal += (numeric2[x1][x2][x3]* test2);
else if( x3 == 2)
scoreTotal += (numeric2[x1][x2][x3]* test3);
else if( x3 == 3)
scoreTotal += (numeric2[x1][x2][x3]* test4);
else
scoreTotal += (numeric2[x1][x2][x3]* final_test);
}
numeric2[x1][x2][5] = scoreTotal; // final numeric grade
scoreTotal = 0;
if(x2 >=courseSize)
break;
if((numeric2[x1][x2][5] > maxGrade) || (numeric2[x1][x2][5] <
minGrade) )
{
cout << "Error in calculating grade. Please fix the issue
then rerun the program. ignore broken program!";
cout << endl << endl;
continue;
}
else if(numeric2[x1][x2][5] >= letterGrade_A)
lettergrades[x1][x2] = 'A';
else if(numeric2[x1][x2][5] >= letterGrade_B)
lettergrades[x1][x2] = 'B';
else if(numeric2[x1][x2][5] >= letterGrade_C)
lettergrades[x1][x2] = 'C';
else if(numeric2[x1][x2][5] >= letterGrade_D)
lettergrades[x1][x2] = 'D';
else
lettergrades[x1][x2] = 'F';
}
}
for(int a1=0; a1< studsize; a1++)
{ fout << nonNumerical[a1][0]<< endl;
fout << right << setw(35) << "Name of Student:\t";
fout << nonNumerical[a1][1] << endl;
fout << right << setw(35) << "Student ID:\t";
fout << nonNumerical[a1][2]<< endl;
fout << right << setw(35) << "Address:\t" ;
fout << nonNumerical[a1][3] << endl;
fout << right << setw(35) << "Telephone Number:\t";
fout << nonNumerical[a1][4] << endl;
fout << right << setw(35) << "Student Soc. Security:\t";
fout << nonNumerical[a1][5] << endl;
fout << right << setw(35) << "Age:\t";
fout << numeric1[a1][0] << endl;
fout << right << setw(35) << "Number of years at Texas State:\t";
fout << numeric1 [a1][1] << endl << endl;
for(int b1=0; b1 <courseSize; b1++)
{
fout << right << setw(35) << "Course number:\t";
fout << nonNumerical[a1][(b1+6)] << endl;
for(int c1= 0; c1 <(examSize-1); c1++)
{
fout << right << setw(32) << "Exam #" << (a1 +1) << ":\t";
fout << numeric2[a1][b1][c1] << endl;
}
fout << right << setw(35) << "Numerical grade:\t";
fout << numeric2[a1][b1][5] << endl;
fout << right << setw(35) << "Letter grade:\t";
fout << lettergrades[a1][b1] << endl;
if( numeric2[a1][b1][5] < 70)
{
fout << right << setw(14) << " Warning Note: Your grade is too low
and needs improvements!" << endl << endl;
}
else if ( numeric2[a1][b1][5] >= 95)
{
fout << right << setw(14) << " Appreciation Note: Congratulations,
Your performance is Excellent!" << endl << endl;
}
else
fout << endl;
}
fout << endl;
}
inputFile.close();
fout.close();
return 0;
}
void inputdata(ifstream & inputFile, string array1[][strSize],double
array2[]
[numSize])
{
for(int x =0; x< studsize; x++)
{
for(int y=0; y < strSize; y++)
{
if(y >= strSize)
break;
getline(inputFile, array1[x][y]);
}
}
for(int a1=0; a1<studsize;a1++)
{
for(int a2=0; a2 < numSize; a2++)
{
if(a2>=numSize)
break;
inputFile >> array2[a1][a2];
}
}
}
Right now Im trying to write a program that can take an input file that has a bunch of integers, and then show the amount of numbers in certain ranges. For example:
If the input file has 20, 30, 40, 50, 60. And the ranges are 1-20, 21-40, 41-60
The output would be
1-20: 1
21-40: 2
41-60: 3
etc.
Im new to programming, so Im just having a little problem with my code, I know im close. Im using arrays to store them, but I wasnt sure how to make an array that adapts to how many integers there are in a file. So this is what I got so far. (Just for this exercise, Im trying to make the max amount the array will store 100 integers).
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
int ranges(int);
int main()
{
ifstream indata;
ofstream outdata;
indata.open("scores1.txt");
outdata.open("scoresoutput.txt");
if(!indata)
{
cout << "Unable to open file" << endl;
return 1;
}
int range1=0;
int range2=0;
int range3=0;
int range4=0;
int range5=0;
int range6=0;
int range7=0;
int range8=0;
const int max=100;
int score[max];
while(indata)
{
for(int i=0; i < max ; i++)
{
indata >> score[i];
indata.ignore(1);
if(0 <= score[i] <= 24)
{
range1++;
}
else if(25 <= score[i] <= 49)
{
range2++;
}
else if(50 <= score[i] <=74)
{
range3++;
}
else if(75<= score[i] <= 99)
{
range4++;
}
else if(100 <= score[i] <= 124)
{
range5++;
}
else if(125 <= score[i] <= 149)
{
range6++;
}
else if(150 <= score[i] <= 174)
{
range7++;
}
else if(175 <= score[i]<= 200)
{
range8++;
}
}
}
outdata << "Range" << setw(10) << "Number of Students" << endl <<
"0-24" << setw(10) << range1 << endl<<
"25-49" << setw(10) << range2 << endl <<
"50-74" << setw(10) << range3 << endl <<
"75-99" << setw(10) << range4 << endl <<
"100-124" << setw(10) << range5 << endl <<
"125-149" << setw(10) << range6 << endl <<
"150-174" << setw(10) << range7 << endl <<
"175-200" << setw(10) << range8 << endl;
return 0;
}
Right now, the output just displays 0-24 100. Any easier way to do this than what I am doing now?
Array is your good friend. Try to use an array to manage the counts:
int range_count[8];
for (int i = 0; i < 8; ++i) {
range_count[i] = 0;
}
and in the for-loop inside your while-loop:
for(int i = 0; i < max ; ++i) {
indata >> score[i];
indata.ignore(1);
if (score[i] >= 0 && score[i] <= 200) {
range_count[score[i] / 25]++; // since your range intervals is a constant 25.
}
}
and finally, in the output:
outdata << "Range" << setw(10) << "Number of Students" << endl;
for (int i = 0; i < 8; ++i) {
i * 25 << "-" << i * 25 + 24 << setw(10) << range_count[i] << endl;
}