We are doing an assignment that reads from a file on the hard drive, asks for user input and then writes it to a different file on the hard drive. We have to use three different functions, besides the main, and cannot use global variables.

I cannot seem to get all of the information I need to print to my output file. Everything prints in the file except the address and tamount. The tamount prints, just not what is input by the user.

Thanks in advance for your help.

#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, address[SIZE];
    int X, pref, tamount;
    
    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;  
   }
   
    ofstream outputFile;  //File to output to
    outputFile.open ("c:\\confirmed.txt", ios::out);
    
    if (outputFile.fail()) //Error Opening File
   { 
     cout << "Error opening file. The file could not be found.\n";
     cout <<""<<endl;
     system ("PAUSE");
     return EXIT_FAILURE;  
   }

  do
    {
    system ("CLS");        
    cout << "Customer Call list:" << endl;
    cout << " " << endl;
    
    // Determine status
    inputFile >> status;
    if (status == 'X' || status == 'x')
   {
      cout << "Preferred Customer offer a 7.9% interest rate!" << endl;
      pref=0;
   }  
    else 
   {
      cout << "Offer a 12.9% interest rate!" << endl; 
      pref=1;
   }  
    
    // Get name
    inputFile >> first >> last;
    cout << first << " " << last << endl;    
    
    // Get phone number
    inputFile >> phone;
    cout << phone << endl;
    cout << "" << endl;
    cout << "" << endl;
    cout << "" << endl;

   char letter;
   
    
        letter = menu ();
        switch (letter)
        {
            case 'A':  accept ('A');
                       cin.getline (address, SIZE);
                       outputFile << first << " " << last << " " << status << " " << address << " " << "New Card" << endl;
                       break; 
            case 'a':  accept ('a');
                       cin.getline (address, SIZE);
                       outputFile << first << " " << last << " " << status << " " << address << " " << "New Card" << endl;
                       break;
            case 'T':  transfer ('T');
                       cin.getline (address, SIZE);
                       outputFile << first << " " << last << " " << status << " " << address << " " << "Transfer: "<< tamount << endl;
                       break;
            case 't':  transfer ('t');
                       cin.getline (address, SIZE);
                       outputFile << first << " " << last << " " << status << " " << address << " " << "Transfer: "<< tamount << endl;
                       break;
            case 'D':  decline ('D');
                       cin.ignore ();
                       break;
            case 'd':  decline ('d');
                       cin.ignore ();
                       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
        char address[SIZE];

          
        cout << "Ask the customer for their address and enter it here: " << endl;
        cin.ignore ();
        cin.getline (address, SIZE);
        system ("CLS");
        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;
        cout << "Press ENTER key for next customer." << endl;
}

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

void transfer (char letter)
{
      const int SIZE = 100; //Length of line
      char address[SIZE], status, pfc, y, Y;
      int tamount;
      
      cout << "Is this a Preferred Customer?" << endl;
      cin  >>  pfc;
             
      if (pfc == 'y' || pfc == 'Y')   
      {
        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.ignore ();
        cin.getline (address, SIZE);
        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;
        cout << "Press ENTER key for next customer." << endl;
      }
      else  
      {
        cout << "There is a transfer limit of $1000.  How much would you like to transfer?" << endl;
        cin  >> tamount;
      
        if (tamount <=1000)
        {
          cout << "" << endl;
          cout << "Ask the customer for their address and enter it here: " << endl;
          cin.ignore ();
          cin.getline (address, SIZE);
          system ("CLS");
          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;
          cout << "Press ENTER key for next customer." << endl;
        }
        if (tamount >1000)
        {
          cout << "You have entered an amount over the transfer limit, please enter an amount up to $1000." << endl;
          cin  >> tamount;          
          cout <<""<<endl;  
          cout << "Ask the customer for their address and enter it here: " << endl;
          cin.ignore ();
          cin.getline (address, SIZE);
          system ("CLS");          
          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;
          cout << "Press ENTER key for next customer." << endl;
        }
      }
} 

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

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

      cout << "Is this a Preferred Customer?" << endl;
      cin  >>  pfc;
      cout << "" << endl;
      system ("CLS");
      
      if (pfc == 'Y' || pfc == 'y')
      {
        cin.ignore ();    
        cout << "Tell the customer, 'There are amazing cash back rewards available to you! Don't miss out on this opportunity!'" << endl;
        cout << " " << endl;
        cout << "Please call 888-555-1234 for a deal you won't be able to pass on!" << endl;
        cout << "" << endl;
        cout << "Press ENTER key for next customer." << endl;
      }
      else 
      {
        cin.ignore ();
        cout << "Thank you for your time and consideration.  Goodbye." << endl;
        cout << " " << endl;
        cout << "Press any key for next customer." << endl;
      }  
}

