This program compiles and lets me select a letter from the menu, but then it just repeats the menu, it never outputs the statements that correspond with the letter chosen. Any ideas why?

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>

using namespace std;

//Function Prototypes
void accept ();
void transfer ();
void decline ();
int menu ();

int main()
{
        
    
    const int SIZE = 100; //Length of line
    char first[SIZE], last[SIZE], phone[SIZE], status;
    int X;
       
    ifstream inputFile; 
    inputFile.open ("c:\\potentials.txt", ios::in);
    
   
    if (inputFile.fail()) 
   { 
      cout << "Error opening file. The file could not be found.\n";
      cout <<""<<endl;
      system ("PAUSE");
      return EXIT_FAILURE;  
   }

    // Determine status
    inputFile >> status;
    if (status == 'X' || status == 'x')
   {
      cout << "Preferred -- 7.9%" << endl;
   }  
    else if (!(status == 'X' || status == 'x'))
   {
      cout << "12.9%" << endl; 
   }
    
    // Get name
    inputFile >> first >> last;
    cout << first << " " << last << endl;    
    
    // Get phone number
    inputFile >> phone;
    cout << phone << endl;
    cout << "" << endl;

   char selection;
    
    do
    {
        selection = menu ();
        switch (selection)
        {
            case 1:  accept ();
                     break;
            case 2:  transfer ();
                     break;
            case 3:  decline ();
                     break;
        }
    }   while (1);
    return 0;    
}

//******************************************************************************
// Definition of function menu.                                                *
// Displays the menu and asks user to select an option.                        *
//******************************************************************************

int menu ()
{
    char letter;
    
    cout << "Please press the corresponding letter for the option you would like:" << endl;
    cout << "" << endl;
    cout << "A -- Accept the credit card" << endl;
    cout << "T -- Transfer a balance" << endl;
    cout << "D -- Decline the card" << endl;
    cout << "" << endl;
    cout << "Please enter your choice:" << endl;
    cin  >> letter;
    cout << "" << endl;
    while ((letter == 'T' || letter == 't') && (letter =='A' || letter =='a') && (letter == 'D' || letter == 'd'))
    {
       cout << "Invalid Selection.  Enter A, T or D:" << endl;
       cin >> letter;
    }
    return letter;
}

//******************************************************************************
// Definition for letter choice A.                                             *
// This function is for customers who choose to accept the card offer.         *
//******************************************************************************

void accept ()
{
      char letter, address;
     
      if (letter == 'A' || letter == 'a')
      {
        cout << "Ask the customer for their address and enter it here: " << endl;
        cin >> address;
        cout << "" << endl;
        cout << "Tell The customer, ' Thank you for your order today, expect" << 
        cout << " your card to arrive in the mail soon, happy charging.'" << endl;
        cout << endl;
      }
}

//******************************************************************************
// Definition for letter choice T.                                             *
// This function is for customers who choose to accept the card offer.         *
//******************************************************************************

void transfer ()
{
      char address, letter, status;
      int tamount;
       
      if (letter == 'T' || letter == 't' && status == 'X' || status == 'x')   
      {
        cout << "There is no transfer limit.  How much would you like to transfer?" << endl;
        cin  >> tamount;
        cout << "" << endl;
        cout << "Ask the customer for their address and enter it here: " << endl;
        cin  >> address;
        cout << "" << endl;
        cout << "Tell The customer, ' Thank you for your order today, expect" << 
        cout << " your card to arrive in the mail soon, happy charging.'" << endl;
        cout << endl;
      }
      else if (letter == 'T' || letter == 't' && !(status == 'X' || status == 'x')&& tamount <=1000)
      {
        cout << "There is a transfer limit of $1000.00.  How much would you like to transfer?" << endl;
        cin  >> tamount;
        cout << "" << endl;
        cout << "Ask the customer for their address and enter it here: " << endl;
        cin >> address;
        cout << "" << endl;
        cout << "Tell The customer, ' Thank you for your order today, expect" << 
        cout << " your card to arrive in the mail soon, happy charging.'" << endl;
        cout << endl;
      }
      else if (letter == 'T' || letter == 't' && !(status == 'X' || status == 'x') && tamount <=1000)
      {
        cout << "There is a transfer limit of $1000.00.  How much would you like to transfer?" << endl;
        cin  >> tamount;
        cout << "You have entered an amount over the transfer limit, please try again." << endl;
        cout <<""<<endl;
      }
} 

//******************************************************************************
// Definition for letter choice D.                                             *
// This function is for customers who choose to accept the card offer.         *
//******************************************************************************

