hi...
i want to ask about my receipt program again..

this time ,i really like to know the ways how to clear the informations after the user has entered those informations that are neede by the program..
For example,take a look at this sample output :

--------------------------------------------------------------------------------------
CASH RECEIPT PROGRAM
--------------------------------------------------------------------------------------

ENTER THE PRODUCT ID :12345
ENTER THE PRODUCT NAME :test
ENTER THE PRICE FOR SINGLE ITEM :$12
ENTER THE QUANTITY :3

WOULD YOU LIKE TO ENTER ANOTHER ITEM ? (Y/N) : y

------------------------------------------------------------------------------------


So,what i want to know is how if i want to clear the informations about the previous product id,name,price and the quantity that the user have entered before proceed to ask the user again about the informations needed for the next item??


this is the full code of my program :

#include <iostream>
#include <iomanip>
using namespace std;

int main () {

//A class named record to organize the data more easily
struct record{
   int id;
   string name;
   double price;
   int quantity;
   double sale;
   };

  record product[100];
  int id[100];


      int i=0;
      char answer;

      cout<<"--------------------------------------------------------------------------------";
      cout<<"\t\t\t\tCASH RECEIPT PROGRAM"<<endl;
      cout<<"--------------------------------------------------------------------------------";
      cout<<endl;



do{

        cout<<"Enter the Product ID ?:";
        cin>>product[i].id;

//why this is not working correctly???
if (cin.fail()){
      do{
           cin.clear() ; //clear the stream
           while(cin.get() != '\n'); //clear out the junk

           cout<<"Wrong Data Type of Input!";
           cout << "Please Enter the Product ID Again : ";
           cin >> product[i].id;//whats wrong here??

          }while(cin.fail());
}

      cout << "Enter the Product Name : ";
      cin >> product[i].name;

if (cin.fail()){
      do{
           cin.clear() ; //clear the stream
           while(cin.get() != '\n'); //clear out the junk

           cout<<"Wrong Data Type of Input!";
           cout << "Please Enter the Product Name Again : ";
           cin >> product[i].id;//whats wrong here??

          }while(cin.fail());
}


      cout << "Enter the Price For Single Item : ";
      cin >> product[i].price;


if (cin.fail()){
      do{
           cin.clear() ; //clear the stream
           while(cin.get() != '\n'); //clear out the junk

           cout<<"Wrong Data Type of Input!";
           cout << "Please Enter the Price For Single Item Again : ";
           cin >> product[i].id;//whats wrong here??

          }while(cin.fail());
}


      cout << "Enter The Quantity : ";
      cin >> product[i].quantity;


if (cin.fail()){
      do{
           cin.clear() ; //clear the stream
           while(cin.get() != '\n'); //clear out the junk

           cout<<"Wrong Data Type of Input!";
           cout << "Please Enter the Quantity Again : ";
           cin >> product[i].id;//whats wrong here??

          }while(cin.fail());
}




      cout<<endl;

      cout << "Would You like to enter another product? (Y/N) ";
      cin >> answer;


      i++;

      cout<<endl;

      }while(answer == 'y' || answer == 'Y');


cout<<"--------------------------------------------------------------------------------";
cout<<"ID |"<<setw(13)<<"ITEM |"<<setw(12)<<"PRICE |"<<setw(10)<<"QTY |"<<setw(10)<<"SALE"<<endl;
cout<<"--------------------------------------------------------------------------------";

 int index;
for(index=0; index<i; index++){

product[index].sale=(product[index].price)*(product[index].quantity);

// Display all the info about that product
cout <<product[index].id<<setw(10)<<product[index].name<<setw(10)<<"$"<<fixed<<setprecision(2)<<product[index].price<<setw(10)
<<product[index].quantity<<setw(10)<<product[index].sale;

cout<<endl;
}

cout<<endl;

int total = 0;
for(int counter=0; counter < i; counter++)
{
total += product[counter].sale;
}

cout<<"TOTAL SALE IS :"<<total;
cout<<endl;

double payment;

cout<<"PLEASE ENTER THE PAYMENT RECEIVED :";
cin>>payment;
cout<<endl;

while (payment<total){
cout<<"The Amount Of Payment Is Insufficient !"<<endl;
cout<<"PLEASE ENTER THE PAYMENT RECEIVED :";
cin>>payment;
}

cout<<endl;
cout<<"------------------------"<<endl;
cout<<"TOTAL SALE      :$"<<total<<endl;
cout<<"RECEIVE PAYMENT :$"<<payment<<endl;
cout<<"BALANCE         :$"<<payment-total<<endl;
cout<<"------------------------";
cin.get();
}

