While Loop
Jul 4, 2014 at 11:24am UTC
Hello everyone, this is my first post here and I am new to cpp. I have tried to make a program for Customers details. Initially I had a problem with while loop. I made a trick of using GOTO and LABEL, that's why the program runs smoothly. But I don't know from where did this new problem arised I can only enter the data till "Customer's purchase details", I cannot enter further detail i.e. "Enter Balance:".Because the program keeps on looping. It was working all fine after adding goto statement. I had just added "Customer's purchase details" later to the program and these are the problems I am facing. Can anyone help me in this please?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
#include "stdafx.h"
#include<iostream>
#include<fstream>
#include<cstdlib>
using std::ofstream;
using std::ifstream;
using namespace std;
class Customer_Details
{
private :
char cust_id[20];
char firstName[10];
char lastName[10];
char purchase_details;
float balance;
public :
void read_data();
void show_data();
void write_data();
void search_data();
};
void Customer_Details::read_data()
{
cout <<"-------------------------------------------------" << endl;
cout << "\nEnter ID Number: " ;
cin >> cust_id;
cout << "Enter Customers First Name: " ;
cin >> firstName;
cout <<"Enter Last Name: " ;
cin >> lastName;
cout <<"Customer's purchase details:" ;
cin >> purchase_details;
cout <<"Enter Balance: " ;
cin >> balance;
cout << endl;
cout <<"-------------------------------------------------" << endl;
}
void Customer_Details::show_data()
{
cout <<"-------------------------------------------------" << endl;
cout <<"Customer ID: " << cust_id << endl;
cout <<"First Name: " << firstName << endl;
cout <<"Last Name: " << lastName << endl;
cout <<"Purchase Details: " << purchase_details << endl;
cout <<"Current Balance: Rs. " << balance << endl;
cout <<"-------------------------------------------------" << endl;
}
void Customer_Details::write_data()
{
ofstream customer_file;
customer_file.open("customerdata.txt" , ios::in|ios::binary|ios::app|ios::out);
read_data();
customer_file.write(reinterpret_cast < char *>(this ), sizeof (*this ));
customer_file.close();
}
void Customer_Details:: search_data()
{
ifstream infile;
infile.open("customerdata.txt" , ios::binary);
if (!infile)
{
cout<<"Error in Opening! File Not Found!!" << endl;
return ;
}
cout<<"\n----------Data from Text file----------" << endl;
while (!infile.eof())
{
if (infile.read(reinterpret_cast <char *>(this ), sizeof (*this ))>0)
{
show_data();
}
}
infile.close();
}
int main()
{
Customer_Details A;
LOOP:int choice = 0;
cout<<"---------- Customer's Data from Text File----------" <<endl;
cout<<"Select one option below " ;
cout<<"\n\t1-->Add record to file" ;
cout<<"\n\t2-->Show record from file" ;
cout<<"\n\t3-->Search Record from file" ;
cout<<"\nEnter your choice: " ;
cin>>choice;
while (choice)
{
switch (choice)
{
case 1:
A.read_data();
goto LOOP;
break ;
case 2:
A.show_data();
goto LOOP;
break ;
case 3:
A.write_data();
goto LOOP;
break ;
case 4:
A.search_data();
goto LOOP;
break ;
default :
cout << "Wrong choice. Would you like to try again?" ;
goto LOOP;
break ;
}
}
getchar();
return 0;
}
Last edited on Jul 7, 2014 at 9:18am UTC
Jul 4, 2014 at 11:37am UTC
Jul 7, 2014 at 9:14am UTC
@coder777 Thank you it worked for me. I have now changed the PURCHASE_DETAILS to int and I enter integer as inputs.
Last edited on Jul 7, 2014 at 9:14am UTC
Topic archived. No new replies allowed.