Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

warning C4305: 'return' : truncation from 'const int' to 'bool' & error C2059: syntax error : '}'

Posted on 2004-04-15
18
Medium Priority
?
1,026 Views
Last Modified: 2008-01-09
Hi,
I have 1 error and 1 warning when I compile my code. Can anyone help?
Thanks
Kaz
0
Comment
Question by:KazIT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 4
  • +3
18 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 10832531
Please show your code.
0
 
LVL 7

Expert Comment

by:leflon
ID: 10832609
hi KazIT,

the variable you return from a function seems to be different from the return type in function declaration.
function returns a const int but the declaration says the return type is bool&.
but i agree with AlexFm, some code would be nice.

leflon
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10832633
Hi KazIT,
Looks like you tried to return an int as bool - if you have something like
return my_int
use
return (my_int != 0);
instead.

There is not much use in returning a bool reference anyway.

Or you have a problem with {} nesting.

Cheers,
Stefan
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:KazIT
ID: 10832707
The entire code isn't finished this is just the part I am up to
I have a set order that the program has to appear in
first the "Welcome function that has to be hard coded"
then "are there any customers Y or N"
while yes continue
then "Please enter your Customer ID"
the customer Id has limits 0000 to 9999
which is the part I am having problems with.
I will post the entire code though

//PREPORCESSOR DIRECTIVES
#include <iostream>         //FOR CIN AND COUT
#include <iomanip>          //SETW
#include <cctype>           //FOR TOUPPER
#include <string>           //FOR STRING CLASS

using namespace std;        //REQUIRED WHEN USING IOSTREAM

//FUNCTION PROTOTYPES
void Welcome (void);
bool AskForStart();
bool AskForCustomerID(int & customerId);
int GetIntegerResponse(int Lower=1000, int Upper=9999);

void DisplayStock (int AxleStock, int BearingsStock, int BrakeStock);
void GetOrderQuantities(int& AxleQty, int& BearingsQty, int& BrakeQty,
                        int AxleStock, int BearingsStock, int BrakeStock);
void UpdatePartsStock(int& AxleStock, int& BearingsStock, int&
                           BrakeStock,int AxleQty,int BearingsQty,
                                       int BrakeQty);
float TotalCostParts(int AxleQty, int BearingsQty, int BrakeQty);
void DisplayInvoice ( int CustomerId, int AxleQty, int BearingsQty,
                      int BrakeQty);
void Reorder (int AxleStock, int BearingsStock, int BrakeStock);

//MAIN FUNCTION
void main()
{
//DEFINE VARIABLES
int AxleStock =0;                  //AXLES IN STOCK
int BearingsStock =0;            //BEARINGS IN STOCK
int BrakeStock =0;          //BRAKES IN STOCK
int CustomerId =0;
int AxleQty =0;
int BearingsQty =0;
int BrakeQty =0;
int Integer =0;              
int QuantityOnHand =0;     //QUANTITY ON HAND (GLOBAL)
int reorderLevel =0;     //REORDER LEVEL
int Lower = 1000;
int Upper = 9999;
float TotalCostOfParts =0.0;     //TOTAL COSTS OF PARTS (GLOBAL)
float Price = 0.0;          //PRICE OF ITEM (GLOBAL)
string Part = " ";          //PART (GLOBAL)

//CALL FUNCTIONS
Welcome ();
AskForStart();
GetIntegerResponse(Lower,Upper);
DisplayStock (AxleStock,BearingsStock,BrakeStock);
GetOrderQuantities(AxleQty,BearingsQty,BrakeQty,AxleStock,BearingsStock,
                           BrakeStock);
UpdatePartsStock(AxleStock,BearingsStock,BrakeStock,AxleQty,BearingsQty,
                         BrakeQty);
TotalCostParts(AxleQty,BearingsQty,BrakeQty);
DisplayInvoice (CustomerId,AxleQty,BearingsQty,BrakeQty);
Reorder (AxleStock,BearingsStock,BrakeStock);
}
//END main()
//-----------------------------------------------------------------------
//THIS FUNCTION DISPLAYS A WELCOME MESSAGE THAT INCLUDES
//YOUR  NUMBER AND NAME
void Welcome (void)
{
string welcomeMessage ="Welcome to S0023838 Karen Stedman Spare Parts.";
string supplying = "We supply parts for your vehicle. ";

 cout << welcomeMessage <<endl
       << supplying <<endl<<endl;}
