Solved

C++ program

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
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.

713 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