void decline ()
{      
      char letter, status;
      
      if (letter == 'D' || letter == 'd' && status == 'X' || status == 'x')
      {
        cout << "Tell then customer, 'There are amazing cash back rewards available to you!" <<
        cout << " Please call 888-555-1234 for a deal you won't be able to pass on!" << endl;
        cout << "" << endl;
      }
      else if (letter == 'D' || letter == 'd' && !(status == 'X' || status == 'x'))
      {
        cout << "Thank you for your time and consideration.  Goodbye." << endl;
        cout << "" << endl;
      }  
}

Your switch statement is looking for selection to correspond with a number:

switch(selection)
{
case 1: doSomething()
    break;
case 2: doSomething()
    break;
case 3: doSomething()
    break;
}

This should be:

switch(selection)
{
case 'T':
case 't':
    doSomething()
    break;
case 'D':
case 'd':
    doSomething()
    break;
case 'A':
case 'a':
    doSomething()
    break;
}

Because your switch is actually checking for a number for the case, it's never actually falling into the case, and instead falling out and back into the do...while loop.

Thanks... I changed it to this, but it still is not reading the if statements

char selection;
    
    do
    {
        selection = menu ();
        switch (selection)
        {
            case 'A':  accept ();
                       break; 
            case 'a':  accept ();
                       break;
            case 'T':  transfer ();
                       break;
            case 't':  transfer ();
                       break;
            case 'D':  decline ();
                       break;
            case 'd':  decline ();
                       break;
        }
    }   while (1);
    return 0;    
}

Thanks... I changed it to this, but it still is not reading the if statements

You are using uninitialized variables local to the functions, consider the following ...