Where do you get tamount from the user? All I see is a declaration without any sort of input or initialization before you write it to the file. This causes the variable to have a really weird value in it (a.k.a. "garbage").

In your browser, do a find with "tamount" as the search word. The results will jump from line 20 (the declaration) directly to line 91 (the file write), then to line 95 (another file write).

Also, you really should to a tolower() or a toupper() on "letter" to normalize it. Then you only need 3 cases in your switch instead of 6. Either that or combine your cases:

case 'A':
  accept ('A');
  cin.getline (address, SIZE);
  outputFile << first << " " << last << " " << status << " " << address << " " << "New Card" << endl;
  break; 
case 'a':
  accept ('a');
  cin.getline (address, SIZE);
  outputFile << first << " " << last << " " << status << " " << address << " " << "New Card" << endl;
  break;

and

case 'A': 
case 'a':  accept ('a');
  cin.getline (address, SIZE);
  outputFile << first << " " << last << " " << status << " " << address << " " << "New Card" << endl;
  break;

are equivalent, but the second version is less redundant and easier to maintain.

Edited 6 Years Ago by Fbody: n/a

That may be true, but you are not getting the value back from the function in any way. That means that the "tamount" variable in main() is still "garbage" and the value that is input in transfer() is destroyed/lost as soon as the function ends.

Different variables + different scopes + no variable passing = garbage.

Change the arguments/parameters for your functions from the "letter" input char (char letter) to something more relevant. such as (int &tamount) then you will be headed in the right direction.

Edited 6 Years Ago by Fbody: n/a

I decided to try and start fresh. I don't know if I'm headed in the right direction or not but everything is working except that my values still aren't returning to the switch statement so I can output them into my file.

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

using namespace std;

//Function Prototypes
char accept ();
char transfer ();
void decline ();
char menu ();
 
int main()
{
        
    
    const int SIZE = 100; //Length of line
    char first[SIZE], last[SIZE], phone[SIZE], status, address[SIZE];
    int X, pref, tamount;


    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;  
   }
   
    ofstream outputFile;  //File to output to
    outputFile.open ("c:\\confirmed.txt", ios::app);
    
    if (outputFile.fail()) //Error Opening File
   { 
     cout << "Error opening file. The file could not be found.\n";
     cout <<""<<endl;
     system ("PAUSE");
     return EXIT_FAILURE;  
   }

  
   
  do
    {
    system ("CLS");        
    cout << "Customer Call list:" << endl;
    cout << " " << endl;
    
    // Determine status
    inputFile >> status;
    if (status == 'X' || status == 'x')
   {
      cout << "Preferred Customer offer a 7.9% interest rate!" << endl;
      pref=0;
   }  
    else 
   {
      cout << "Offer a 12.9% interest rate!" << endl; 
      pref=1;
   }  
    
    // Get name
    inputFile >> first >> last;
    cout << first << " " << last << endl;    
    
    // Get phone number
    inputFile >> phone;
    cout << phone << endl;
    cout << "" << endl;
    cout << "" << endl;
    cout << "" << endl;

   char letter;
   
    
        letter = menu ();
        switch (letter)
        {
            case 'A':  
            case 'a':  accept ();
                       cin.ignore ();
                       outputFile << status  << " " << first << " " << last << " " << address << " " << "New Customer "<< endl;
                       break;
            case 'T':  
            case 't':  transfer ();
                          cin.ignore ();
                          outputFile << status  << " " << first << " " << last << " " << address << " " << "Transfer: "<< tamount << endl;
                       break;
            case 'D': 
            case 'd':  decline ();
                       break;
        }
    }   while (1);
    return 0;    
}
//******************************************************************************
// Definition of function menu.                                                *
// Displays the menu and asks user to select an option.                        *
//******************************************************************************