//-----------------------------------------------------------------------
//THIS OUTPUT ASKS IF THERE ARE ANY CUSTOMERS AND CHECKS FOR INCORRECT INPUT.
bool AskForStart()
{
      char Reply = ' ';
      cout << "Are there any customers? (Y/N) => ";
      cin >> Reply;               //READ USER RESPONSE
      Reply = toupper(Reply);     //CONVERT TO UPPERCASE
      
      // IF WE DON'T HAVE A YES OR NO KEEP PROMPTING UNTIL WE DO
      while ((Reply != 'N') && (Reply != 'Y'));
      {
            cout << "ERROR:  Valid responses are 'Y' or 'N'" <<endl
                  << "Are there any customers? (Y/N) => ";
            cin >> Reply;
            Reply = toupper (Reply);
      }//END while
      return (Reply == 'Y');
}

bool AskForCustomerID(int & customerId) (THIS PART IS WHERE THE PROBLEM IS)
{
      cout << "Please enter your Customer ID  (or -1 to quit)=>";
      cin >> customerId;
      
                   return (customerId != -1);
                  
                   int CustomerId =0;
                  
                   if (!AskForStart())
                         return 1;
                   if (!AskForCustomerID(CustomerId))
                         return 2;
}
int GetIntegerResponse(int Lower,int Upper)
{
      int Response=0;
      do
      {
            int ReadInteger(Response);
            if (Response < Lower || Response > Upper)
            {
                  cerr<<"\n\t\tERROR: Number must be between "
                        << Lower << "and" << Upper << "=>";
            }while (Response < Lower || Response > Upper);
            
            return Response;
      }
}
//----------------------------------------------------------------------
//DEFINE AND INITIALISE PART 1 OBJECTS AND VARIABLES
int Code1 = 1;                              //CODE PART 1
string Part1 = "Axle";               //PART REAR AXLE
float Price1 = 128.00;                    //PRICE PART 1
int QuantityOnHand1 = 5;               //QUANTITY ON HAND PART 1
int reorderLevel1 = 2;                    //REORDER LEVEL PART 1

//DEFINE AND INITIALISE PART 2 OBJECTS AND VARIABLES
int Code2 = 2;                              //CODE PART 2
string Part2 = "Bearings";               //PART BEARINGS
float Price2 = 25.50;                    //PRICE PART 2
int QuantityOnHand2 = 20;               //QUANTITY ON HAND PART 2
int reorderLevel2 = 10;                    //REORDER LEVEL PART 2

//DEFINE AND INITIALISE PART 3 OBJECTS AND VARIABLES
int Code3 = 3;                              //CODE PART 3
string Part3 = "Brake Pads";          //PART BRAKE PADS
float Price3 = 30.00;                    //PRICE PART 3
int QuantityOnHand3 = 12;               //QUANTITY ON HAND PART 3
int reorderLevel3 = 6;                    //REORDER LEVEL PART 3

//THIS FUNCTION DISPLAYS A LIST OF THE ITEMS AVAILABLE, THEIR PRICES
//AND THE NUMBERS OF EACH ITEM THAT ARE AVAILABLE FOR SALE
void DisplayStock (int AxleStock, int BearingsStock, int BrakeStock)
{
      cout << "\nOur current stock is: " <<endl;

//DISPLAY HEADINGS

cout  << setw(4) <<"Code"
      << setw(8) <<"Part"
      << setw(24)<<"Quantity On Hand"
      << setw(8) <<"Price" << endl;

//DISPLAY CURRENT STOCK
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);

cout  << setw(2) << Code1
      << setw(10) << Part1
      << setw(16) << QuantityOnHand1
      << setw(13) << "$ " << Price1 << endl;

cout  << setw(2) << Code2
      << setw(14) << Part2
      << setw(13) << QuantityOnHand2
      << setw(13) << "$  " << Price2 << endl;

cout  << setw(2) << Code3
      << setw(16) << Part3
      << setw(11) << QuantityOnHand3
      << setw(13) << "$  " << Price3 << endl;
}//END Display Stock
//---------------------------------------------------------------------
    //THIS FUNCTION OBTAINS FROM THE USER THE ITEM CODE AND
    //THE QUANTITY OF EACH ITEM THAT ARE REQUIRED BY THE USER

