I have just made a conversion program that convert amount of money from dollar system to old English pound system (when shilling and pens where in use) and vice versa.
The problem is that when I trying to do another operation it doesn't do it.
here is the Header file
#include"bmony.h"
void main()
{
char *s,ch;
do
{
s=newchar[20];
bmony obj(s);
cout<<"Enter your string mony amount"<<endl;
cin.getline(s,strlen(s));
obj.constom();
delete[]s;
cout<<"Do you want to do another conversion (y,n)"<<endl;
cin>>ch;
}while(ch=='y');
}
and here is the output
Enter your string mony amount
$120.55
Your money in old Englis pound system
£2.8.2
Do you want to do another conversion (y,n)
y
Destructing
Enter your string mony amount
This is not money amount
Do you want to do another conversion (y,n)
It basically reports back the exit state of the program to the OS.
Edit: cin.sync(); clears any 'junk' from the stream (cin). consider using it when ever you use cin, it can be good to use it to clear the stream immediately prior to asking for input, just incase the user has been leaning on their keyboard or another function didn't clean the stream when it finished with it. It is also good to use clear() to clear any error flag on the stream.
int i = 0;
while (true)
{
s=newchar[20];
bmony obj(s);
cout<<"Enter your string mony amount"<<endl;
cin.getline(s,strlen(s));
if (i >0)
cin.ignore();
obj.constom();
delete[]s;
cout<<"Do you want to do another conversion (y,n)"<<endl;
cin>>ch;
cin.ignore();
if (ch == 'n' || ch == 'N')
break;
i = 1;
}
Do you want to do another conversion (y,n)
y [enter]
cin >> ch // extracts the 'y', a new line character remains in the stream.on the 2nd iteration:
cin.getline(..) // will appear to skip input, but it is actually extracting and discarding the previous new line character.
and for Nisheeth , I used your code and it worked just fine, but I have another question.
Why do you set i=0; at the end of the loop?
I removed it and the program worked fine , the only difference was that when i=0; exist I have to press two Enter key after input the amount of money.
Thanks allot.
A trivial information, You are misspelling replies as replays.
Now, I had i = 1 at the end of the loop.
It was supposed to prevent the program from ignoring the getline the first time when it is actually working fine.
Another way to do it would be to create a bool type variable and set it to false.
In the condition check for the if statement, put the bool variable, and at the end of the loop set it to true!