Recommended Answers

All 16 Replies

You shouldn't have to clear it out, in fact you want to save it for your totaling and output at the end. When you are incrementing i you are getting the next struct from your array when the flow comes back to the top of the while loop. Maybe I'm misunderstanding what you are saying...

ya...i think u misunderstood what is my question....

to make it clear,this is the sample output that i'm expected :

--------------------------------------------------------------------------------------
CASH RECEIPT PROGRAM
--------------------------------------------------------------------------------------

ENTER THE PRODUCT ID :12345
ENTER THE PRODUCT NAME :test
ENTER THE PRICE FOR SINGLE ITEM : $12
ENTER THE QUANTITY :3

WOULD YOU LIKE TO ENTER ANOTHER ITEM ? (Y/N) : y

------------------------------------------------------------------------------------

ok,this time after the user entered "y" the program will becomes like this :------>>>>>


--------------------------------------------------------------------------------------
CASH RECEIPT PROGRAM
--------------------------------------------------------------------------------------

ENTER THE PRODUCT ID :

---------------------------------------------------------------------------------------

so,the screen will becomes empty like i showed above each time the user answered "y"

Could you move your banner into the do-while loop:

do{
      cout<<"--------------------------------------------------------------------------------";
      cout<<"\t\t\t\tCASH RECEIPT PROGRAM"<<endl;
      cout<<"--------------------------------------------------------------------------------";
      cout<<endl;

  cout<<"Enter the Product ID ?:";
        cin>>product[i].id;

etc.

that way you get the header each time.

Also, just a minor thing, but your variable total (towards the end on line 131) should be a double instead of an int.

EDIT: Are you asking about clearing the screen completely in between? I'm really sorry that I misunderstood. For that you should see this thread http://www.daniweb.com/forums/thread76934.html which goes through the pluses and minuses of using different methods.

just use "clrscr();" at the end of main method.

its not portable, but for windows works good...

#include <windows.h>
#include <stdio.h>

void cls(HANDLE hStdOutput)
{
 COORD coordScreen = {0,0};
 DWORD cCharsWritten;
 CONSOLE_SCREEN_BUFFER_INFO csbi;
 DWORD dwConSize;
 
 GetConsoleScreenBufferInfo(hStdOutput, &csbi);
 dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
 FillConsoleOutputCharacter(hStdOutput,(TCHAR)' ',dwConSize,coordScreen,&cCharsWritten);
 GetConsoleScreenBufferInfo(hStdOutput,&csbi);
 FillConsoleOutputAttribute(hStdOutput,csbi.wAttributes,dwConSize, coordScreen,&cCharsWritten);
 SetConsoleCursorPosition(hStdOutput,coordScreen);
}

int main(void)
{
 HANDLE hStdOutput;

 puts("Hello, World!"); 
 hStdOutput=GetStdHandle(STD_OUTPUT_HANDLE);
 puts("     /npress any key to continue.....");
 getchar();
 cls(hStdOutput);
 puts("Screen Cleared!");
 getchar(); 

 return 0;
}

yes yes...
what i want to do is something like what Frederick2 have shown...

but i'm still wondering,how do i apply that code to my program???

could somebody show me please....
Thank You... :)

You're going to have to compile it as a Windows console application. If you are using VC++ you can just select Win32 console after you create a new project. If not which system are you using?
Keep your includes and add his (though change <stdio.h> to <cstdio>).
Put a prototype for his function at the top of your code and put his function after your main().
Then move your banner ("CASH RECEIPT PROGRAM") into your while loop and call his function before you display that.

You could try outputting 50 lines of blank space to the screen every time you want to clear the screen.

void clearscreen()
{
   for(int i = 0; i < 50; ++i)
     cout << '\n';
}

However that will probably leave the cursor in the left lower corner. To reposition the cursor you need to use nonstandard code---at least I've not see a standard way to do that, yet.

I was toying with that idea too. I mean there's a simple solution, not a good solution, but for the purposes of this project (Wiser Ones please avert your eyes for a moment) he/she could probably get away with the system call. That's why I referred him to the other thread so he/she could make an informed decision.

oh....
Thank You soo much to all of you...
I really appreaciate your helps...
now my program is working like what i wish....
Below is the full code of my program that i've modified...

