473,508 Members | 2,425 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

my do while loop never repeat... plese help!!

GrOUsE
6 New Member
#include<iostream>
#include<string>

using namespace std;

class Project
{
char ch, name[30], add[30], subj[15];
int age;
float msal,gross, lfee[11], annual, total[11];
public:
void enter();
void disp();
};


void Project::enter()
{
do
{
cout<<"\nEnter name: ";
gets(name);
cout<<"\nEnter address: ";
gets(add);
cout<<"\nEnter subject taken: ";
gets(name);
cout<<"\nEnter monthly salary: ";
cin>>msal;

gross = msal*12;

cout<<"\n\t\tEnter late fee";
{
for(int i=0; i<12; i++)
{
cout<<"\nIn month "<<(i+1)<<":\t";
cin>>lfee[i];

total[i] = total[i] + lfee[i];
annual = msal-total[i];
}

};
cout<<"Do you want to continue?\t (Y/N)\n";
cout"\n\n";
cin>>ch;
}
while(ch=='Y'&&ch=='y');

};
void Project::disp()
{
cout<<"total salary: "<<annual;
};
int main()
{
Project obj;

obj.enter();
obj.disp();

return 0;
}
Jan 9 '15 #1
5 1489
weaknessforcats
9,208 Recognized Expert Moderator Expert
This code:

Expand|Select|Wrap|Line Numbers
  1. while(ch=='Y'&&ch=='y');
  2.  
ch cannot be Y AND y at the same time. So the expression is always false and the loop stops.
Jan 9 '15 #2
GrOUsE
6 New Member
Sir, thank you for your answer, but the program still end after i press y... see this image..

http://bytes.com/attachments/attachm...46/dowhile.jpg
Jan 9 '15 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
Your loop is inside Project::enter(). When you leave that function you call Project::disp() and I can see the display of total salary on your screen shot. Once that's done you return to main() where your program completes successfully (that's the "process exited with a return value 0").

If you want to enter another project, then your loop needs to be inside main() and not inside Project::enter():

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     Project obj;
  4.     char ch;
  5.     do
  6.     {
  7.     obj.enter();
  8.     obj.disp();
  9.  
  10.     cout << "Do you want to continue?\t (Y/N)\n";
  11.     cout << "\n\n";
  12.     cin >> ch;
  13.  
  14.     } while (ch == 'Y' || ch == 'y');
  15.  
  16. }
Note that I had to move ch out of your class Project.

Also, each cycle of the loop in main(), reuses the obj variable so the second Project overwrites the data from the previous cycle. If you need t capture multiple projects, then you would need an array of Projects instead of a single Project.
Jan 9 '15 #4
GrOUsE
6 New Member
Sir,
Sorry for the big trouble i made, when the program repeat

cout<<"\nEnter name: ";
this statement appear but the next statement

gets(name);
do not work...

so far i read in the internet... i knew i have to used the flush... i would be glad if you tell me how to use the flush thank you
Jan 11 '15 #5
weaknessforcats
9,208 Recognized Expert Moderator Expert
You have to be careful using gets because it doesn't stop getting characters until you press enter. Your name is an array of 30 characters which is big enough for a 29 character name plus the \0. If you enter a 30 character you would need a 31 character array so this name would corrupt your memory and crash your program.

A better choice is cin.getline because you can specify the maximum number of characters to get. You can also specify a delimiter other than \n if you wish.

Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.    char data[30];
  4.    cout << " Enter data until '\n' " << endl;
  5.    cin.getline( data, 29, '\n' );
  6.    cout << data;
  7. }
As to flush, there is no flush for a input stream since you can't tell if more data is going to be entered later. A flush applies only to an output stream.

Finally, editing input is a very tricky and time consuming business. Unless you are writing some actual code I wouldn't bother with it. Instead just enter data your program expects and don't try to trick it into failing. If, in fact you are writing production code, leave the input editing to the very last after the entre program has been debugged and is working.
Jan 11 '15 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

7
3181
by: DaVinci | last post by:
I am writing a pong game.but met some problem. the ball function to control the scrolling ball, void ball(int starty,int startx) { int di ,i; int dj,j; di = 1; dj = 1; i = starty;
5
12219
by: é›· | last post by:
suggest add do while loop in later version
16
2942
by: LODIE | last post by:
Hello All, I was hoping that someone might have a suggestion on the best way to complete the follwoing problem. I have a bunch of roads. Each road has a Start and end Distance (chainage in...
5
2209
by: =?Utf-8?B?V2lsbGlhbSBGb3N0ZXI=?= | last post by:
Good evening all, I am trying to write a process that uses a while loop to cycle multiple files from an array throught the StreamReader Process. The whole thing works using: Dim...
5
1702
by: jure87 | last post by:
hello everyone im new to this forum and need some help...i have a java code called flight.java and another code called checkflights.java, the purpose of this is to readin a text that is called...
3
4465
by: numlock00 | last post by:
I have a nested 'while' loop that won't repeat, no matter how many times the outer loop repeats. The outer loop reads through an array of elements; the inner loop Ithe 'while' loop) is supposed to...
9
26126
viswammamilla
by: viswammamilla | last post by:
Hai to everyone.I need a help I have an application,in this user want to type his IP address.I want MaskedtextBox for IP address format like 192.168.0.10 like this,in my toolbox i didnt have...
2
2796
by: ocli5568 | last post by:
this main is supposed to receive user input (file names, flags and a keyword), then encrypt the file according to the user input, then close the file pointers. however it goes wrong for some reason....
2
3392
by: John Hutchison | last post by:
Hey everybody, this is my first post, so I hope I'm doing this right. I'm just starting to learn how to program, so I'm sorry if my error is obvious, but I can't seem to find the answer. I...
0
7224
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7323
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7379
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5625
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3192
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1550
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.