char 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;
    return letter;
    while ((letter == 'T' || letter == 't') && (letter =='A' || letter =='a') && (letter == 'D' || letter == 'd'))
    {
       cout << "Invalid Selection.  Enter A, T or D:" << endl;
       cin >> letter;
    }
}
char accept ()
{
        
        const int SIZE = 200; //Length of line
        char address[SIZE];
                     
        cout << "Ask the customer for their address and enter it here: " << endl;
        cin.ignore ();
        cin.getline (address, SIZE);
        system ("CLS");
        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;
        cout << "Press ENTER key for next customer." << endl;
        return address[200];
}
//******************************************************************************
// Definition for letter choice T.                                             *
// This function is for customers who choose to accept the card offer.         *
//******************************************************************************
char transfer ()
{
      const int SIZE = 100; //Length of line
      char address[SIZE], status, pfc, y, Y;
      int tamount;       
            
      cout << "Is this a Preferred Customer?" << endl;
      cin  >>  pfc;
             
      if (pfc == 'y' || pfc == 'Y')   
      {
        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.ignore ();
        cin.getline (address, SIZE);
        system ("CLS");
        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;
        cout << "Press ENTER key for next customer." << endl;
        return tamount, address[2002];       
      }
      else  
      {
        cout << "There is a transfer limit of $1000.  How much would you like to transfer?" << endl;
        cin  >> tamount;
      
        if (tamount <=1000)
        {
          cout << "" << endl;
          cout << "Ask the customer for their address and enter it here: " << endl;
          cin.ignore ();
          cin.getline (address, SIZE);
          system ("CLS");
          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;
          cout << "Press ENTER key for next customer." << endl;
          return tamount, address[2002]; 
        }
        if (tamount >1000)
        {
          cout << "You have entered an amount over the transfer limit, please enter an amount up to $1000." << endl;
          cin  >> tamount;          
          cout <<""<<endl;  
          cout << "Ask the customer for their address and enter it here: " << endl;
          cin.ignore ();
          cin.getline (address, SIZE);
          system ("CLS");          
          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;
          cout << "Press ENTER key for next customer." << endl;
          return tamount, address[200];           
        }
      }
} 
//******************************************************************************
// Definition for letter choice D.                                             *
// This function is for customers who choose to accept the card offer.         *
//******************************************************************************

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

      cout << "Is this a Preferred Customer?" << endl;
      cin  >>  pfc;
      cout << "" << endl;
      system ("CLS");
      
      if (pfc == 'Y' || pfc == 'y')
      {
        cin.ignore ();    
        cout << "Tell the customer, 'There are amazing cash back rewards available to you! Don't miss out on this opportunity!'" << endl;
        cout << " " << endl;
        cout << "Please call 888-555-1234 for a deal you won't be able to pass on!" << endl;
        cout << "" << endl;
        cout << "Press ENTER key for next customer." << endl;
      }
      else 
      {
        cin.ignore ();
        cout << "Thank you for your time and consideration.  Goodbye." << endl;
        cout << " " << endl;
        cout << "Press any key for next customer." << endl;
      }  
}

Edited 6 Years Ago by tallygal: n/a

You have the exact same error you did before. You are not getting values back from your functions properly. In addition, you have compounded the situation by using return improperly. You can only return a single value. On line 93, you are attempting to return both tamount and address from main() which causes program termination with a failure status code. This is essentially the same as if you had explicitly said return EXIT_FAILURE .

I don't think you understand how functions work. Here are a couple pages that should help you:
cplusplus.com function tutorial I (covers what functions are and return values)
cplusplus.com function tutorial II (covers variable passing conventions)

Edited 6 Years Ago by Fbody: n/a

You're right, I don't completely understand how they work. I'm taking and online beginning C++ class and trying to learn this on my own. Thank you for the links.

