Problem with pointers in Loops

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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#pragma once
#include<iostream>
#include <string>
#include<process.h>
#include<iomanip>
using namespace std;
class bmony
{
private:
	char *str;
	double Dollar, Pound, Shilling, Pens;
public:
	bmony(char*s);
	void constom();
	void disDol() const;
	void disPou() const;
	void itspound();
	void itsdollar();
	~bmony();
};


and here is the definition

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
#include "bmony.h"

bmony::bmony(char*s)
{
	int length = strlen(s); 
    str = new char[length+1]; 
    str=s; 
}


void bmony::constom()
{
	switch (*str)
	{
	case 'E': itspound();
		break;
	case '$': itsdollar();
		break;
	default:
		cout<<"This is not money amount"<<endl;
	}
}

void bmony::itspound()
{
	char *set;
	int n,poundarr[3];
	double p;
	for(int j=0;j<3;j++)
	{
		set=new char[strlen(str)];
	    n=0;
		str++;
		while( *str!='\0' && *str!='.')
		{
			set[n]=*str;
			n++;
			str++;
		}
		poundarr[j]=atoi(set);
		delete[]set;
	}
	p= (poundarr[2]/240) + (poundarr[1]/20) + (poundarr[0]);
	Dollar= p/50;
	bmony::disDol();
}

void bmony::disDol() const
{
	cout<<"The amount in Dollar "<<endl;
	cout<<"$"<<setprecision(2)<<Dollar<<endl;
}

void bmony::itsdollar()
{
	str++;
	float x,po,sh,pe,swap;
	 x = atof(str);
	 x/=50;
	 po = static_cast<int>(x);
	 sh = (x-po)*20;
	 swap=sh;
	 sh=static_cast<int>(swap);
	 pe=static_cast<int>((swap-sh)*12);
	 Pound=po;
	 Shilling=sh;
	 Pens=pe;
	 bmony::disPou();
}

void bmony::disPou() const
{
	cout<<"Your money in old Englis pound system"<<endl;
	cout<<char(156)<<Pound<<"."<<Shilling<<"."<<Pens<<endl;
}

bmony::~bmony()
{
	cout<<"Destructing"<<endl;
	//delete[]str;
}


and here is the program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include"bmony.h"
void main()
{
	char *s,ch;
	do
	{
		s=new char[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)

Some body answer me please
Firstly, using, void main() is not standard. Main should return a int.

Now the problem is that it is skipping the getline on line 10?
Try adding this cin.sync(); after line 10.
Firstly, thanks for replay

But why main() should return value?

and cin.sync(); after lline10. doesn't solve the problem, nothing changed at all.

and another question What cin.sync(); really do?

Thanks.
closed account (z05DSL3A)
But why main() should return value?
because the standard says so.

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.
Last edited on
Try changing content of main () to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int i = 0;
while (true)
{		
	s=new char[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;
}


As for cin.sync():
http://www.cplusplus.com/reference/iostream/istream/sync/

&

http://www.cplusplus.com/forum/beginner/45543/
closed account (DSLq5Di1)
Explanation of what is happening,
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.
Thanks Everybody for replays.

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!
Nisheeth

Excuse me for my bad English.
And I'm very thankful for your replies.

Thanks.
You are welcome!
Topic archived. No new replies allowed.