Incorrect user input - c++

Trying to let only numbers be a vaild input for a quadratic equation solver. I used bool and broke my code and no matter the input it just gives me my error message even if it is a vaild input.
After the progarm ask you to confirm the coefficients are correct, even if you put Y you get the " Please input a number. Please try again". how do you fix this?
I added the bool becasue that what my teacher showed me to use for checking inputs, very new to c++ and coding. I just added the whole code, the HW was to update HW 5 to do a few things more. I broke it trying to check inputs, when adding bool.
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
string first, last;
double a, b, c, x1, x2, discriminant, realpart, imaginarypart;
char ch;
cout << "Please enter First and Last name," << endl;
cout << " " << endl;
cout << "First Name= ";
cin >> first;
cout << "and" << endl;
cout << "Last Name=";
cin >> last;
cout << " " << endl;
cout << " Hello " << first << " " << last << " "
<< "welcome." << endl;
cout << " " << endl;
bool isCorrect{ true };
do {
start:
isCorrect = true;
cout << "Enter the coefficients of a: ";
cin >> a;
cout << " " << endl;
cout << "Enter the coefficients of b: ";
cin >> b;
cout << " " << endl;
cout << "Enter the coefficient of c: ";
cin >> c;
cout << " " << endl;
cout << " A = " << a;
cout << " B = " << b;
cout << " C = " << c << endl
<< endl;
cout << " Confirm the coefficients value are correct or not (y/n): " << endl;
cout << " " << endl;
cin >> ch;
if (ch == 'n' || ch == 'N')
goto start;
cout << " " << endl;
discriminant = b * b - 4.0 * a * c;
if (cin.fail())
;
{
isCorrect = false;
cin.clear();
cin.ignore(245, '\n');
cout << " Please input a number. Please try again" << endl;
}
} while (!isCorrect);
bool isExit(false);
if (a == 0) {
cout << " " << endl;
cout << "Error message: a can not = zero (0) " << endl;
}
else if (discriminant > 0) {
x1 = (-b + sqrt(discriminant)) / (2.0 * a);
x2 = (-b - sqrt(discriminant)) / (2.0 * a);
cout << "Roots are real and different." << endl;
cout << " " << endl;
cout << "x1 = " << x1 << endl;
cout << "x2 =" << x2 << endl;
}
else if (discriminant == 0) {
cout << "Roots are real and same." << endl;
cout << " " << endl;
x1 = -b / (2.0 * a);
cout << "x1 = x2 ="
" "
<< x1 << endl;
}
else {
//cout << "Error message: No real solution exist." << endl; // Part 1 error code for no real solutions
realpart = -b / (2.0 * a); //Code for part 2
imaginarypart = sqrt(-discriminant) / (2.0 * a); // Code for part 2
cout << "Roots are complex and different." << endl; // Code for part 2
cout << " " << endl;
cout << "x1 = " << realpart << "+" << imaginarypart << "i" << endl; // Code for part 2
cout << "x2 = " << realpart << "-" << imaginarypart << "i" << endl; // Code for part 2
}
cout << " " << endl;
cout << " Would you like to solve another quadratic equation (y/n): " << endl;
cin >> ch;
if (ch == 'y' || ch == 'Y')
goto start;
else
(ch == 'n' || ch == 'N');
return 0;
}

Related

Are there identifiers for strings or numbers?

