Article View: alt.comp.lang.learn.c-c++
Article #105Re: Problematic Program
From: Josh Sebastian
Date: Sun, 20 Oct 2002 23:41
Date: Sun, 20 Oct 2002 23:41
61 lines
3147 bytes
3147 bytes
On Sun, 20 Oct 2002 18:30:09 -0400, "CM" <mckenzie992@comcast.net>
wrote:
>Runtime error is that is allows one to type the first name of the student
>but errors thereafter
>a copy of below..
>const int noOfGrades=3;
>const int noOfStudents=5;
>char names[noOfStudents]; // array of names (strings)
This is not an array of strings. It is an array of individual
characters. For a given x, names[x] is *one* character. An array of
strings looks like this:
std::string names[noOfStudents];
Of course, that requires you to include the <string> header.
>char ID[noOfStudents]; // array of ID's (strings)
This is also an array of characters, not an array of strings.
>double Grades[noOfStudents][noOfGrades]; // array of 5 arrays each of 3
>//elements (each student has 3 double grades)
>double GPA[5]; // array of gpa's (doubles)
>float points;
>int i,j;
>
>for (i=0;i<noOfStudents;i++)
>{
>j=0;
>points=0;
>cout<<"Enter information for Student #"<<i+1<<":"<<endl;
>cout<<"\tName: ";
>//cin.getline(names[20],[i]);
>cin>>names[i];
This reads in the 'C' you typed. It only reads in one character
because names[i] is a char.
>cout<<"\n\tID: ";
>//cin.getline(ID[i],5);
>cin>>ID[i];
This tries to read the 'r' you typed into a single character, ID[i].
It succeeds.
>for (j=0;j<noOfGrades;j++)
>{
>cout<<"\t\nGrade #"<<j+1<<": ";
>cin>>Grades[i][j];
This tries to parse the "iag" part into a double. Of course, there's
no way to finagle a double out of "iag", so the read fails. By
default, when a read fails, cin won't let any subsequent read
operations succeed until you've acknowledged that there was an error
and clear the error state. You fail to ever check for an error, so all
subsequent read operations also fail.
HTH
Josh
Technical Details
Message-ID:
<7gf6ru0bggbt26llh51dthc3ag6ug36p9l@4ax.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!nntp-relay.ihug.net!ihug.co.nz!west.cox.net!cox.net!p01!news2.east.cox.net.POSTED!53ab2750!not-for-mailReferences:
<ESudnc40b4i4hS6gXTWcow@comcast.com> <3db31a56$1_1@mk-nntp-1.news.uk.worldonline.com> <BR2cndXHZoH7vi6gXTWcqg@comcast.com> <ur6a8ldf3qqc31@corp.supernews.com> <7FidnZKg45Ttsy6gXTWcoQ@comcast.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 114592,635d58b7486c58ea X-Google-Attributes: gid114592,public X-Google-Thread: 109fba,635d58b7486c58ea X-Google-Attributes: gid109fba,public X-Google-Thread: 10d4ba,635d58b7486c58ea X-Google-Attributes: gid10d4ba,public X-Google-ArrivalTime: 2002-10-20 16:41:35 PST Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!newsfeed.stanford.edu!logbridge.uoregon.edu!nntp-relay.ihug.net!ihug.co.nz!west.cox.net!cox.net!p01!news2.east.cox.net.POSTED!53ab2750!not-for-mail From: Josh Sebastian <curien@cox.net> Newsgroups: alt.comp.lang.learn.c-c++,att.lang.c++,comp.lang.c++ Subject: Re: Problematic Program Message-ID: <7gf6ru0bggbt26llh51dthc3ag6ug36p9l@4ax.com> References: <ESudnc40b4i4hS6gXTWcow@comcast.com> <3db31a56$1_1@mk-nntp-1.news.uk.worldonline.com> <BR2cndXHZoH7vi6gXTWcqg@comcast.com> <ur6a8ldf3qqc31@corp.supernews.com> <7FidnZKg45Ttsy6gXTWcoQ@comcast.com> X-Newsreader: Forte Agent 1.91/32.564 MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 61 Date: Sun, 20 Oct 2002 23:41:34 GMT NNTP-Posting-Host: 68.10.124.242 X-Complaints-To: abuse@cox.net X-Trace: news2.east.cox.net 1035157294 68.10.124.242 (Sun, 20 Oct 2002 19:41:34 EDT) NNTP-Posting-Date: Sun, 20 Oct 2002 19:41:34 EDT Organization: Cox Communications