Thread: cant break loop without ending program

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    28

    cant break loop without ending program

    Double nested loop, first writes grades to a text file and second reads grades from the text and averages, I cant get the break right too finish the program I have tried for 2 days now. any ideas?

    Code:
    #include <iostream>
    #include <fstream>
    
    
    using namespace std;
    
    int main()
    {
    
        int x, studentID, grade1, grade2, grade3, grade4;
        int score, total;
        char again = 'Y';
    
        ofstream file;
        file.open("grade.txt");
    
        while ( total == 0)
        {
        for (x = 0; x < 1; x++)
        {
            
        //enter grades
        cout << "Enter student ID#:" << endl;
        cin >> studentID;
        cout << "Enter grade: " << endl;
        cin >> grade1;
        if (grade1 <0 || grade1 > 100)
        {
            cout << "0 - 100 only! Enter again: " << endl;
            cin >> grade1;
        }
        else if (grade1 <= 0 || grade1 >= 100)
        {
            cout << endl;
        }
        cout << "Enter grade: " << endl;
        cin >> grade2;
            if (grade2 <0 || grade2 > 100)
        {
            cout << "0 - 100 only! Enter again: " << endl;
            cin >> grade2;
        }
        else if (grade2 <= 0 || grade2 >= 100)
        {
            cout << endl;
        }
        cout << "Enter grade: " << endl;
        cin >> grade3;
            if (grade3 <0 || grade3 > 100)
        {
            cout << "0 - 100 only! Enter again: " << endl;
            cin >> grade3;
        }
        else if (grade3 <= 0 || grade3 >= 100)
        {
            cout << endl;
        }
        cout << "Enter grade: " << endl;
        cin >> grade4;
            if (grade4 <0 || grade4 > 100)
        {
            cout << "0 - 100 only! Enter again: " << endl;
            cin >> grade4;
        }
        else if (grade4 <= 0 || grade4 >= 100)
        {
            cout << endl;
        }
        
     
        //file imput
        file << studentID << " " << grade1 << " " << grade2 << " " << grade3 << " " << grade4 << endl;
        }
       
        //exit or not
        cout << "Do you want to enter another?" << endl;
        cin >> again;
        if ( again == 'Y' || again == 'y' )
        {
            cout << "OK lets continue" << endl << endl;
        }
        else if ( again == 'N' || again == 'n' ) 
        {
            break;
        }
    
        }//while loop ends
    
        total = 0;
        
        while ( 400 < score)
        {
            for (x = 0; x < 0; x++)
            {
             score = grade1 + grade2 + grade3 + grade4;   
             ifstream file ("grade.txt");
             file >> score;
             cout << "Average is: " << score / 4 << endl;
             
            }
        }
        
        return 0;
     
    }
    Last edited by Salem; 03-27-2016 at 02:24 AM. Reason: Added code tags, learn to use them yourself

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    First off, you need to improve the indentation a little (especially your nested loops)
    https://en.wikipedia.org/wiki/Indent_style

    Second, start using some functions.
    A development process
    Putting the two major loops into two separate functions will allow you to make much more sense of the program flow (and pave the way for future enhancements).
    Massive main() functions containing everything are just too confusing.

    Code:
        int score, total;
        char again = 'Y';
     
        ofstream file;
        file.open("grade.txt");
     
        while ( total == 0)
    Q: What value is total initialised to, before comparing it with zero?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    28
    Won't let me remove the thread now?

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You are not allowed to delete threads since the questions posted here are archived so that people facing similar problems can benefit from the knowledge posted here.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User DedicatedGamer's Avatar
    Join Date
    Mar 2016
    Posts
    21
    Instead of using Break, you could simply make the condition keeping the loop going to the opposite.

    Code:
    bool bloop = 1;
    while (bloop)
    {
        for (int i = 0; i < 6; i++)
        {
            // Break out of the loops
            i = 6;
            bloop = 0;
        }
    }
    Last edited by DedicatedGamer; 03-29-2016 at 11:47 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. CTL^D not ending my stdin loop
    By csharp100 in forum C Programming
    Replies: 9
    Last Post: 09-16-2013, 12:59 PM
  2. waitpid() in loop, not ending correctly
    By Gatsu in forum C Programming
    Replies: 5
    Last Post: 03-21-2013, 02:05 AM
  3. Never ending loop
    By mikeman in forum C++ Programming
    Replies: 3
    Last Post: 02-15-2010, 12:03 PM
  4. Ending after do while loop
    By Chipmunkey in forum C++ Programming
    Replies: 6
    Last Post: 08-25-2007, 01:55 PM
  5. Problem ending loop
    By carolsue2 in forum C++ Programming
    Replies: 4
    Last Post: 02-16-2006, 12:43 PM

Tags for this Thread