help using EOF to stop the loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • complexc
    New Member
    • Apr 2007
    • 2

    help using EOF to stop the loop

    I am having trouble figuring out how to stop from the loop using EOF. I need to use a while loop in main and use EOF to stop it. Can anyone guide me.
    This is part of the program I was asked to do:
    Code:
    cout << "Type EOF if you wish to end\n";
    	while (counter != 'EOF') 
    	{
    		read_scores (test1, test2, test3);
    		grade = calc_grade (test1, test1, test3);
    		display_grade (grade, test1, test2, test3, i);
    		i++;
    
    	}
    i am aware that my loop is endless... plz help
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    You use 'EOF' in single quotes, which turns it into the "end of file" character, not the letters E, O, and F which would be input by the user. (Aside of the fact that you never cin anything...)

    I'd recommend changing that to a sentinel value such as -1, or something else that the user is not likely to input on accident.

    Comment

    • complexc
      New Member
      • Apr 2007
      • 2

      #3
      ok here is a revised version, please tell me what you think
      Code:
      cout << "Enter any number to continue: <EOF> to stop.\n";
      	while (cin >> x)
      		{
      		read_scores (test1, test2, test3);
      		grade = calc_grade (test1, test1, test3);
      		display_grade (grade, test1, test2, test3, i);
      		i++;
      		}
      	return 0;
      btw, why do i get no error messages when i compile the whole program, but when i build the program, i get this message:
      Linking...
      proj51.obj : error LNK2001: unresolved external symbol "char __cdecl calc_grade(int, int,int)" (?calc_grade@@Y ADHHH@Z)
      Debug/prog5.exe : fatal error LNK1120: 1 unresolved externals
      Error executing link.exe.

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by complexc
        ok here is a revised version, please tell me what you think
        Code:
        cout << "Enter any number to continue: <EOF> to stop.\n";
        	while (cin >> x)
        		{
        		read_scores (test1, test2, test3);
        		grade = calc_grade (test1, test1, test3);
        		display_grade (grade, test1, test2, test3, i);
        		i++;
        		}
        	return 0;
        btw, why do i get no error messages when i compile the whole program, but when i build the program, i get this message:
        Linking...
        proj51.obj : error LNK2001: unresolved external symbol "char __cdecl calc_grade(int, int,int)" (?calc_grade@@Y ADHHH@Z)
        Debug/prog5.exe : fatal error LNK1120: 1 unresolved externals
        Error executing link.exe.
        Ok, so let me ask you, with this bit of code:
        Code:
        while (cin >> x) {
        when is that going to exit the while loop? Granted, if the user types in 'EOF' it will, but it will error out, and I'm not sure that's what you want. I'd recommend putting something like a boolean test to set after all 3 of your methods complete properly.

        Other than that, let's take a look at your error:
        Originally posted by error
        proj51.obj : error LNK2001: unresolved external symbol "char __cdecl calc_grade(int, int,int)" (?calc_grade@@Y ADHHH@Z)
        Debug/prog5.exe : fatal error LNK1120: 1 unresolved externals
        Okay, so those together say you have an 'unresolved external' at ' char __cdecl calc_grade(int, int, int)'. With that, how and where is your calc_grade() method defined? Did you provide the implementation for that method? Do the signatures match (three ints passed to it with a return type of int)?

        After having looked at your program, I'd like to ask, why do you have a 'read_scores()' method that is already passed 3 variables? Shouldn't the read_scores() method ... well ... read the scores in? And unless you use an array as a return type, those can only be done one at a time, which means the internal logic of that bit of code is a bit skewed. Maybe something like this would be better?

        Originally posted by pseudocode
        while boolean_stillAd dingGrades {
        read grade in
        add to sum
        increment num_of_grades_r ead
        check to see if boolean_stillAd dingGrades needs to be changed
        }
        calculate average
        display grades

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          This code:

          while (counter != 'EOF')


          should be:

          while (counter != EOF)

          The EOF character is a CTRL+Z indicating end of all input.

          Use it after all your data lines have been entered and then enter CTRL+Z followed by the enter key to get the EOF character into the buffer.

          Comment

          • sicarie
            Recognized Expert Specialist
            • Nov 2006
            • 4677

            #6
            Originally posted by weaknessforcats
            This code:

            while (counter != 'EOF')


            should be:

            while (counter != EOF)

            The EOF character is a CTRL+Z indicating end of all input.

            Use it after all your data lines have been entered and then enter CTRL+Z followed by the enter key to get the EOF character into the buffer.
            What OS/Compiler are you using? I tried that on an FC6/GCC box and could not get it to work (though I'm probably doing something wrong...).

            ::edit:: when I say "I can't get it to work" I mean I can't get it to exit the input loop and still execute code after that (some sort of print or calculation of the input variables) - Ctrl+Z and then Enter will background the program, but will not terminate the input on the while loop for me (I didn't think there was a way to input the EOF char from the keyboard without setting it up specially).

            Comment

            Working...