Has your class covered functions or is it covering them right now?

It is not advisable to get too far ahead of your class if you are not completely comfortable with the class' current topic. A good programming class is built such that the topics build on each other bit-by-bit. First you learn about variables, then you learn about control structures, then functions, then passing conventions, etc., etc.

It's just like construction, if the foundation is shaky, the building will collapse. Except that if a programmer has a shaky foundation, their programs will simply explode. :P

We're doing them right now. She said that we don't have to pass information in this particular assignment but that it was good practice.

So I've decided to just force my output to file within each function since I can't get my functions working right. However, my problem is when I output the address to file, it cuts off the first half of the address line. I've tried using cin.getline and std::getline and they both do the same thing. Any ideas?

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

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, address[SIZE];
    int X, pref, tamount;


    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;  
   }
   
    ofstream outputFile;  //File to output to
    outputFile.open ("c:\\confirmed.txt", ios::out);
    
    if (outputFile.fail()) //Error Opening File
   { 
     cout << "Error opening file. The file could not be found.\n";
     cout <<""<<endl;
     system ("PAUSE");
     return EXIT_FAILURE;  
   }

  
   
  do
    {
    system ("CLS");        
    cout << "Customer Call list:" << endl;
    cout << " " << endl;
    
    // Determine status
    inputFile >> status;
    if (status == 'X' || status == 'x')
   {
      cout << "Preferred Customer offer a 7.9% interest rate!" << endl;
      pref=0;
   }  
    else 
   {
      cout << "Offer a 12.9% interest rate!" << endl; 
      pref=1;
   }  
    
    // Get name
    inputFile >> first >> last;
    cout << first << " " << last << endl;    
    
    // Get phone number
    inputFile >> phone;
    cout << phone << endl;
    cout << "" << endl;
    cout << "" << endl;
    cout << "" << endl;

   char letter;
   
    
        letter = menu ();
        switch (letter)
        {
            case 'A':  
            case 'a':  accept ('a');
                       outputFile << status << " " << first << " " << last << endl;
                       cin.ignore ();
                       break;
            case 'T':  
            case 't':  transfer ('t');
                       outputFile << status << " " << first << " " << last << endl;
                       cin.ignore ();
                       break;
            case 'D': 
            case 'd':  decline ('d');
                       cin.ignore ();
                       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)
{
        
       std::string address;
        
        ofstream outputFile;  //File to output to
        outputFile.open ("c:\\confirmed.txt", ios::app); 
                 
        std::cout << "Ask the customer for their address and enter it here: " << endl;
        cin.ignore (500,'\n');
        std::getline (std::cin, address);
        outputFile << " " << address << " " << "-- New Card" << endl;
        system ("CLS");
        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;
        cout << "Press ENTER key for next customer." << endl;
        
}

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

void transfer (char letter)
{
      const int SIZE = 100; //Length of line
      char address[SIZE], status, pfc, y, Y, first[SIZE], last[SIZE];
      int tamount;       
      
      ofstream outputFile;  //File to output to
      outputFile.open ("c:\\confirmed.txt", ios::app);
      
      cout << "Is this a Preferred Customer?" << endl;
      cin  >>  pfc;
             
      if (pfc == 'y' || pfc == 'Y')   
      {
        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.ignore ();
        cin.getline (address, SIZE);
        outputFile << " " << address << " -- " << "Transfer: "<< tamount << endl;
        system ("CLS");
        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;
        cout << "Press ENTER key for next customer." << endl;
        
      }
      else  
      {
        cout << "There is a transfer limit of $1000.  How much would you like to transfer?" << endl;
        cin  >> tamount;
      
        if (tamount <=1000)
        {
          cout << "" << endl;
          cout << "Ask the customer for their address and enter it here: " << endl;
          cin.ignore ();
          cin.getline (address, SIZE);
          outputFile << " " << address << " -- " << "Transfer: "<< tamount << endl;
          system ("CLS");
          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;
          cout << "Press ENTER key for next customer." << endl;
          
        }
        if (tamount >1000)
        {
          cout << "You have entered an amount over the transfer limit, please enter an amount up to $1000." << endl;
          cin  >> tamount;          
          cout <<""<<endl;  
          cout << "Ask the customer for their address and enter it here: " << endl;
          cin.ignore ();
          cin.getline (address, SIZE);
          outputFile << " " << address << " -- " << "Transfer: "<< tamount << endl;
          system ("CLS");          
          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;
          cout << "Press ENTER key for next customer." << endl;
          
        }
      }
} 

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

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

      cout << "Is this a Preferred Customer?" << endl;
      cin  >>  pfc;
      cout << "" << endl;
      system ("CLS");
      
      if (pfc == 'Y' || pfc == 'y')
      {
        cin.ignore ();    
        cout << "Tell the customer, 'There are amazing cash back rewards available to you! Don't miss out on this opportunity!'" << endl;
        cout << " " << endl;
        cout << "Please call 888-555-1234 for a deal you won't be able to pass on!" << endl;
        cout << "" << endl;
        cout << "Press ENTER key for next customer." << endl;
      }
      else 
      {
        cin.ignore ();
        cout << "Thank you for your time and consideration.  Goodbye." << endl;
        cout << " " << endl;
        cout << "Press any key for next customer." << endl;
      }  
}