void GetOrderQuantities(int& AxleQty, int& BearingsQty, int& BrakeQty,
                        int AxleStock, int BearingsStock, int BrakeStock)
                                    
{
      int Code ;      //ITEM CODE                  
      cout << "Enter the code for the part you want to buy =>" ;
      cin >> Code;
      int Integer;                               
      cout << "How many of this part would you like => ";
      cin >> Integer;    

}
//----------------------------------------------------------------------
//THIS FUNCTION DISPLAYS THE INVOICE DETAILS
void DisplayInvoice ( int CustomerId, int AxleQty, int BearingsQty,
                               int BrakeQty)
{
         string Part = " ";
         int Quantity = 0;
         float Cost = 0.0;

    cout << "Invoice for Customer Number:" << CustomerId << endl;

    cout << "Part\tQuantity\tCost" <<endl;

    cout << setw(2) << Part
             << setw(10)<< Quantity
             << setw(16)<< Cost<<endl<<endl;
}
//THIS FUNCTION CALCULATES AND RETURNS THE TOTAL COST OF THE ITEMS
//THAT HAVE BEEN ORDERED

float TotalCostParts(int AxleQty, int BearingsQty, int BrakeQty)
{

    cout << "Total Cost of parts: $" << TotalCostParts <<endl;
return 0;
}//END Display Invoice
//--------------------------------------------------------------------
//THIS FUNCTION UPDATES THE QUANTITY OF EACH ELEMENT THAT REMAINS IN STOCK
//AFTER THE CUSTOMER IS SERVED
void UpdatePartsStock(int& AxleStock, int& BearingsStock, int&
                           BrakeStock,
                           int AxleQty, int BearingsQty, int BrakeQty)
{
      int Integer =0;
      int Code =0;
 switch (Code)
     {
     case 1:     AxleQty = AxleStock - Integer;
          break;

     case 2:     BearingsQty = BearingsStock - Integer;
          break;

     case 3:     BrakeQty = BrakeStock - Integer;
          break;
         
     case 4:     cout << "\nInvalid Code......Try again.";
     }//END switch
}
//--------------------------------------------------------------------
//THIS FUNCTION DETERMINES WHICH ITEMS SHOULD BE REORDERED
void Reorder (int AxlesStock, int BearingsStock, int BrakeStock)
{
      int reorderLevel =0;
   if (AxlesStock < reorderLevel)
      {
        cout << "The following parts should be reordered: " <<endl
                   << "Axles" <<endl;
    }
      if (BearingsStock < reorderLevel)
      {
          cout << "The following parts should be reordered: " <<endl
                   << "Bearings" <<endl;
      }
      if (BrakeStock < reorderLevel)
      {
            cout << "The following parts should be reordered: " <<endl
                   << "Brakes" <<endl;
      }
      else
    {
        cout << "No parts to be reordered at present. " << endl;
      }  
   
}

THANKS
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10832722
You are very likely mixing BOOL and bool: I've seen this (a lot) when a windows function returns a BOOL, but you've defined a bool and assign the return code to your bool variable. If you want to get rid of this warning, do something like this (as already suggested by Stefan):

bool myBool = (WinAPIFunctionReturningBool() != 0);

In addition to this you have a missing '}'. This error is unrelated.
0
 
LVL 44

Assisted Solution

by:Karl Heinz Kremer
Karl Heinz Kremer earned 750 total points
ID: 10832738
You are returning a bool, but your return values are "1" and "2". This is wrong. Use false and true instead.
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10832744
Your function declarations don't have the correct return types.
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 10832753
Sorry, I wanted to say "main()" does not have the correct return type: THe standard requires that main() returns int.
0
 
LVL 86

Assisted Solution

by:jkr
jkr earned 600 total points
ID: 10832772
khkremer is right about that. furthermore, 'GetIntegerResponse()' should be

int GetIntegerResponse(int Lower,int Upper)
{
    int Response=0;
    do
    {
         int ReadInteger(Response);
         if (Response < Lower || Response > Upper)
         {
              cerr<<"\n\t\tERROR: Number must be between "
                   << Lower << "and" << Upper << "=>";
         } // <---! closing bracket here
    }while (Response < Lower || Response > Upper);
         
         return Response;
}
0
 

