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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GrOUsE
    New Member
    • Jan 2015
    • 6

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

    #include<iostre am>
    #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\tEn ter 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;
    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This code:

    Code:
    while(ch=='Y'&&ch=='y');
    ch cannot be Y AND y at the same time. So the expression is always false and the loop stops.

    Comment

    • GrOUsE
      New Member
      • Jan 2015
      • 6

      #3
      Sir, thank you for your answer, but the program still end after i press y... see this image..

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        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( ):

        Code:
        int main()
        {
        	Project obj;
        	char ch;
        	do
        	{
        	obj.enter();
        	obj.disp();
        
        	cout << "Do you want to continue?\t (Y/N)\n";
        	cout << "\n\n";
        	cin >> ch;
        
        	} while (ch == 'Y' || ch == 'y');
        
        }
        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.

        Comment

        • GrOUsE
          New Member
          • Jan 2015
          • 6

          #5
          do while loops do not repeat correctly

          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

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            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.

            Code:
            int main()
            {
               char data[30];
               cout << " Enter data until '\n' " << endl;
               cin.getline( data, 29, '\n' );
               cout << data;
            }
            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.

            Comment

            Working...