samedi 9 février 2019

why is my if statement always true? and how do I fix it?

when I hover over my "if(valid)" statement, the compiler tells me "address of function valid will always evaluate to 'true' "

how do I fix it?

#include<iostream>
#include<iomanip>
#include <algorithm> 
using namespace std;

bool valid (string results);


int main(){
    string results;
  char winnerOfRace=' ';
    double scores[26]={0};
    int AmountOfMembers[26]={0};
    int teams=0,members=0,score=0;


    cout<<"What is the result of the race? Enter 'done' when you have finished: ";
    while(cin>>results)
    {
        if(results=="done")break;
      valid (results);
      if (valid){ //this is where my problem is
      for(int i=0;i<results.size();i++){
        AmountOfMembers[(int)results[i]-65]++; 
        // ^^  this will put each team into one element of an array and add one when there is a new member
            scores[(int)results[i]-65]+=i+1;
      }
      }
      else cout << "INVALID\n";
    }
    score=scores[0];

    for(int i=0;i<26;i++)
    {
        if(AmountOfMembers[i]>0)
        {
            teams++;
            members=AmountOfMembers[i];
        }
    }

    for(int i=0;i<26;i++)
    {
        if(scores[i]>0)
        {
            scores[i]/=members;
            if(scores[i]<score)
            {
                score=scores[i];
                winnerOfRace=(char)(i+65);
            }
        }
    }

    cout<<"There are "<<teams<<" teams."<<endl<<endl;
    cout<<"Each team has "<<members<<" runners."<<endl<<endl;
    cout<<"Team:\t"<<"Score:"<<endl;
    for(int i=0;i<26;i++)
    {
        if(AmountOfMembers[i]>0)
        {
            cout<<(char)(i+65)<<"\t"<<"\t"<<setprecision(2)<<fixed<<scores[i]<<endl;
        }
    }
    cout<<"The winning team is team "<<winnerOfRace<<" with a score of "<<setprecision(2)<<fixed<<scores[(int)(winnerOfRace)-65]<<"."<<endl;

    return 0;
}

bool valid (string results){
  int a[1000] = {};
  int *end = a + results.size();
  bool validity = true;
  int counter = 0;
  for (int i = 0; i < results.size(); i++){
    a[i] = results[i];
  }
  sort(a,end);
  for (int j = 0; j < results.size(); j++){
    if (a[j] == a[j-1]) counter++;
  }
  if (counter < results.size()) validity = false;
  return validity;


}

ignore this part: (SO wont let me post cuz of too much code)

when I hover over my "if(valid)" statement, the compiler tells me "address of function valid will always evaluate to 'true' "when I hover over my "if(valid)" statement, the compiler tells me "address of function valid will always evaluate to 'true' "when I hover over my "if(valid)" statement, the compiler tells me "address of function valid will always evaluate to 'true' "when I hover over my "if(valid)" statement, the compiler tells me "address of function valid will always evaluate to 'true' "when I hover over my "if(valid)" statement, the compiler tells me "address of function valid will always evaluate to 'true' "when I hover over my "if(valid)" statement, the compiler tells me "address of function valid will always evaluate to 'true' "when I hover over my "if(valid)" statement, the compiler tells me "address of function valid will always evaluate to 'true' "

Aucun commentaire:

Enregistrer un commentaire