Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old October 10th, 2004, 01:21 AM
Registered User
 
Join Date: Oct 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default C++ Beginner having (the usual?) getline problems

hi all -

I'm trying to use both cin and getline in the same program, which (from other forum posts here and elsewhere) seems fraught with peril.

The program is an exercise from Beginning C++, 2nd ed. You input up to ten names and grades, and return the average as well as lists of names and grades.

I'm getting a serious weirdness: I have to press ENTER two times to get the program to accept a name.

I've seen that a lot of people seem to have trouble with getline, but none of the solutions I saw elsewhere for their problems seemed to fit. (Or I'm having too much trouble understanding the problem to know when they might fit.)

I did get the Service Pack 6 for Visual Studio 6 -- since there had been a glitch in their getline implementation -- but rebuilding the program didn't show any improvement.

I hope someone can show me the error of my ways, or the way of my errors. The file is included below.

Thanks -- Slappy

-- main.cpp --

// Store first names and scores (0-100) of up to 10 students.
// Use a loop to prompt to enter users and grades.
// Calculate average grade (use a loop counter), display it
// and display the names and grades for all the students in the table.

#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main(void)
{
    const int max = 10;
// const char newLine = '\n';

    string name[max];
    string temp = "";
    int grade[max] = {0};
    int average = 0;
    int count = 0;
    int total = 0;
    char yesno = 0;

    cout << "This program averages the grades of up to 100 students." << endl;

    for (count = 0; count < max; count++)
    {
        cout << "Enter student's first name: ";
// cin >> name[count];

        getline (cin,name[count]);

// if (!cin) cout << "Stream is fouled!" << endl;


        cout << "Enter student's grade (0-100): ";
        cin >> grade[count];
        cin.ignore();
        total += grade[count];

        cout << "Enter another name? (y/n): ";
        cin >> yesno;
        cin.ignore();

        if (tolower(yesno) == 'n')
        {
            count++;
            break;
        }
    }

    cout << "The number of grades entered is " << count << "." << endl;
    cout << "The average grade is " << (total / count) << endl;

    cout << setw(20) << "Name" << setw(6) << "Grade" << endl;

    for (int i = 0; i < count; i++)
    {
        cout << setw(20) << name[i] << setw(6) << grade [i] << endl;
    }

    return 0;
}


Reply With Quote
  #2 (permalink)  
Old October 10th, 2004, 09:28 AM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Your symptoms exactly exhibit the known bug in the STRING header in Visual C++ version 6. The latest service pack that I could find is Service Pack 5, and it does not fix the problem.

Try this, from

http://support.microsoft.com/default...0015#appliesto


Quote:
quote:
This article was previously published under Q240015
SYMPTOMS
The Standard C++ Library template getline function reads an extra character after encountering the delimiter. Please refer to the sample program in the More Information section for details.
RESOLUTION
Modify the getline member function, which can be found in the following system header file string, as follows:
Code:
    else if (_Tr::eq((_E)_C, _D))
                {_Chg = true;
              //  _I.rdbuf()->snextc(); /* Remove this line and add the line below.*/ 
          _I.rdbuf()->sbumpc();
                break; }


NOTE: Because the resolution involves modifying a system header file, extreme care should be taken to ensure that nothing else is changed in the header file. Microsoft is not responsible for any problems resulting from unwanted changes to the system header files.
Regards,

Dave
Reply With Quote
  #3 (permalink)  
Old October 10th, 2004, 01:45 PM
Registered User
 
Join Date: Oct 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks - I thought I'd checked that file in SP6, and found it fixed, but it turns out I was wrong.

Changing STRING. did the trick. Thank you!

Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
cin.getLine() function skips. Please help! unpopular C++ Programming 3 February 29th, 2008 11:50 AM
more getline problmes Gubber C++ Programming 1 July 17th, 2004 06:16 PM
cin.getline alfrepa Visual C++ 0 April 14th, 2004 08:05 PM
Beginner Problems(maybe server) stu9820 ASP.NET 1.0 and 1.1 Basics 2 August 21st, 2003 01:30 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.