You input and output the address in 4 different places, which one is the one with the issue?

Never duplicate code if you can avoid it. It makes it harder to troubleshoot and maintain your code. If you need to change this in the future, you have to change it in 4 places instead of just 1. Your if statements are similar enough, you should be able to do the transfer part of the input in them then do the address part outside of them, after the closing '}' of the last case.

You might consider putting the address part in a whole separate function, then calling the function when you need to input an address. That way, if you need to change the behavior, you only need to change it once, not four times.

Edited 6 Years Ago by Fbody: n/a

I actually thought about making it a whole separate function. I might just do that. But I'm having issues in the void accept function and the transfer function. They both do the same thing.

Are you required to have the Accept and Transfer be separate menu items. If it's allowed by the assignment, I'm thinking you may want to consider only making the first be Accept Card / Decline Card. You would then make the Transfer option part of the program a sub-section of the Accept part.

i.e.

void accept() {
  /* signal acceptance and thank customer for interest */
  int transferChoice = 0;
  cout << "Does the customer want to transfer a balance?\n";
  cout << "  1. Yes\n" << "  2. No\n";
  cin >> transferChoice

  if ( /* customer wants to transfer balance */ ) {
    /* call transfer() */
  }
  /* thank customer for business */
}

I think your address output issue is the result of re-opening the output file in the new scope without closing it first. The file stream is a pointer to a specific position in the file. If that pointer is pointing at a location that already has data, it will overwrite the existing data. I think that re-opening the file is resetting the pointer.

Edited 6 Years Ago by Fbody: n/a

The main requirements are that we have three functions other than main and don't use globals variables. We have to input from one file and output to another. I was thinking that re-opening was messing it up. I really might have to rethink all of my functions.

Thank you for your help. It really is appreciated.

I finally got it working. It is writing to the output file as needed. It's not the cleanest I could have done it, but it works.

Thanks for all of your help.

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

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, address[SIZE];
    int X, pref, tamount;


    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;  
   }
   
    ofstream outputFile;  //File to output to
    outputFile.open ("c:\\confirmed.txt", ios::app);
    
    if (outputFile.fail()) //Error Opening File
   { 
     cout << "Error opening file. The file could not be found.\n";
     cout <<""<<endl;
     system ("PAUSE");
     return EXIT_FAILURE;  
   }

  
   
  do
    {
    system ("CLS");        
    cout << "Customer Call list:" << endl;
    cout << " " << endl;
    
    // Determine status
    inputFile >> status;
    if (status == 'X' || status == 'x')
   {
      cout << "Preferred Customer offer a 7.9% interest rate!" << endl;
      pref=0;
   }  
    else 
   {
      cout << "Offer a 12.9% interest rate!" << endl; 
      pref=1;
   }  
    
    // Get name
    inputFile >> first >> last;
    cout << first << " " << last << endl;    
    
    // Get phone number
    inputFile >> phone;
    cout << phone << endl;
    cout << "" << endl;
    cout << "" << endl;
    cout << "" << endl;
   
    char letter;
       
        letter = menu ();
        switch (letter)
        {
            case 'A':  
            case 'a':  outputFile << status << " " << first << " " << last << endl;
                       accept ('a');
                       cin.ignore ();
                       break;
            case 'T':  
            case 't':  outputFile << status << " " << first << " " << last << endl;
                       transfer ('t');
                       cin.ignore ();
                       break;
            case 'D': 
            case 'd':  decline ('d');
                       cin.ignore ();
                       break;
        }
       } while (1);
    return 0;    
}

