Good Afternoon,

I'm getting this errors on the program:
warning C4244: 'argument' : conversion from 'double' to 'const int', possible loss of data
warning C4244: 'argument' : conversion from 'double' to 'const int', possible loss of data
warning C4244: 'argument' : conversion from 'double' to 'const int', possible loss of data
warning C4101: 'doubleResult' : unreferenced local variable

Below is the code for the cpp and header files:
CPP FILE:

#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cmath> 
#include <cstdlib>
#include <iomanip>

//include header file
#include "lab_17_head.h"

using namespace std; 

int main( )
{

    //variable declaration
    int number1, number2;                   //vars to store int values
    double intResult, doubleResult;         //vars to store the result
    char action;                            //var for store the operator
    char flag;                              //var to stor the choice
    int plusWins, plusLosses, plusTotal,    //plus stats for winning, losing, and total games played
        minuWins, minuLosses, minuTotal,    //minus stats for winning, losing, and total games played
        multWins, multLosses, multTotal,    //multiplication stats for winning, losing, and total games played
        diviWins, diviLosses, diviTotal;    //division stats for winning, losing, and total games played
    double answer;                          //var for answer from the user

    //initializations
    plusWins = plusLosses = plusTotal = 0;  
    minuWins = minuLosses = minuTotal = 0;
    multWins = multLosses = multTotal = 0;
    diviWins = diviLosses = diviTotal = 0;  

    //show welcome message
    welcome(); 

    //enter your choice
    getChoice(flag); 

    //start loop to play the game
    while(flag=='y' || flag=='Y')
    {
        //get operator
        getOperator(action); 

        //four cases for action
        switch(action)
        {
            case '+':                       //do addition   
                    //get two random numbers of two digits
                    getTwoNumbers(number1, number2); 
                    //add
                    intResult = add(number1, number2); 
                    //getAnsers from the user
                    getAnswer(number1, number2, action, answer); 
                    //win or loss
                    addGame(intResult, static_cast<int>(answer), plusWins, plusLosses, plusTotal); 
                    break; 

            case '-':                       //do substraction   
                    //get two random numbers of two digits
                    getTwoNumbers(number1, number2); 
                    //add
                    intResult = minus(number1, number2); 
                    //getAnsers from the user
                    getAnswer(number1, number2, action, answer); 
                    //win or loss
                    minuGame(intResult, static_cast<int>(answer), minuWins, minuLosses, minuTotal); 
                    break; 

            case '*':                       //do multiplication 
                    /***************************************************
                     This part is for you to complete
                     ***************************************************/
                    getTwoNumbers(number1, number2);
                    intResult = multiply(number1, number2);
                    getAnswer(number1, number2, action, answer);
                    multiGame(intResult, static_cast<int>(answer), multWins, multLosses, multTotal);
                    break;


            case '/':                       //do division   
                    /***************************************************
                     This part is for you to complete
                     ***************************************************/
                    getTwoNumbers(number1, number2);
                    intResult = divide(number1, number2);
                    getAnswer(number1, number2, action, answer);
                    diviGame(intResult, static_cast<int>(answer), diviWins, diviLosses, diviTotal);
                    break;



            default: 
                    cout<<action<<" is not a valid operator ." <<endl; 
        }

        //get choice
        getChoice(flag); 
    }

    //show stats
    statsShow(plusWins, plusLosses, plusTotal, minuWins, minuLosses, minuTotal,
              multWins, multLosses, multTotal, diviWins, diviLosses, diviTotal);    

    //byebye
    bye(); 

    //well done and exit
    return 0; 
}



Header file

#include <iostream>
#include <cstring>

using namespace std; 

#ifndef     LAB_17_HEAD_H       
#define     LAB_17_HEAD_H

//show welcome message
void welcome()
{
    cout<<" Welcome to my math world and enjoy some training ...... "<<endl; 
    return;                     //no value is returned here 
}

//get the choice
void getChoice(char & ch)                       //use reference paramter to receive the choice 
{
     cout<<"\n Would you like to play the game? (y/n) => "; 
     cin>>ch;                   //get the choice
     return; 
}

//get operator
void getOperator(char & action)                 //use reference paramter to receive the action 
{
    cout<<"\n Which operation do you like to play? (+, -, *, /) => "; 
    cin>>action; 
    return;                     //get the choice
}

//get two random numbers of two digits
void getTwoNumbers(int & number1, int & number2) //use reference paramter to receive the action 
{
    unsigned seed;              //local variable for random seed

    //get a random seed
    cout<<"\n Give me an integer for random =>"; 
    cin>>seed; 

    //set the random seed
    srand(seed);

    //get two random numbers of single digit
    number1 = rand()%10; 
    number2 = rand()%10; 

    return; 
}


//do addition                   
int add(const int number1, const int number2)   //const value parameters
{
    return number1 + number2;   //return the sum
}

//do substraction                   
int minus(const int number1, const int number2) //const value parameters
{
    return number1 - number2;   //return the substraction
}

//do addition                   
int multiply(const int number1, const int number2)//const value parameters
{
    return number1 * number2;   //return the multiplication
}

//do addition                   
double divide(const int number1, const int number2)//const value parameters
{
    return double((double)number1 / (double)number2);   
                                //return the division
}

//getAnsers from the user
void getAnswer(const int number1, const int number2, const char action, 
               double & answer)
{
    cout<<endl<<number1<< "  " <<action <<"  " <<number2<< " = ?  "; 
    cin>>answer; 
}

//addition win or loss
void addGame(const int intResult, const int answer, 
               int & plusWins, int & plusLosses, int & plusTotal)
{
    if (answer == intResult)    //decide winning
        plusWins++; 
    else                        //or loss
        plusLosses++;

    plusTotal++;                //counting the games
}

//substrcation win or loss
void minuGame(const int intResult, const int answer, 
               int & minuWins, int & minuLosses, int & minuTotal)
{
    /*********************************************************
    This part is for you to complete
    *********************************************************/
    //write your code here
    if (answer == intResult)
        minuWins++;
    else
        minuLosses++;
    minuTotal++;
}

//multiplication win or loss
void multiGame(const int intResult, const int answer, 
               int & multWins, int & multLosses, int & multTotal)
{
    /*********************************************************
    This part is for you to complete
    *********************************************************/
    //write your code here
    if (answer == intResult)
        multWins++;
    else
        multLosses++;
    multTotal++;
}

//division win or loss
void diviGame(const double result, const double answer, 
               int & diviWins, int & diviLosses, int & diviTotal)
{
    /********************************************************
    This part is for you to complete
    ********************************************************/
    //write your code here
    if (fabs(answer - result) < 0.01)
        diviWins++;
    else
        diviLosses++;
    diviTotal++;

}

//show stats
void statsShow(const int plusWins, const int plusLosses, const int plusTotal, 
               const int minuWins, const int minuLosses, const int minuTotal,
               const int multWins, const int multLosses, const int multTotal, 
               const int diviWins, const int diviLosses, const int diviTotal)
{
    double plusScore,           //local variable for scores
           minuScore,
           multScore,
           diviScore;

    //compute the scores
    plusScore = plusTotal==0 ? 0: static_cast<double>(plusWins)/static_cast<double>(plusTotal);
    plusScore *= 100;
    minuScore = minuTotal==0? 0: static_cast<double>(minuWins)/static_cast<double>(minuTotal);
    minuScore *= 100;
    multScore = multTotal==0? 0: static_cast<double>(multWins)/static_cast<double>(multTotal);
    multScore *= 100;
    diviScore = diviTotal==0? 0: static_cast<double>(diviWins)/static_cast<double>(diviTotal);
    diviScore *= 100;

    cout<<endl; 

    //for addition
    cout<<" You played "<<plusTotal<<" additions."<<endl
        <<" You won " <<plusWins <<" many times." <<endl
        <<" You lost "<<plusLosses<<" many times." <<endl
        <<" Your addition score is " <<setprecision(2)<<fixed<<showpoint
        << plusScore
        <<endl<<endl;

    //for substraction
    cout<<" You played "<<minuTotal<<" substractions."<<endl
        <<" You won " <<minuWins <<" many times." <<endl
        <<" You lost "<<minuLosses<<" many times." <<endl
        <<" Your substration score is " <<setprecision(2)<<fixed<<showpoint
        << minuScore
        <<endl<<endl;

    //for multiplication
    cout<<" You played "<<multTotal<<" multiplications."<<endl
        <<" You won " <<multWins <<" many times." <<endl
        <<" You lost "<<multLosses<<" many times." <<endl
        <<" Your multiplication score is " <<setprecision(2)<<fixed<<showpoint
        << multScore 
        <<endl<<endl;

    //for division
    cout<<" You played "<<diviTotal<<" divisions."<<endl
        <<" You won " <<diviWins <<" many times." <<endl
        <<" You lost "<<diviLosses<<" many times." <<endl
        <<" Your division score is " <<setprecision(2)<<fixed<<showpoint
        << diviScore 
        <<endl<<endl;

}

//byebye
void bye()
{
    cout<<"Hope you enjoyed playing this game. Byebye." <<endl<<endl;
    return; 
}


#endif

Again with putting code in the header files... sigh. Worse, it is now clear that this horrible practice is being perpetrated and perpetuated by your instructor, who apparently wrote the majority of said code in this fashion, leaving only sections for you to fill out.

<rant>
Either your professor is too lazy to explain how to use project files (or Makefiles), or too incompetent to teach them. Given the excrable quality of the code provided, I am inclined towards the latter. Either way, this course is doing you a disservice, and you'd be better off learning on your own than continuing with this idiot of a professor.
</rant>

Sorry, now that I've got that off my chest... nothing to see, move along.

All kidding aside (wait, who was kidding?), I was able (after moving the code out of the header and into a separate compilation unit.... grrrr) to compile and run this without getting those warnings. I did get a confilct with a part of the STL, but fixed that by renaming minus() to subtract(), after which it compiled cleanly. Why your compiler is catching this and mine isn't doesn't seem clear.

BTW, which compiler and IDE are you using? I may be able to explain how to use the project files so you can link multiple compilation units, if I knew which one you were using.

Edited 4 Years Ago by Schol-R-LEA

Ah, I went and made a copy of the project into Visual C++ Express 2010, and when I compiled it there, I got exactly the same errors you mentioned (which also answers my question about what compiler you used). Checking into things a bit, I found that the variable intResult was actually declared as type double for some reason, which was the cause of the warning - implicitly converting a double to an int does indeed lose information in most cases.

The easiest solution is to change the variable declaration. However, since it looks as if the declaration is part of the code given to you by the instructor, I don't know if the project rules allow you to alter it or not. Assuming you can, then you get another such warning on the line

intResult = divide(number1, number2);

... as divide() returns a double. This can be fixed by explicitly converting the double to an int using the floor() function

intResult = floor(divide(number1, number2));

It still loses information, but now at least it does so explicitly. And now at least the variable name intResult makes sense...

This leaves the last warning, which basically says that you declared doubleResult but never used it. To get rid of the error message, you can just comment the declaration out.

Edited 4 Years Ago by Schol-R-LEA

This article has been dead for over six months. Start a new discussion instead.