Solved

C++ program

Posted on 2000-03-16
4
272 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
ID: 2625057
 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
ID: 2625059
what value is in input?  you never set it.
0
 

Accepted Solution

by:
hazman earned 100 total points
ID: 2625128
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
ID: 2625951
Thanx alot.  Why did it make such a difference using void instead of int?
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

785 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