Author Comment

by:KazIT
ID: 10832862
thankyou  khkremer that solved my bool prob.

jkr...placing that closing bracket where you advised allows me to compile and build without errors, however when I run the program and answer Yes or No to "are there any customers" it hangs repeating over and over" Error number must be between 1000 and 9999. " but that shouldn't come up until I ask for the customer ID.  

Kaz
0
 
LVL 86

Expert Comment

by:jkr
ID: 10832922
>>but that shouldn't come up until I ask for the customer ID.  

You might want to implement 'ReadInteger()' then. There is no such function in the above code, thus the runtime problem.
0
 

Author Comment

by:KazIT
ID: 10833096
jkr,
do you mean this after the GetIntegerResponse function?
void ReadInteger(int &Integer)
{
      cin >> Integer;
      while (cin.fail())
      {
            cin.clear();
            Clear();
            cout << "ERROR: You must enter an integer => ";
            cin >> Integer;
      }
      Clear();
}
void Clear(void)
{
      char Char=' ';
      while (Char!='\n')
      {
            cin.get(Char);
      }
}
I'm not sure if this is what you meant.
However, I still get the same run problem with these added 2 functions so I'm obviously wrong again somewhere.

Kaz
0
 
LVL 86

Expert Comment

by:jkr
ID: 10833152
>>do you mean this after the GetIntegerResponse function?

Yes, this was not part of the code you posted. Now, it should read

void Clear(void)
{
    char Char=' ';
    while (Char!='\n')
    {
         cin.get(Char);
    }
}
void ReadInteger(int &Integer)
{
    cin >> Integer;
    while (cin.fail())
    {
         cin.clear();
         Clear();
         cout << "ERROR: You must enter an integer => ";
         cin >> Integer;
    }
    Clear();
}
int GetIntegerResponse(int Lower,int Upper)
{
    int Response=0;
    do
    {
         ReadInteger(Response);
         if (Response < Lower || Response > Upper)
         {
              cerr<<"\n\t\tERROR: Number must be between "
                   << Lower << "and" << Upper << "=>";
         }
    }while (Response < Lower || Response > Upper);
         
         return Response;
}

(right in this order)
0
 

Author Comment

by:KazIT
ID: 10833349
This is a printout of my screen after the run command
Welcome to S0023838 Karen Stedman Spare Parts.
We supply parts for your vehicle.

Are there any customers? (Y/N) => y
ERROR:  Valid responses are 'Y' or 'N' //! gives this response whether Y or N is put in the first time.
Are there any customers? (Y/N) => Y
//!! Nothing happens after this unless I enter a couple of times and key something

1234


2
2

Our current stock is:                                     )//!! this is what is supposed to happen
Code    Part        Quantity On Hand   Price      )after Yes to "are there any customers"
 1      Axle               5           $ 128.00           )
 2      Bearings           20          $  25.50         )
 3      Brake Pads         12          $  30.00        )
Enter the code for the part you want to buy =>1
How many of this part would you like => 2
Total Cost of parts: $00401357 //! I'm still working on this from here down.
Invoice for Customer Number:0
Part    Quantity        Cost
           0            0.00

No parts to be reordered at present.

Kaz
0
 
LVL 86

Expert Comment

by:jkr
ID: 10833688
It seems to me that you should find out what you want your program to do first...
0
 
LVL 44

Assisted Solution

by:Karl Heinz Kremer
Karl Heinz Kremer earned 750 total points
ID: 10834459
Actaully, it's not so much "what you want your program to do first", it's "what you want your program to do next" :-) This is from your main() function:

//CALL FUNCTIONS
Welcome ();
AskForStart();
GetIntegerResponse(Lower,Upper);
DisplayStock (AxleStock,BearingsStock,BrakeStock);

You are missing one call between AskForStart() and GetIntegerResponse().
0
 
LVL 7

Accepted Solution

by:
leflon earned 150 total points
ID: 10834572
and what about the return value of AskForStart()? the prog will continue no matter if you enter 'yes' or 'no'.
give the return values a chance :).

leflon
0
 

Author Comment

by:KazIT
ID: 10837195
Thanks everyone for the assistance

KazIT
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question