void decline ()
{
  char letter,  // <- A local variable, uninitialized
       status;  // <- A local variable, uninitialized

  // See what values these variables have at this point ...
  std::cout << "letter's value is [" << letter << "]" << std::endl;
  std::cout << "status's value is [" << status << "]" << std::endl;

  std::cout << "About to enter if-statements with those values ..";

  if (letter == 'D' || letter == 'd' && status == 'X' || status == 'x')
  {
...
}

Add those extra lines there and see what the output is. Apparently you need to figure out how to pass the needed information to the functions.

Good catch, Mit.

Tally, what compiler/IDE are you using?

I'm using Bloodshed DEV-C++.

This is my first programming class. I was doing well until we got to loops, but I think I'm starting to pick them up.

Mit --

You're right...it's not passing information. The status and letter come back blank. Let me see what I can figure out on passing information between my functions.

Can you all tell me if i'm even on the right track with this program. I'm really starting to doubt it. I've gotten the switch statements to read the letter and access the right menu, but I also need it to read information from a file on the hard drive. Here are the instructions for the program and what I have so far.

Instructions:
Background

Your assignment this time is to build an interactive program that allows telemarketers to solicit people in a file for credit cards. The program will open up a text file on the C drive filled with potential customers and display processed data to the screen in a way that the telemarketer can use it. The program will then give them choices to continue the sales call and finally, will save output to a file on the C drive for future credit card processing.


Functionality

The program should do the following:


• Read a customer status, name and phone number from a file called C://potentials.txt

o A customer has a preferred status if the field contains an ‘X’. If it contains anything else, they are not preferred.

• Display the name, phone number and the available interest rate for that customer so that the telemarketer can read it, call the customer and offer them a credit card

o Preferred customers get 7.9%, everybody else gets 12.9%

• Display a menu offering the following options:

o Enter ‘A’ to Accept the card
For this option –
 Ask for an address to send the card to
 Accept the address
 Print a thank you message for telemarketer to read telling the customer to expect the card soon, happy charging

o Enter ‘T’ to accept and Transfer a balance
For this option-
 If preferred – tell them there is no transfer limit
• Accept any dollar amount entered
 If not preferred – tell them they can transfer up to $1000
• Accept any dollar amount entered as long as it isn’t greater than 1000 (keep asking until you get something less than 1000)
 Ask for an address to send the card to
 Accept the address
 Print thank you message for telemarketer to read telling the customer to expect the card soon, happy charging

o Enter ‘D’ to Decline the card
For this option
 If preferred – tempt them with cash back rewards and give them a special phone number to call for a better card
 If not preferred – tell them thanks anyhow

• Write the results to a file called C://confirmed.txt

o This file should contain the following:
 Name
 Address
 Status flag (X or whatever)
 If the account is new (A) or transfer (T)
 Any dollar amount they may be transferring.

If the customer declines, they should not appear in the confirmed file

Requirements:

• You must implement at least three functions other than the main function.
• You may not use global variables!


Here is my code so far:

//This program is from Brandi Wilkins
// Assignment 8 -- Telemarketer's Guide
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>

using namespace std;

//Function Prototypes
void accept (char letter);
void transfer (char letter);
void decline (char letter);
int menu ();

int main()
{
        
    
    const int SIZE = 100; //Length of line
    char first[SIZE], last[SIZE], phone[SIZE], status;
    int X, pref;
    
    ifstream inputFile; //File Stream Object
    inputFile.open ("c:\\potentials.txt", ios::in);  //Open File
    
   
    if (inputFile.fail()) //Error Opening File
   { 
      cout << "Error opening file. The file could not be found.\n";
      cout <<""<<endl;
      system ("PAUSE");
      return EXIT_FAILURE;  
   }

    // Determine status
    inputFile >> status;
    if (status == 'X' || status == 'x')
   {
      cout << "Preferred -- 7.9%" << endl;
      pref=0;
   }  
    else 
   {
      cout << "12.9%" << endl; 
      pref=1;
   }
    
    // Get name
    inputFile >> first >> last;
    cout << first << " " << last << endl;    
    
    // Get phone number
    inputFile >> phone;
    cout << phone << endl;
    cout << "" << endl;

   char letter;
    
    do
    {
        letter = menu ();
        switch (letter)
        {
            case 'A':  accept ('A');
                       break; 
            case 'a':  accept ('a');
                       break;
            case 'T':  transfer ('T');
                       break;
            case 't':  transfer ('t');
                       break;
            case 'D':  decline ('D');
                       break;
            case 'd':  decline ('d');
                       break;
        }
    }   while (1);
    return 0;    
}

//******************************************************************************
// Definition of function menu.                                                *
// Displays the menu and asks user to select an option.                        *
//******************************************************************************

int menu ()
{
    char letter;
    
    cout << "Please press the corresponding letter for the option you would like:" << endl;
    cout << "" << endl;
    cout << "A -- Accept the credit card" << endl;
    cout << "T -- Transfer a balance" << endl;
    cout << "D -- Decline the card" << endl;
    cout << "" << endl;
    cout << "Please enter your choice:" << endl;
    cin  >> letter;
    cout << "" << endl;
    while ((letter == 'T' || letter == 't') && (letter =='A' || letter =='a') && (letter == 'D' || letter == 'd'))
    {
       cout << "Invalid Selection.  Enter A, T or D:" << endl;
       cin >> letter;
    }
    return letter;
}

//******************************************************************************
// Definition for letter choice A.                                             *
// This function is for customers who choose to accept the card offer.         *
//******************************************************************************

void accept (char letter)
{
      
      const int SIZE = 100; //Length of line
      float address;

     {
        cout << "Ask the customer for their address and enter it here: " << endl;
        cin  >> address;
        cout << "" << endl;
        cout << "Tell The customer, ' Thank you for your order today, expect" << endl; 
        cout << " your card to arrive in the mail soon, happy charging.'" << endl;
        cout << endl;
     } 
}

//******************************************************************************
// Definition for letter choice T.                                             *
// This function is for customers who choose to accept the card offer.         *
//******************************************************************************

void transfer (char letter)
{
      char address, status;
      int tamount;
       
      if (status == 'X' || status == 'x')   
      {
        cout << "There is no transfer limit.  How much would you like to transfer?" << endl;
        cin  >> tamount;
        cout << "" << endl;
        cout << "Ask the customer for their address and enter it here: " << endl;
        cin  >> address;
        cout << "" << endl;
        cout << "Tell The customer, ' Thank you for your order today, expect" << 
        cout << " your card to arrive in the mail soon, happy charging.'" << endl;
        cout << endl;
      }
      else if (!(status == 'X' || status == 'x') && tamount <=1000)
      {
        cout << "There is a transfer limit of $1000.00.  How much would you like to transfer?" << endl;
        cin  >> tamount;
        cout << "" << endl;
        cout << "Ask the customer for their address and enter it here: " << endl;
        cin >> address;
        cout << "" << endl;
        cout << "Tell The customer, ' Thank you for your order today, expect" << 
        cout << " your card to arrive in the mail soon, happy charging.'" << endl;
        cout << endl;
      }
      else if (!(status == 'X' || status == 'x') && tamount >1000)
      {
        cout << "There is a transfer limit of $1000.00.  How much would you like to transfer?" << endl;
        cin  >> tamount;
        cout << "You have entered an amount over the transfer limit, please try again." << endl;
        cout <<""<<endl;
      }
} 

//******************************************************************************
// Definition for letter choice D.                                             *
// This function is for customers who choose to accept the card offer.         *
//******************************************************************************

void decline (char letter)
{
    // <- A local variable, uninitialized
      char status;  // <- A local variable, uninitialized

  // See what values these variables have at this point ...

      if (status == 'X' || status == 'x')
      {
        cout << "Tell then customer, 'There are amazing cash back rewards available to you!" <<
        cout << " Please call 888-555-1234 for a deal you won't be able to pass on!" << endl;
        cout << "" << endl;
      }
      else if (!(status == 'X' || status == 'x'))
      {
        cout << "Thank you for your time and consideration.  Goodbye." << endl;
        cout << "" << endl;
      }  
}

Thanks everyone, I figured a way around it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.