Solved

C++ program

Posted on 2000-03-16
4
269 Views
Last Modified: 2010-04-02
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
Comment
Question by:licker
4 Comments
 
LVL 1

Expert Comment

by:Binder
Comment Utility
 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
 

Expert Comment

by:mmachie
Comment Utility
what value is in input?  you never set it.
0
 

Accepted Solution

by:
hazman earned 100 total points
Comment Utility
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
 

Author Comment

by:licker
Comment Utility
Thanx alot.  Why did it make such a difference using void instead of int?
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
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 learn how to clear a vector as well as how to detect empty vectors in C++.

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now