Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 279
  • Last Modified:

C++ program

I am working on a program that passes values to/from functions and displays an insurance premium based on certain criteria.  Everything works great except that when I prompt the user to enter "H" for health or "A" for auto, the program always rejects the first answer as invalid.  I've tried putting the loop in different place but it always does the same thing.

#include <iostream.h>
#include <conio.h>



int main()
{

 char input;
 
 int Prompt_User_For_Selection();
 int Service_Selection(char);

 
 Prompt_User_For_Selection();
 clrscr();
 Service_Selection(input);

 return 0;
}

//      submodules



int Prompt_User_For_Selection()
{
 
 char choice;
 
 cout<<"Enter the type of policy needed(H for Health/A for Auto):\n";
 cin>>choice;
 choice=toupper(choice);
 return(choice);


}
int Service_Selection(char selection)
{
  int cost;
  int health_insurance_quote();
  int auto_insurance_quote();
  int display_insurance_quote(int);

 
 for(;selection!='H'&&selection!='A';){
   selection--;
   cout<<"Invalid entry.  Enter H for Health or A for Auto:\n";
   cin>>selection;
   selection=toupper(selection);
 }
 if(selection=='H'){
   cost=health_insurance_quote();
 }
 if(selection=='A'){
   cost=auto_insurance_quote();
 }

 display_insurance_quote(cost);

}
int health_insurance_quote()
{
 int premium1;
 char smoke;
 cout<<"\n\nAre you a smoker(Y/N)?\n";
 cin>>smoke;
 smoke=toupper(smoke);
 for(;smoke!='Y'&& smoke!='N';){
  cout<<"\n\nInvalid answer.  Please enter Y for smoker or N for nonsmoker:\n";
  cin>>smoke;
  smoke=toupper(smoke);
 }
 if(smoke=='Y'){
   premium1=250;
 }
 if(smoke=='N'){
    premium1=190;
 }
 return(premium1);
}
int auto_insurance_quote()
{
 int premium2;
 int tickets;
 cout<<"Enter the number of traffic tickets you have been issued:\n";
 cin>>tickets;
 while(tickets<0){
  cout<<"\n\nInvalid entry.  Please enter the number of tickets you have:\n";
  cin>>tickets;
 }
 if(tickets>=3){
  premium2=175;
 }
 else
  if(tickets==1||tickets==2){
   premium2=140;
  }
  else
   if(tickets==0){
    premium2=95;
   }
 return(premium2);
}
int display_insurance_quote(int quote)
{

 cout<<"\n\nThe cost for your insurance is: $"<<quote<<endl;
 getch();
}
0
licker
Asked:
licker
1 Solution
 
BinderCommented:
 Well, your functionint Prompt_User_For_Selection()  returns a value
which you are not getting in main. Then you pass input to Service_Selection(input);  but what is the value for input? It could
be any value. So you may try:

                      input = Prompt_User_For_Selection();
                      Service_Selection(input);  
0
 
mmachieCommented:
what value is in input?  you never set it.
0
 
hazmanCommented:
I've fixed the problem, and other little problems too. :) Some other functions were not returning any values, so I changed them to void instead of int.

Hazman
---

#include <iostream.h>
#include <conio.h>
#include <stdlib.h>

int main()
{
      
      char input;
      
      int Prompt_User_For_Selection();
      void Service_Selection(char);
      
      
      input = Prompt_User_For_Selection();
      // clrscr();
      Service_Selection(input);
      
      return 0;
}

//      submodules



int Prompt_User_For_Selection()
{
      
      char choice;
      
      cout<<"Enter the type of policy needed(H for Health/A for Auto):\n";
      cin>>choice;
      choice=toupper(choice);
      return(choice);
      
      
}
void Service_Selection(char selection)
{
      int cost;
      int health_insurance_quote();
      int auto_insurance_quote();
      void display_insurance_quote(int);
      
      
      for(;selection!='H'&&selection!='A';){
            selection--;
            cout<<"Invalid entry.  Enter H for Health or A for Auto:\n";
            cin>>selection;
            selection=toupper(selection);
      }
      if(selection=='H'){
            cost=health_insurance_quote();
      }
      if(selection=='A'){
            cost=auto_insurance_quote();
      }
      
      display_insurance_quote(cost);
      
}

int health_insurance_quote()
{
      int premium1;
      char smoke;
      cout<<"\n\nAre you a smoker(Y/N)?\n";
      cin>>smoke;
      smoke=toupper(smoke);
      for(;smoke!='Y'&& smoke!='N';){
            cout<<"\n\nInvalid answer.  Please enter Y for smoker or N for nonsmoker:\n";
            cin>>smoke;
            smoke=toupper(smoke);
      }
      if(smoke=='Y'){
            premium1=250;
      }
      if(smoke=='N'){
            premium1=190;
      }
      return(premium1);
}
 
int auto_insurance_quote()
{
      int premium2;
      int tickets;
      cout<<"Enter the number of traffic tickets you have been issued:\n";
      cin>>tickets;
      while(tickets<0){
            cout<<"\n\nInvalid entry.  Please enter the number of tickets you have:\n";
            cin>>tickets;
      }
      if(tickets>=3){
            premium2=175;
      }
      else
            if(tickets==1||tickets==2){
                  premium2=140;
            }
            else
                  if(tickets==0){
                        premium2=95;
                  }
                  return(premium2);
}

void display_insurance_quote(int quote)
{
      
      cout<<"\n\nThe cost for your insurance is: $"<<quote<<endl;
      getch();
}


0
 
lickerAuthor Commented:
Thanx alot.  Why did it make such a difference using void instead of int?
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now