// 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;
}

// This function is for customers who choose to accept the card offer.         
void accept (char letter)
{
        
       const int SIZE = 100; //Length of line
       char address[SIZE];
        
       ofstream outputFile;  //File to output to
       outputFile.open ("c:\\confirmed.txt", ios::app); 
                       
       cout << "Ask the customer for their address and enter it here: " << endl;
       cin.ignore ();
       cin.getline (address, SIZE);
       outputFile << address << " " << "-- New Card" << endl;
       outputFile << endl;
       system ("CLS");
       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;
       cout << "Press ENTER key for next customer." << endl;
       
       outputFile.close ();
        
}

// This function is for customers who choose to accept the card offer.         
void transfer (char letter)
{
      const int SIZE = 100; //Length of line
      char address[SIZE], status, pfc, y, Y, first[SIZE], last[SIZE];
      int tamount;       
      
      ofstream outputFile;  //File to output to
      outputFile.open ("c:\\confirmed.txt", ios::app);
      
      cout << "Is this a Preferred Customer?" << endl;
      cin  >>  pfc;
             
      if (pfc == 'y' || pfc == 'Y')   
      {
        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.ignore ();
        cin.getline (address, SIZE);
        outputFile << " " << address << " -- " << "Transfer: "<< tamount << endl;
        outputFile << endl;
        system ("CLS");
        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;
        cout << "Press ENTER key for next customer." << endl;
        
      }
      else  
      {
        cout << "There is a transfer limit of $1000.  How much would you like to transfer?" << endl;
        cin  >> tamount;
      
        if (tamount <=1000)
        {
          cout << "" << endl;
          cout << "Ask the customer for their address and enter it here: " << endl;
          cin.ignore ();
          cin.getline (address, SIZE);
          outputFile << " " << address << " -- " << "Transfer: "<< tamount << endl;
          outputFile << endl;
          system ("CLS");
          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;
          cout << "Press ENTER key for next customer." << endl;
          
        }
        if (tamount >1000)
        {
          cout << "You have entered an amount over the transfer limit, please enter an amount up to $1000." << endl;
          cin  >> tamount;          
          cout <<""<<endl;  
          cout << "Ask the customer for their address and enter it here: " << endl;
          cin.ignore ();
          cin.getline (address, SIZE);
          outputFile << " " << address << " -- " << "Transfer: "<< tamount << endl;
          outputFile << endl;
          system ("CLS");          
          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;
          cout << "Press ENTER key for next customer." << endl;
      
          outputFile.close ();    
        }
      }
} 

// This function is for customers who choose to accept the card offer.         
void decline (char letter)
{
      
      char status, pfc;  

      cout << "Is this a Preferred Customer?" << endl;
      cin  >>  pfc;
      cout << "" << endl;
      system ("CLS");
      
      if (pfc == 'Y' || pfc == 'y')
      {
        cin.ignore ();    
        cout << "Tell the customer, 'There are amazing cash back rewards available to you! Don't miss out on this opportunity!'" << endl;
        cout << " " << endl;
        cout << "Please call 888-555-1234 for a deal you won't be able to pass on!" << endl;
        cout << "" << endl;
        cout << "Press ENTER key for next customer." << endl;
      }
      else 
      {
        cin.ignore ();
        cout << "Thank you for your time and consideration.  Goodbye." << endl;
        cout << " " << endl;
        cout << "Press any key for next customer." << endl;
      }  
}
This question has already been answered. Start a new discussion instead.