I recently developed a calculator, this is the code:
/*
*All 4 operations + percentage finder + Raise to power and more....
* by Ulisse
* ulissebenedennti#outlook.com
* Feel free to take some parts of this code an put them
* in yours, but do not take all the code and change/delete
* the comments to take the credit, trust me, it doesn't
* gives the satisfaction you expect.
*/
#include <iostream> //For cin and cout
#include <iomanip> //For setprecision()
#include <windows.h> //For SetconsoleTitle()
#include <stdlib.h> //For system()
#include <cmath> //For pow()
#include <cctype> //For isdigit()
using namespace std;
int main(){
reset:
system("cls"); //Screen cleaner
system("color 0f");
SetConsoleTitle("Calculator by Ulisse");//Setting window title
char op; //Start of the variables declaration
double a, b, ra;
string p, ms, d, me, e;
p = " + ";
ms = " - ";
d = " : ";
me = " x ";
e = " = "; //End of the variable declaration
cout << "Type now '?' for help page, or another character to continue." << endl;
cin >> op;
if (op == '?'){
help:
system("cls");
cout << "Write the whole operation.\nEXAMPLE: 2 ^ 3 \n OUTPUT: 2 ^ 3 = 8"<< endl;
cout << "(+) Sum between a and b\n(-) Subtraction between a and b" << endl;
cout << "(^) Raise to power\n(%)finds the a% of b\n(x or *)Multiplicate a by b" << endl;
cout << "(: or /) Divide a by b" << endl;
system("pause");
system("cls");
goto start;
}
else{
system("cls");
while(1){
start:
cout << "CALC> ";
cin >> a;
cin >> op;
cin >> b;
//The four operations
if (op == '+'){
cout << "RESULT" << endl;
cout << setprecision(999) << a << p << b << e << a + b << endl;
cout << "________________________________________________________________________________" << endl;
}
if (op == '-'){
cout << "RESULT" << endl;
cout << setprecision(999) << a << ms << b << e << a - b << endl;
cout << "________________________________________________________________________________" << endl;
}
if (op == '*' || op == 'x'){
cout << "RESULT" << endl;
cout << setprecision(999) << a << me << b << e << a * b << endl;
cout << "________________________________________________________________________________" << endl;
}
if (op == '/' || op == ':'){
cout << "RESULT" << endl;
cout << setprecision(999) << a << d << b << e << a / b << endl;
cout << "________________________________________________________________________________" << endl;
}
if (op == '%'){
cout << "RESULT" << endl;
cout << setprecision(999) << "The " << a << "% of " << b << " is " << b / 100 * a << endl;
cout << "________________________________________________________________________________" << endl;
}
if (op == '^'){
cout << "RESULT" << endl;
cout << setprecision(999) << a << " ^ " << b << " = " << pow (a, b) << endl;
cout << "________________________________________________________________________________" << endl;
}
//Some useful functions
if (op == 'c'){
system("cls");
}
if (op == '?'){
system("cls");
goto help;
}
if (op == 'r'){
goto reset;
}
if (op == 'b'){
system("color 0c");
Beep(400,500);
cout << "CLOSING, ARE YOU SURE?(y/n)";
system("color 0c");
cin >> op;
if(op == 'y'){
cout << "Closing..." << endl;
system("cls");
system("color 0f");
system("pause");
break;
}
if(op == 'n'){
goto start;
}
}
if (op == '<'){
if (a < b){
cout << "RESULT" << endl;
cout << setprecision(999) << a << " < " << b << e << " TRUE " << endl;
cout << "________________________________________________________________________________" << endl;
}
else{
cout << "RESULT" << endl;
cout << setprecision(999) << a << " < " << b << e << " FALSE " << endl;
cout << "________________________________________________________________________________" << endl;
}
}
if (op == '>'){
if (a > b){
cout << "RESULT" << endl;
cout << setprecision(999) << a << " > " << b << e << "TRUE" << endl;
cout << "________________________________________________________________________________" << endl;
}
else{
cout << "RESULT" << endl;
cout << setprecision(999) << a << " > " << b << e << "FALSE" << endl;
cout << "________________________________________________________________________________" << endl;
}
}
if (op == '='){
if (a == b){
cout << "RESULT" << endl;
cout << setprecision(999) << a << " = " << b << " is TRUE" << endl;
cout << "________________________________________________________________________________" << endl;
}
else{
cout << "RESULT" << endl;
cout << setprecision(999) << a << " = " << b << " is FALSE" << endl;
cout << "________________________________________________________________________________" << endl;
}
}
}
}
}
This is how it works:
You write a number, then an operator(like +, - plus other functions...) and it makes the operation between the two numbers you typed depending o what is the typed operato, so if you type 4 + 3 it will output 4 + 3 = 7.
Now that you understand how it works, let us go to the qyestion...
Is there an indentifier for a number or a character? When you type a sttring or a character when you cin >> (not a number variable) the application will start printing out characters that you did never inserted:
Input
I think like this(console output) will be printed out(until you dont close the process.
So i would like to prevent the applicatin for failing when you type an invalid input for the variable and making it executes another instruction, here's what i mean:
if(anumbervariable != number || anumbervariable == string){
cout << "invalid input" << endl;
}
This isn't a real working code, it's just a representation of what i mean, or i wouldn't came hre to make you waste you lives :)
Thanks in advance.
You can do something like follows
int getNumber(){
int x;
cin >> x;
while(cin.fail()){
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "invalid input"<<endl;
cin >> x;
}
return x;
}
If you want to do a character by character thing, C++ has an isalpha() function, so you can use !isalpha(). The numeric limits thing is the max buffer that can be taken before a new line. If you print it out, it's just some large number so that it can ignore that amount of input.

While loop and boolean function

Hello guys I'm not an expert on the subject so please excuse my pour skills. I finished my program and it works fine (calculator). The problem is that now I don't know where to locate the while loop in conjunct with the Boolean function to repeat the process once it is done with a task (once the program completes a math operation). Any help, comment or suggestion will be greatly appreciated. Thank you.!!
#include <iostream>
#include <math.h>
#include <cmath>
int main()
{
double a=0.0;
double b=0.0;
double c=0.0;
bool repeat = true;
do {
using namespace std;
int x;
cout << "**********************************" << endl;
cout << "| |" << endl;
cout << "| 0 - Quit |" << endl;
cout << "| 1 - Add |" << endl;
cout << "| 2 - Subtract |" << endl;
cout << "| 3 - Divide |" << endl;
cout << "| 4 - Multiply |" << endl;
cout << "| 5 - Raise X to the power Y |" << endl;
cout << "| 6 - Sine ( x ) |" << endl;
cout << "| 7 - Cosine ( x ) |" << endl;
cout << "| 8 - Tangent ( x ) |" << endl;
cout << "**********************************" << endl;
cout << "Enter a selection, please: " << endl;
cin >> x;
switch (x)
{
{
case 1:
cout << " Enter the first value" <<endl;
cin >> a ;
cout << " Enter second value " << endl;
cin >> b;
c=a+b;
cout << "The addition of " << a << " and "<< b << "is" << c << endl;
break;
bool repeat = true;
}
{
case 2:
cout << " Enter the first value" << endl;
cin >> a ;
cout << " Enter the second value " << endl;
cin >> b;
c=a-b;
cout << "The subtraction of " << a << " and " << b << " is: " << c << endl;
break;
bool repeat = true;
}
{
case 3:
cout << " Enter the first value" <<endl;
cin >> a ;
cout << " Enter the second value " << endl;
cin >> b;
c=a/b;
cout << " The division os " << a << " and " << b << "is" << c << endl;
break;
bool repeat = true;
}
{
case 4:
cout << " Enter the first value" <<endl;
cin >> a ;
cout << " Enter the second value " << endl;
cin >> b;
c=a*b;
cout << " The product of " << a << " times " << b << " is " << c << endl;
break;
bool repeat = true;
}
{
case 5:
cout << " Enter the value to be exponentiated " <<endl;
cin >> a ;
cout << " Enter the exponent" << endl;
cin >> b;
c= pow(a,b);
cout << a << " Rased to the power of " << b << " is: " << c << endl;
break;
bool repeat = true;
}
{
case 6:
cout << " Enter the value that you want the sine to be taken of" <<endl;
cin >> a ;
c=sin(a);
cout << " The sine of " << a << " is: " << c << endl ;
break;
bool repeat = true;
}
{
case 7:
cout << " Enter the value that you want the cosine to be taken of" <<endl;
cin >> a ;
c=cos(a);
cout << " The cosine of " << a << " is: " << c << endl ;
break;
bool repeat = true;
}
{
case 8:
cout << " Enter the value that you want the tangent to be taken of" <<endl;
cin >> a ;
c=tan(a);
cout << " The tangent of " << a << " is: " << c << endl ;
break;
bool repeat = true;
}
{
case 0:
cout << "Ending the program" << endl;
return 0;}
break;
bool repeat = true;
}
} while (repeat = true );
return 0;
}
So here is few moments.
Call
using namespace std;
just believe - is bad idea;
In conditions like if() or while() use operator == instead of =. Because "=" - is assigne operator, and return value depended on success of operation. And "==" is compare operator.
Ow and figure one more missunderstanding. Using bool rezult = true; is wrong. You should use rezult = true; Because every time when you write type specifer you create local variable in context of case, and this don`t affect rezult declared in main
My opinion for your question is little change:
from:
do{
int x;
...
case 0:
cout << "Ending the program" << endl;
return 0;}
break;
bool repeat = true;
}
} while (repeat = true );
to
do{
int x;
...
case 0:
cout << "Ending the program" << endl;
repeat = false;}
break;
}
} while (repeat == true);
and if you need a bit more calculations you can wrapped it to new cicle something like:
while(new_condtion == true) {
do {
...
} while(repeat == true);
//change new_condtion
}
Don't redefine repeat within switch case. This creates a different variable named repeat which, although it has the same name, is not the variable named repeat defined before the loop. This is what you get when you copy a definition of the form bool repeat = true; into multiple places.
The continuation condition for the loop (repeat = true) will also loop forever. Comparison is two = signs, not one.

C++ Calculator Prog. Issue

If anyone has some suggestions - I could utilize the help. The program compiles, but does not
compute properly. I have tried numerous reitterations and I am at an impase. Any suggestions?
I have to initialize all these variables? Thank you....
//This program mimics a calculator
//*****************************************
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
double firstint, secint, ansrlt, ansrlt2, ansrlt3, ansrlt4;
char operat;
cout << fixed << showpoint;
cout << setprecision(4);
cout << "This program mimics a standard calculator and outputs a result accurate to 4 decimal
places." <<endl;
cout << '\n';
cout << "Please enter 1st Integer to be calculated: ";
cin >> firstint;
cout << '\n';
cout << "Please enter operation to be performed in the form of *, /, +, - : ";
cin >> operat;
cout << '\n';
cout << "Please enter 2nd Integer to be calculated: ";
cin >> secint;
cout << '\n';
if (operat == '/' && secint == 0)
cout << "Division by 0 not allowed enter again." << endl;
cin.get();
if (operat == '*')
ansrlt = firstint * secint;
cout << firstint << " " << operat << " " << secint << " = " << ansrlt << endl;
cin.get();
if (operat == '/')
ansrlt2 = firstint / secint;
cout << firstint << " " << operat << " " << secint << " = " << ansrlt2 << endl;
cin.get();
if (operat == '+')
ansrlt3 = firstint + secint;
cout << firstint << " " << operat << " " << secint << " = " << ansrlt3 << endl;
cin.get();
ansrlt4 = firstint - secint;
cout << firstint << " " << operat << " " << secint << " = " << ansrlt4 << endl;
cin.get();
return 0;
}
Firstly, you should explain what you do, what happens, and what you expected to happen - as that will generally make things a lot easier for
anyone reading your question.
However, you're missing {} around the body of your if's everywhere.
e.g. it should be
if (operat == '*') {
ansrlt = firstint * secint;
cout << firstint << " " << operat << " " << secint << " = " << ansrlt << endl;
}
(And get rid of the cin.get() calls, unless you're using them for debugging purposes)

C++ functions and loops

I have to write a program that simulates an ice cream cone vendor. The user inputs the number of cones, and for each cone, the user inputs the number of scoops, then the flavor(a single character) for each scoop. At the end, the total price is listed. For the pricing, 1 scoop costs 2.00, 2 scoops costs 3.00 and each scoop after 2 costs .75.
I'm having trouble with the pricing. The correct price is displayed if the user only wants one cone.
/*
* icecream.cpp
*
* Created on: Sep 14, 2014
* Author:
*/
#include <iostream>
#include <string>
using namespace std;
void welcome() {
cout << "Bob and Jackie's Ice Cream\n";
cout << "1 scoop - $1.50\n";
cout << "2 scoops - $2.50;\n";
cout << "Each scoop after 2 - $.50\n";
cout << "Ice Cream Flavors: Only one input character for each flavor.\n";
}
bool checkscoops(int scoops) {
int maxscoops = 5;
if ((scoops > maxscoops) || (scoops < 1))
return false;
else
return true;
}
bool checkcones(int cones) {
int maxcones = 10;
if ((cones > maxcones) || cones < 1)
return false;
else
return true;
}
int price(int cones, int numberofscoops) {
float cost = 0.00;
{
if (numberofscoops == 5) {
cost = cost + 5 + (.75 * 3);
}
if (numberofscoops == 4) {
cost = cost + 5 + (.75 * 2);
}
if (numberofscoops == 3) {
cost = cost + 5.75;
}
if (numberofscoops == 2) {
cost = cost + 5.00;
}
if (numberofscoops == 1) {
cost = cost + 2.00;
}
}
cout << "Total price is: " << cost << endl;
}
int buildcone(int numcones) {
char flav1, flav2, flav3, flav4, flav5;
int numberofscoops;
for (int i = 1; i <= numcones; i++) {
cout << "Enter the amount of scoops you wish to purchase. (5 max): ";
cin >> numberofscoops;
checkscoops(numberofscoops);
while (checkscoops(numberofscoops) == false) {
cout << "You are not allowed to buy more than 5 scoops and you "
"cannot buy less than one scoop. Please try again.\n";
cout << "How many scoops would you like?(5 max): ";
cin >> numberofscoops;
checkcones(numberofscoops);
}
cout << "You are buying " << numberofscoops
<< " scoops of ice cream.\n";
if (numberofscoops == 5) {
cout << "Enter flavor 1: ";
cin >> flav1;
cout << "Enter flavor 2: ";
cin >> flav2;
cout << "Enter flavor 3: ";
cin >> flav3;
cout << "Enter flavor 4: ";
cin >> flav4;
cout << "Enter flavor 5: ";
cin >> flav5;
cout << " ( " << flav1 << " )/" << endl;
cout << " ( " << flav2 << " )" << endl;
cout << " ( " << flav3 << " )" << endl;
cout << " ( " << flav4 << " )" << endl;
cout << " ( " << flav5 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
if (numberofscoops == 4) {
cout << "Enter flavor 1: ";
cin >> flav1;
cout << "Enter flavor 2: ";
cin >> flav2;
cout << "Enter flavor 3: ";
cin >> flav3;
cout << "Enter flavor 4: ";
cin >> flav4;
cout << " ( " << flav1 << " )" << endl;
cout << " ( " << flav2 << " )" << endl;
cout << " ( " << flav3 << " )" << endl;
cout << " ( " << flav4 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
if (numberofscoops == 3) {
cout << "Enter flavor 1: ";
cin >> flav1;
cout << "Enter flavor 2: ";
cin >> flav2;
cout << "Enter flavor 3: ";
cin >> flav3;
cout << " ( " << flav1 << " )" << endl;
cout << " ( " << flav2 << " )" << endl;
cout << " ( " << flav3 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
if (numberofscoops == 2) {
cout << "Enter flavor 1: ";
cin >> flav1;
cout << "Enter flavor 2: ";
cin >> flav2;
cout << " ( " << flav1 << " )" << endl;
cout << " ( " << flav2 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
if (numberofscoops == 1) {
cout << "Enter a flavor: ";
cin >> flav1;
cout << " ( " << flav1 << " )" << endl;
cout << " \\"
<< " /" << endl << " |" << endl;
}
}
price(numcones, numberofscoops);
}
int main() {
int numberofcones;
int numberofscoops;
welcome();
cout << "How many cones would you like?(10 max) ";
cin >> numberofcones;
checkcones(numberofcones);
while (checkcones(numberofcones) == false) {
cout << "You are not allowed to buy more than 10 cones and you cannot "
"buy less than one cone. Please try again.\n";
cout << "How many cones would you like?(10 max): ";
cin >> numberofcones;
checkcones(numberofcones);
}
cout << "You are buying " << numberofcones << " ice cream cones.\n";
buildcone(numberofcones);
}
Start by changing the return value of price() to float, or the function won't be able to return the proper cost. Also, since cones is not used to compute the cost of the purchase, we don't it as a parameter:
float price(int numberofscoops)
{
float total_cost = 0.0f;
if (numberofscoops == 1) {
total_cost = 2.0f;
}
else if (numberofscoops == 2) {
total_cost = 3.0f;
}
else if (numberofscoops > 2) {
total_cost = 5.0f + ((numberofscoops-2) * 0.75f);
}
return total_cost;
}
You code could have other problems, but I think these changes will let you continue to debug and fix the code on your own.
Your while() loop is flawed. Comment your call to checkcones() as shown below. You're already calling checkcones() as the conditional in your while(), no need to evaluate again as this will sent you into a perma-loop. You've got two of these while() statements that I could see, you'll want to comment out both.
while ( checkcones( numberofcones ) == false )
{
cout << "You are not allowed to buy more than 10 cones and you cannot buy less than one cone. Please try again.\n";
cout << "How many cones would you like?(10 max): ";
cin >> numberofcones;
// THIS LINE IS THE PROBLEM :)
// checkcones(numberofcones);
}
After this fix, your program begins to work but the pricing fails. You should be able to figure that out with the answer given above.
I would also see if you can figure out how to implement a c++ class with members and methods as your current approach is very "c" like. Happy coding! :)

How to sort alphabetically in c++ programming

I'm trying to sort alphabetically by team for my program, but not having any luck. If there is any hints or advice out there I would appreciate it. Below is the program I have minus the data input. Basically I just want to know how I would go about specifically sorting only by team in alphabetical order.
nflrecievers data[100];
ifstream fin;
fin.open("data.txt");
ofstream fout;
fout.open("validationReport.txt");
int i = 0;
while(!fin.eof())
{
fin >> data[i].fname >> data[i].lname >> data[i].team >> data[i].rec >> data[i].yards >> data[i].avgyrds_percatch >> data[i].tds >> data[i].longest_rec >> data[i].recpasttwenty_yrds >> data[i].yrds_pergame >> data[i].fumbles >> data[i].yac >> data[i].first_dwns ;
i = i + 1;
}
int a;
int b;
cout << " Select NFL Receivers Statistics. Input 1-4 " << endl;
cout << " 1) Receivers with 25+ Rec and 300+ Yards. " << endl;
cout << " 2) Recievers with 3+ TDs and 3+ Rec over 20 Yards. " << endl;
cout << " 3) Recievers with 100+ Yards per game and 15+ First Downs. " << endl;
cout << " 4) Veiw Total Recievers Statistics. " << endl;
cin >> a;
int c = 0;
if (a==1)
{
cout << " Receivers with 25+ Rec and 300+ Yards. " << endl;
while( c < i-1)
{
if(data[c].rec > 25 && data[c].yards > 300)
{
cout << endl;
cout << data[c].fname << " " << data[c].lname << " " << data[c].team << endl;
cout << " Rec: " << data[c].rec << " Yards: " << data[c].yards << endl;
cout << endl;
}
c++;
}
}
else if(a==2)
{
cout << " Recievers with 3+ TDs and 3+ Receptions past 20 Yards. " << endl;
while( c < i-1)
{
if(data[c].tds > 3 && data[c].recpasttwenty_yrds > 3)
{
cout << endl;
cout << data[c].fname << " " << data[c].lname << " " << data[c].team << endl;
cout << " TDs: " << data[c].tds << " Receptions past 20 Yards: " << data[c].recpasttwenty_yrds << endl;
cout << endl;
}
c++;
}
}
else if(a==3)
{
cout << " Recievers who average over 100+ yards per game and 15+ First Downs. " << endl;
while( c < i-1)
{
if(data[c].yrds_pergame > 100 && data[c].first_dwns > 15)
{
cout << endl;
cout << data[c].fname << " " << data[c].lname << " " << data[c].team << endl;
cout << " Average Yards per game: " << data[c].yrds_pergame << " First Downs: " << data[c].first_dwns << endl;
cout << endl;
}
c++;
}
}
else if(a==4)
{
cout << " Select a Reciever: " << endl;
while( c < i-1)
{
cout << c << ") " << data[c].fname << " " << data[c].lname << endl;
c++;
}
cout << " Total NFL Receivers Statistics. " << endl;
cin >> b;
cout << data[b].fname << " " << data[b].lname << endl;
cout << " Team: " << data[b].team << endl;
cout << " Receptions: " << data[b].rec << endl;
cout << " Yards: " << data[b].yards << endl;
cout << " Average Yards Per Catch: " << data[b].avgyrds_percatch << endl;
cout << " Longest Reception: " << data[b].longest_rec << endl;
cout << " Receptions over 20 Yards: " << data[b].recpasttwenty_yrds << endl;
cout << " Yards per game " << data[b].yrds_pergame << endl;
cout << " Fumbles: " << data[b].fumbles << endl;
cout << " Average Yards After Catch " << data[b].yac << endl;
cout << " Total First Downs: " << data[b].first_dwns << endl;
}
return 0;
}
std::sort(std::begin(data), std::end(data),
[](const nflrecievers& a, const nflrecievers& b) { return a.team < b.team; });
I would use std::sort
bool compare_teams(const nflrecievers &a, const nflrecievers &b) {
return a.team < b.team;
}
int i = 0;
while(!fin.eof())
{
fin >> data[i].fname >> data[i].lname >> data[i].team >> data[i].rec >> data[i].yards >> data[i].avgyrds_percatch >> data[i].tds >> data[i].longest_rec >> data[i].recpasttwenty_yrds >> data[i].yrds_pergame >> data[i].fumbles >> data[i].yac >> data[i].first_dwns ;
i = i + 1;
}
std::sort(data, data+i, compare_teams);
for (int i=0;i<c-1;i++)
for (int j=0;j<c-1;j++)
if (data[j].team>data[j+1].team)
{
nflrecievers temp = data[j];
data[j] = data[j+1];
data[j+1] = temp;
}
another solution:
int compare(const void *v1, const void *v2)
{
nflrecievers p1 = *(nflrecievers *)v1, p2 = *(nflrecievers *)v2;
return strcmp(p1.team,p2.team);
}
qsort(data,c,sizeof(data),compare);