**but,if you dont mind,could somebody helps me to understand this code in my program (i dont understand..) {

void cls(HANDLE hStdOutput)
{
 COORD coordScreen = {0,0};
 DWORD cCharsWritten;
 CONSOLE_SCREEN_BUFFER_INFO csbi;
 DWORD dwConSize;

 GetConsoleScreenBufferInfo(hStdOutput, &csbi);
 dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
 FillConsoleOutputCharacter(hStdOutput,(TCHAR)' ',dwConSize,coordScreen,&cCharsWritten);
 GetConsoleScreenBufferInfo(hStdOutput,&csbi);
 FillConsoleOutputAttribute(hStdOutput,csbi.wAttributes,dwConSize, coordScreen,&cCharsWritten);
 SetConsoleCursorPosition(hStdOutput,coordScreen);
}

and this :

int main () {
HANDLE hStdOutput;
hStdOutput=GetStdHandle(STD_OUTPUT_HANDLE); //why i should put this??what does it means?

and also this :

do{
      void cls(HANDLE hStdOutput);//why should i call this function here?
      cout<<"________________________________________________________________________________";
      cout<<"\t\t\t\tCASH RECEIPT PROGRAM"<<endl;
      cout<<"________________________________________________________________________________";
      cout<<endl;

and below is my full code :

#include <iostream>
#include <iomanip>
#include <windows.h>
#include <cstdio>
using namespace std;

void cls(HANDLE hStdOutput)
{
 COORD coordScreen = {0,0};
 DWORD cCharsWritten;
 CONSOLE_SCREEN_BUFFER_INFO csbi;
 DWORD dwConSize;

 GetConsoleScreenBufferInfo(hStdOutput, &csbi);
 dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
 FillConsoleOutputCharacter(hStdOutput,(TCHAR)' ',dwConSize,coordScreen,&cCharsWritten);
 GetConsoleScreenBufferInfo(hStdOutput,&csbi);
 FillConsoleOutputAttribute(hStdOutput,csbi.wAttributes,dwConSize, coordScreen,&cCharsWritten);
 SetConsoleCursorPosition(hStdOutput,coordScreen);
}


int main () {
HANDLE hStdOutput;
hStdOutput=GetStdHandle(STD_OUTPUT_HANDLE);
//A class named record to organize the data more easily
struct record{
   int id;
   string name;
   double price;
   int quantity;
   double sale;
   };

  record product[100];
  int id[100];


      int i=0;
      char answer;

do{
      void cls(HANDLE hStdOutput);
      cout<<"________________________________________________________________________________";
      cout<<"\t\t\t\tCASH RECEIPT PROGRAM"<<endl;
      cout<<"________________________________________________________________________________";
      cout<<endl;


        cout<<"Enter the Product ID ?:";
        cin>>product[i].id;

//why this is not working correctly???
if (cin.fail()){
      do{
           cin.clear() ; //clear the stream
           while(cin.get() != '\n'); //clear out the junk

           cout<<"Wrong Data Type of Input!";
           cout << "Please Enter the Product ID Again : ";
           cin >> product[i].id;//whats wrong here??

          }while(cin.fail());
}

      cout << "Enter the Product Name : ";
      cin >> product[i].name;

if (cin.fail()){
      do{
           cin.clear() ; //clear the stream
           while(cin.get() != '\n'); //clear out the junk

           cout<<"Wrong Data Type of Input!";
           cout << "Please Enter the Product Name Again : ";
           cin >> product[i].id;//whats wrong here??

          }while(cin.fail());
}


      cout << "Enter the Price For Single Item : ";
      cin >> product[i].price;


if (cin.fail()){
      do{
           cin.clear() ; //clear the stream
           while(cin.get() != '\n'); //clear out the junk

           cout<<"Wrong Data Type of Input!";
           cout << "Please Enter the Price For Single Item Again : ";
           cin >> product[i].id;//whats wrong here??

          }while(cin.fail());
}


      cout << "Enter The Quantity : ";
      cin >> product[i].quantity;


if (cin.fail()){
      do{
           cin.clear() ; //clear the stream
           while(cin.get() != '\n'); //clear out the junk

           cout<<"Wrong Data Type of Input!";
           cout << "Please Enter the Quantity Again : ";
           cin >> product[i].id;//whats wrong here??

          }while(cin.fail());
}




      cout<<endl;

      cout << "Would You like to enter another product? (Y/N) ";
      cin >> answer;


      i++;

      cout<<endl;

cls(hStdOutput);
      }while(answer == 'y' || answer == 'Y');


cout<<"--------------------------------------------------------------------------------";
cout<<"ID |"<<setw(13)<<"ITEM |"<<setw(12)<<"PRICE |"<<setw(10)<<"QTY |"<<setw(10)<<"SALE"<<endl;
cout<<"--------------------------------------------------------------------------------";

 int index;
for(index=0; index<i; index++){

product[index].sale=(product[index].price)*(product[index].quantity);

// Display all the info about that product
cout <<product[index].id<<setw(10)<<product[index].name<<setw(10)<<"$"<<fixed<<setprecision(2)<<product[index].price<<setw(10)
<<product[index].quantity<<setw(10)<<product[index].sale;

cout<<endl;
}

cout<<endl;

double total = 0;
for(int counter=0; counter < i; counter++)
{
total += product[counter].sale;
}

cout<<"TOTAL SALE IS :"<<total;
cout<<endl;

double payment;

cout<<"PLEASE ENTER THE PAYMENT RECEIVED :";
cin>>payment;
cout<<endl;

while (payment<total){
cout<<"The Amount Of Payment Is Insufficient !"<<endl;
cout<<"PLEASE ENTER THE PAYMENT RECEIVED :";
cin>>payment;
}

cout<<endl;
cout<<"------------------------"<<endl;
cout<<"TOTAL SALE      :$"<<total<<endl;
cout<<"RECEIVE PAYMENT :$"<<payment<<endl;
cout<<"BALANCE         :$"<<payment-total<<endl;
cout<<"------------------------";


}

oh....
Thank You soo much to all of you...
I really appreaciate your helps...
now my program is working like what i wish....
Below is the full code of my program that i've modified...

**but,if you dont mind,could somebody helps me to understand this code in my program (i dont understand..) {

void cls(HANDLE hStdOutput)
{
 COORD coordScreen = {0,0};
 DWORD cCharsWritten;
 CONSOLE_SCREEN_BUFFER_INFO csbi;
 DWORD dwConSize;

 GetConsoleScreenBufferInfo(hStdOutput, &csbi);
 dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
 FillConsoleOutputCharacter(hStdOutput,(TCHAR)' ',dwConSize,coordScreen,&cCharsWritten);
 GetConsoleScreenBufferInfo(hStdOutput,&csbi);
 FillConsoleOutputAttribute(hStdOutput,csbi.wAttributes,dwConSize, coordScreen,&cCharsWritten);
 SetConsoleCursorPosition(hStdOutput,coordScreen);
}

and this :

int main () {
HANDLE hStdOutput;
hStdOutput=GetStdHandle(STD_OUTPUT_HANDLE); //why i should put this??what does it means?

and also this :

do{
      void cls(HANDLE hStdOutput);//why should i call this function here?
      cout<<"________________________________________________________________________________";
      cout<<"\t\t\t\tCASH RECEIPT PROGRAM"<<endl;
      cout<<"________________________________________________________________________________";
      cout<<endl;

and below is my full code :

#include <iostream>
#include <iomanip>
#include <windows.h>
#include <cstdio>
using namespace std;

void cls(HANDLE hStdOutput)
{
 COORD coordScreen = {0,0};
 DWORD cCharsWritten;
 CONSOLE_SCREEN_BUFFER_INFO csbi;
 DWORD dwConSize;

 GetConsoleScreenBufferInfo(hStdOutput, &csbi);
 dwConSize = csbi.dwSize.X * csbi.dwSize.Y;
 FillConsoleOutputCharacter(hStdOutput,(TCHAR)' ',dwConSize,coordScreen,&cCharsWritten);
 GetConsoleScreenBufferInfo(hStdOutput,&csbi);
 FillConsoleOutputAttribute(hStdOutput,csbi.wAttributes,dwConSize, coordScreen,&cCharsWritten);
 SetConsoleCursorPosition(hStdOutput,coordScreen);
}


int main () {
HANDLE hStdOutput;
hStdOutput=GetStdHandle(STD_OUTPUT_HANDLE);
//A class named record to organize the data more easily
struct record{
   int id;
   string name;
   double price;
   int quantity;
   double sale;
   };

  record product[100];
  int id[100];


      int i=0;
      char answer;

do{
      void cls(HANDLE hStdOutput);
      cout<<"________________________________________________________________________________";
      cout<<"\t\t\t\tCASH RECEIPT PROGRAM"<<endl;
      cout<<"________________________________________________________________________________";
      cout<<endl;


        cout<<"Enter the Product ID ?:";
        cin>>product[i].id;

//why this is not working correctly???
if (cin.fail()){
      do{
           cin.clear() ; //clear the stream
           while(cin.get() != '\n'); //clear out the junk

           cout<<"Wrong Data Type of Input!";
           cout << "Please Enter the Product ID Again : ";
           cin >> product[i].id;//whats wrong here??

          }while(cin.fail());
}

      cout << "Enter the Product Name : ";
      cin >> product[i].name;

if (cin.fail()){
      do{
           cin.clear() ; //clear the stream
           while(cin.get() != '\n'); //clear out the junk

           cout<<"Wrong Data Type of Input!";
           cout << "Please Enter the Product Name Again : ";
           cin >> product[i].id;//whats wrong here??

          }while(cin.fail());
}


      cout << "Enter the Price For Single Item : ";
      cin >> product[i].price;


if (cin.fail()){
      do{
           cin.clear() ; //clear the stream
           while(cin.get() != '\n'); //clear out the junk

           cout<<"Wrong Data Type of Input!";
           cout << "Please Enter the Price For Single Item Again : ";
           cin >> product[i].id;//whats wrong here??

          }while(cin.fail());
}


      cout << "Enter The Quantity : ";
      cin >> product[i].quantity;


if (cin.fail()){
      do{
           cin.clear() ; //clear the stream
           while(cin.get() != '\n'); //clear out the junk

           cout<<"Wrong Data Type of Input!";
           cout << "Please Enter the Quantity Again : ";
           cin >> product[i].id;//whats wrong here??

          }while(cin.fail());
}




      cout<<endl;

      cout << "Would You like to enter another product? (Y/N) ";
      cin >> answer;


      i++;

      cout<<endl;

cls(hStdOutput);
      }while(answer == 'y' || answer == 'Y');


cout<<"--------------------------------------------------------------------------------";
cout<<"ID |"<<setw(13)<<"ITEM |"<<setw(12)<<"PRICE |"<<setw(10)<<"QTY |"<<setw(10)<<"SALE"<<endl;
cout<<"--------------------------------------------------------------------------------";

 int index;
for(index=0; index<i; index++){

product[index].sale=(product[index].price)*(product[index].quantity);

// Display all the info about that product
cout <<product[index].id<<setw(10)<<product[index].name<<setw(10)<<"$"<<fixed<<setprecision(2)<<product[index].price<<setw(10)
<<product[index].quantity<<setw(10)<<product[index].sale;

cout<<endl;
}

cout<<endl;

double total = 0;
for(int counter=0; counter < i; counter++)
{
total += product[counter].sale;
}

cout<<"TOTAL SALE IS :"<<total;
cout<<endl;

double payment;

cout<<"PLEASE ENTER THE PAYMENT RECEIVED :";
cin>>payment;
cout<<endl;

while (payment<total){
cout<<"The Amount Of Payment Is Insufficient !"<<endl;
cout<<"PLEASE ENTER THE PAYMENT RECEIVED :";
cin>>payment;
}

cout<<endl;
cout<<"------------------------"<<endl;
cout<<"TOTAL SALE      :$"<<total<<endl;
cout<<"RECEIVE PAYMENT :$"<<payment<<endl;
cout<<"BALANCE         :$"<<payment-total<<endl;
cout<<"------------------------";


}

Check this out for those functions: http://msdn.microsoft.com/en-us/library/ms682073(VS.85).aspx (or google any of them with "Win32 functionname" And look up any of the Win32 API tutorials to get an idea about what it can do (goes way beyond just the console).

In terms of the placement of the function call, I think it makes the most sense to do it there, that way you get the clean screen the first time as well. It's just a matter of taste and practicality, you wouldn't want to clear the screen in the middle of the input and you wouldn't want to wait through one cycle before doing it for the first time.

The important thing to glean from this is that there is no command in standard C++ which readily accomplishes this task. So, if you had a user on Linux (and probably Mac, though I don't know for sure) this program wouldn't work for them because it uses the Windows API. Lerner's is a fairly portable solution but puts the cursor in a different spot.

is that possible to do this in the other easier way??r
maybe by using clear stream or something??

Did you read the post that I linked to in post #4? There are 3-4 non-standard ways which are system dependent (the Win32 solution being among those). Something like this http://pdcurses.sourceforge.net/ would be an option but there's no guarantee of portability and you'd have to learn a library.

Its because of stuff like that that I gave up on iostream years and years ago. If you're working in Windows and you've got serious work to do, you need the Win32 Console functions in conjunction with stdio. Just my opinion, of course. And I fully realize I'm a blasphemer. By the way, I didn't look at all your code, but you don't need stdio or cstdio if you aren't using puts(), printf(), or anything like that. I needed it cuz I used puts instead of the more laborious WriteConsole().

you don't have to use any function in winapi
just write
system("cls");
and this instruction of system and clean the board .

and that is :)

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.