Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Help fixing appliction bugs

Posted on 2004-09-30
2
Medium Priority
?
269 Views
Last Modified: 2010-04-01
Hello experts,

1. how do i make my console application wait for the user to press enter before an action.

2.I am  using the following code to print a bill, basically this is how it is suppose to work,

I have a data file wre i store customer details in the following format;-

ID   FIRSTNAME   LASTNAME    UNITS   TYPE
1      HARRY          JONES            33       IND

The thing is that often, the customer ID will occur a number of times. I need to be able to search for the first occurence of that customer ID and if the displayed bill is not correct, go to the next occurrence of that name.

here's the code I have so far, it does not work at all.


if (inTransaction.is_open())
{
      
      inTransaction.ignore (20, '\t');
      
      while (!inTransaction.eof())
      {

            inTransaction >> CustNo >> firstName >> lastName >> custCode >> Reading >> UnitsUsed >> AmountToPay;

            
            if (custID == CustNo)
            {
                  
                  cout << "\n\n" << CustNo << '\t' << firstName <<'\t' << lastName << '\t' << custCode << '\t' << Reading << '\t' << UnitsUsed << '\t' << AmountToPay << endl;
                  printBill << "n\n\t\t\t\t\tCUSTOMER BILL.\n\n";

                        printBill <<"\n\n Customer ID:" << CustNo;
                        printBill <<"\n\n Customer Name:" << firstName << " " << lastName;
                        printBill << "\n\n Expanded Rate:" << custCode;
                        printBill << "\n\n\n-----------------------------------------------------------------------";
                        printBill << "\n Previous Reading:\t\t\t\t\t"<< Reading;
                        printBill << "\n Gas Units Used:\t\t\t\t\t"  <<  UnitsUsed;
                        printBill << "\n-----------------------------------------------------------------------";

                        if (custCode == "BUS" || custCode == "IND")
                        {
                              printBill << "\n\nAmount To Pay: = \t\t\t\t\t  £"<<AmountToPay;
                              printBill << "\n-----------------------------------------------------------------------";
                        }
                        else
                        {
                              printBill << "\n\nAmount To Pay: = \t\t\t\t\t" << "****ERROR****";
                              printBill << "\n-----------------------------------------------------------------------";
                        }
                        
                        cout << "\n\nPrinting Customer Bill..!" << endl ;            

            }
            
            
      }
                  
}
else
{
      cout << "file not opened";

}
            inTransaction.close ();
                  printBill.close();
}
                  

0
Comment
Question by:claracruz
2 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 2000 total points
ID: 12189324
Hi clara,

1. To make user wait for a key press, use getch() or maybe system("pause");

2. About your code, you can do something like this:

if (inTransaction.is_open())
{
    bool dontPrintHeader = false;    // Pay attention to the use of this variable

     inTransaction.ignore (20, '\t');
     
     while (!inTransaction.eof())
     {
          inTransaction >> CustNo >> firstName >> lastName >> custCode >> Reading >> UnitsUsed >> AmountToPay;
         
          if (custID == CustNo)
          {
               cout << "\n\n" << CustNo << '\t' << firstName <<'\t' << lastName << '\t' << custCode << '\t' << Reading << '\t' << UnitsUsed << '\t' << AmountToPay << endl;
               if (!dontPrintHeader) {
                    printBill << "n\n\t\t\t\t\tCUSTOMER BILL.\n\n";

                    printBill <<"\n\n Customer ID:" << CustNo;
                    printBill <<"\n\n Customer Name:" << firstName << " " << lastName;
                    printBill << "\n\n Expanded Rate:" << custCode;
                    printBill << "\n\n\n-----------------------------------------------------------------------";
                    printBill << "\n Previous Reading:\t\t\t\t\t"<< Reading;
                    printBill << "\n Gas Units Used:\t\t\t\t\t"  <<  UnitsUsed;
                    printBill << "\n-----------------------------------------------------------------------";
                    dontPrintHeader = true;
                }    

                    if (custCode == "BUS" || custCode == "IND")
                    {
                         printBill << "\n\nAmount To Pay: = \t\t\t\t\t  £"<<AmountToPay;
                         printBill << "\n-----------------------------------------------------------------------";
                    }
                    else
                    {
                         continue;   // Dont print "ERROR" and loop again until valid record is reached
                         //printBill << "\n\nAmount To Pay: = \t\t\t\t\t" << "****ERROR****";
                         //printBill << "\n-----------------------------------------------------------------------";
                    }
                   
                    cout << "\n\nPrinting Customer Bill..!" << endl ;          
          }
     }
}
else
{
     cout << "file not opened";

}
          inTransaction.close ();
               printBill.close();
}
0
 
LVL 9

Expert Comment

by:jhshukla
ID: 12195678
use scanf() or getline()
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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.
Suggested Courses

876 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