Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Need Help with While Loop Lab

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need Help with While Loop Lab

    I am struggling with this program that wants to ask the user how many grades they wish to enter, have the user enter each grade, then determine the average of all the grades. I am stuck with getting the average to computer correctly... Can you guys check to see what I am missing in my code?

    int main()
    {
        //No Local constants
        int counter = 0; //While Loop counter
     
        //Local variables
        int totalGrades; //Max number of loop executions
        int runningTotal; //Running total of loop counter
        int grade;
        int average; //Average of grades
              char endProgram; //Pause screen
     
        /*************** Begin main Function Executables *****************/
     
        //Clear the screen
        system ("cls");
     
        //Prompt user for number of grades they want to enter
        cout << "How many grades would you like to enter?   ";
        cin >> totalGrades;
     
       while (counter < totalGrades) //stop when counter equals value of totalGrades
       {
             runningTotal = counter + grade;
             cout << "Please enter the next grade --->  ";
             cin >> grade;
             counter = counter + 1; // increase counter by 1
     
       } //end while loop
     
       average = runningTotal / totalGrades; //compute average
       cout << average << " is the average of the grades entered." << endl;
     
        //Pause to read output
        cout << "\n\n"; //skip 2 lines
        system("pause");
     
        //Indicate to OS successful termination of program
        return 0;
     
    }//end main


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Need Help with While Loop Lab

    Please read the forum rules. Your duplicate post has been deleted.

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need Help with While Loop Lab

    Sorry about that... Can anybody help with this?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need Help with While Loop Lab

    The code you posted looks like C++.
    This is a java forum.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2012
    Location
    Superior, CO, USA
    Posts
    80
    Thanks
    0
    Thanked 14 Times in 14 Posts

    Default Re: Need Help with While Loop Lab

    Quote Originally Posted by LennDawg View Post
    Can you guys check to see what I am missing in my code?

    runningTotal = counter + grade;
    Why would you add the counter value to the grade here? Don't you want:

    runningTotal = runningTotal + grade;

    instead? Don't forget to initilize runningTotal to zero first also.
    Need Java help? Check out the HotJoe Java Help forums!

Similar Threads

  1. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 11:22 PM
  2. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  3. For loop; Problems with my for loop
    By mingleth in forum Loops & Control Statements
    Replies: 5
    Last Post: November 16th, 2011, 07:24 PM
  4. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  5. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM