Solved

C++ program

Posted on 2000-03-16
4
273 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

840 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